leetcode 121. Best Time to Buy and Sell Stock 122.Best Time to Buy and Sell Stock II
2018-08-30 17:43
429 查看
121
class Solution { public: int maxProfit(vector<int>& prices) { int length = prices.size(); if(length <= 0) return 0; int min_num = 0x7FFFFFFF; int max_sum = 0; for(int i = 0;i < length;i++){ if(prices[i] < min_num) min_num = prices[i]; if((prices[i] - min_num) > max_sum) max_sum = prices[i] - min_num; } return max_sum; } };
122
class Solution { public: int maxProfit(vector<int>& prices) { int length = prices.size(); if(length <= 0) return 0; int result = 0; for(int i = 1;i < length;i++){ if(prices[i] - prices[i-1] > 0) result += prices[i] - prices[i-1]; } return result; } };
https://www.jianshu.com/p/34bbb0594bd9
相关文章推荐
- 【LeetCode-面试算法经典-Java实现】【121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)】
- LeetCode | 121. Best Time to Buy and Sell Stock循环技巧题
- leetcode解题报告:121 Best Time to Buy and Sell Stock
- leetCode 121. Best Time to Buy and Sell Stock 数组
- leetcode121 Best Time to Buy and Sell Stock
- leetcode top 100 的121_BestTimetoBuyandSellStock
- 【LeetCode-面试算法经典-Java实现】【121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)】
- leetcode-121 Best Time to Buy and Sell Stock
- leetcode121——Best Time to Buy and Sell Stock
- [LeetCode]: 121: Best Time to Buy and Sell Stock
- [LeetCode] 121 - Best Time to Buy and Sell Stock
- LeetCode 121 -Best Time to Buy and Sell Stock ( JAVA )
- LeetCode(121) Best Time to Buy and Sell Stock (Java)
- LeetCode题解:121. Best Time to Buy and Sell Stock
- LeetCode 121 Best Time to Buy and Sell Stock
- leetcode(121)(122)(123)(188) Best Time to Buy and Sell Stock I/II/III/IV JAVA代码
- LeetCode 121 Best Time to Buy and Sell Stock
- LeetCode-121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)
- leetcode【121+122+123 best time to buy and sell stock】【python】
- Leetcode_c++: Best Time to Buy and Sell Stock (121)