码迷,mamicode.com
首页 > 其他好文 > 详细

122. Best Time to Buy and Sell Stock II

时间:2018-09-23 00:48:07      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:length   ice   nbsp   ==   ||   time   bsp   and   else   

 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

标签:length   ice   nbsp   ==   ||   time   bsp   and   else   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!