Array——LeetCode——Best Time to Buy and Sell Stock II

【学到的知识点——】

-----------------------------------------------------------------------------------------------------
【反思】
1、max每次都加自己。
-----------------------------------------------------------------------------------------------------
【别人的Java解法代码】

-----------------------------------------------------------------------------------------------------
【自己的Java解法代码】

 1     public  static int maxProfit(int[] prices) {
 2         int max = 0;
 3         int tmpMax = 0;
 4         int tmp = 0;                                        //当前股票最低价格
 5         if (prices.length != 0 && prices != null) {
 6              tmp = prices[0];
 7          }
 8         for (int i = 1; i < prices.length; i++) {
 9             if (prices[i] >= prices[i-1]) {            //股价持续上涨,或者持平,不卖出
10                 if (i == prices.length - 1) {
11                     //收盘价格
12                     tmpMax = prices[i] - tmp;
13                     max = tmpMax + max;
14                 }        
15             } else {                                                //股价下降
16                 tmpMax = prices[i-1] -tmp;
17                 tmp = prices[i];
18                 max = tmpMax + max;
19             }
20         }
21         return max;
22     }

猜你喜欢

转载自www.cnblogs.com/Dbbf/p/9639394.html