738.单调递增的数字
class Solution {
public:
int monotoneIncreasingDigits(int n) {
string str_num = to_string(n);
int flag = str_num.size();
for (int i = str_num.size() - 1; i > 0; i--)
{
if (str_num[i - 1] > str_num[i])
{
flag = i;
str_num[i - 1]--;
}
}
for (int i = flag; i < str_num.size(); i++)
{
str_num[i] = '9';
}
return stoi(str_num);
}
};
714. 买卖股票的最佳时机含手续费
class Solution {
public:
int maxProfit(vector& prices, int fee) {
int length = prices.size();
int buy = prices[0] + fee;
int profit = 0;
for (int i = 1; i < length; i++)
{
if (prices[i] + fee < buy)
{
buy = prices[i] + fee;
}
else if (prices[i] > buy)
{
profit += prices[i] - buy;//局部最优,不一定是最贵的时候卖的!
buy = prices[i];//为了解决局部最优到全局最优的问题
}
}
return profit;
}
};
968.监控二叉树