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

[LeetCode] Best Time to Buy and Sell Stock

时间:2014-09-08 01:00:16      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   for   div   sp   log   on   

 1 public class Solution {
 2     public int maxProfit(int[] prices) {
 3         if (prices.length <= 1) return 0;
 4         
 5         int profit = 0;
 6         int buyPrices=prices[0];
 7         
 8         for (int i=1; i<prices.length; i++) {
 9             profit = Math.max(profit, prices[i]-buyPrices);
10             if (buyPrices > prices[i]) buyPrices = prices[i];
11         }
12         return profit;
13     }
14 }

 

[LeetCode] Best Time to Buy and Sell Stock

标签:style   blog   color   io   for   div   sp   log   on   

原文地址:http://www.cnblogs.com/yuhaos/p/3961007.html

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