首页 > 其他 > 详细

leetcode 买卖股票问题

时间:2018-09-06 13:33:52      阅读:203      评论:0      收藏:0      [点我收藏+]

leetcode121 Best Time to Buy and Sell Stock

说白了找到最大的两组数之差即可

 1 class Solution {
 2 public:
 3     int maxProfit(vector<int>& prices) {
 4         int m = 0;
 5         for(int i = 0; i < prices.size(); i++){
 6             for(int j = i + 1; j < prices.size(); j++){
 7                 m = max(m, prices[j] - prices[i]);
 8             }
 9         }
10         return m;
11     }
12 };

leetcode122 Best Time to Buy and Sell Stock II

关键在于明白股票可以当天卖出再买进

 1 class Solution {
 2 public:
 3     int maxProfit(vector<int>& prices) {
 4         if(prices.size() == 0) return 0;
 5         int m = 0;
 6         for(int i = 0; i < prices.size() - 1; i++){
 7             if(prices[i] < prices[i + 1]){
 8                 m += prices[i + 1] - prices[i];
 9             }
10         }
11         
12         return m;
13     }
14 };

 

leetcode 买卖股票问题

原文:https://www.cnblogs.com/zousantuier/p/9506022.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!