首页 > 其他 > 详细

122. Best Time to Buy and Sell Stock II

时间:2018-09-23 00:44:10      阅读:175      评论:0      收藏:0      [点我收藏+]
 1 class Solution {
 2     public int maxProfit(int[] prices) {
 3         int len = prices.length;
 4         if(len == 0 || len == 1) return 0;
 5         int res = 0;
 6         int buy = prices[0];
 7         int buyday = 0;
 8         for(int i = 1; i < len; i++) {
 9             int curprice = prices[i];
10             if(curprice <= prices[i-1]) {
11                 if(i - 1 != buyday) {
12                     res += prices[i-1] - buy;
13                 }
14                 buy = prices[i];
15                 buyday = i;
16             }else if (i == len - 1) {
17                 res += prices[i] - buy;
18             }
19             
20         }
21         return res;     
22     }
23 }

 

122. Best Time to Buy and Sell Stock II

原文:https://www.cnblogs.com/goPanama/p/9691669.html

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