面试经典 150 题:121,125
121. 买卖股票的最佳时机
【参考代码】
动态规划解决
class Solution {
public:
int maxProfit(vector<int>& prices) {
int size = prices.size();
int min_price = 99999, max_profit = 0;
for(int i=0; i<size; i++){
if(prices[i] < min_price){
min_price = prices[i];
}
else if(prices[i] - min_price > max_profit){
max_profit = prices[i] - min_price;
}
}
return max_profit;
}
};
125. 验证回文串
【参考代码】
class Solution {
public:
bool isPalindrome(string s) {
string s1 = "";
int size = s.size();
if(size == 0)
{
return true;
}
for(char c : s){
if(isalnum(c)){
s1 += tolower(c);
}
}
string temp = s1;
reverse(temp.begin(), temp.end());
if(temp == s1)
{
return true;
}
else{
return false;
}
}
};