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

买卖股票的最佳时机||

时间:2017-07-22 18:09:11      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:blog   length   src   ret   img   images   imu   array   str   

代码(C++):

class Solution {
/**
* @param prices: Given an integer array
* @return: Maximum profit
*/
public int maxProfit(int[] prices) {
// write your code here
if (0 == prices.length || null == prices) return 0;
int minPrice = prices[0];
int sumProfit = 0;
for (int i = 1; i < prices.length; i++) {
if (minPrice < prices[i]) {
sumProfit += prices[i] - minPrice;
minPrice = prices[i];
} else {
minPrice = prices[i];
}
}
return sumProfit;
}

};

技术分享

买卖股票的最佳时机||

标签:blog   length   src   ret   img   images   imu   array   str   

原文地址:http://www.cnblogs.com/majixian/p/7221790.html

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