首页 > 其他 > 详细

121. Best Time to Buy and Sell Stock

时间:2016-06-15 06:55:27      阅读:250      评论:0      收藏:0      [点我收藏+]

顺着数组走,保存:

1.到目前为止最大profit

2.到目前为止最小price

更新两个数据,结尾返回maxProfit

 1    public int maxProfit(int[] prices) {
 2         if(prices.length < 1) {
 3             return 0;
 4         }
 5         int minPrice = prices[0];
 6         int maxProfit = 0;
 7         for(int i = 1; i < prices.length; i++) {
 8             minPrice = (prices[i] < minPrice)? prices[i]: minPrice;
 9             maxProfit = (maxProfit < prices[i] - minPrice)? prices[i] - minPrice: maxProfit;
10         }
11         return maxProfit;
12     }

 

121. Best Time to Buy and Sell Stock

原文:http://www.cnblogs.com/warmland/p/5586103.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!