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

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

时间:2018-09-13 12:03:55      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:fit   解法   leetcode   amp   别人   static   收盘价   style   color   

【学到的知识点——】

-----------------------------------------------------------------------------------------------------
【反思】
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

标签:fit   解法   leetcode   amp   别人   static   收盘价   style   color   

原文地址:https://www.cnblogs.com/Dbbf/p/9639394.html

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