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

Posted dbbf

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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     }

 






以上是关于Array——LeetCode——Best Time to Buy and Sell Stock II的主要内容,如果未能解决你的问题,请参考以下文章