122. Best Time to Buy and Sell Stock II

 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 }

 

posted @ 2018-09-23 00:09  jasoncool1  阅读(109)  评论(0编辑  收藏  举报