您的位置:首页 > 其它

122 Best Time to Buy and Sell Stock II

2015-11-23 14:09 288 查看
与一非常类似。只需要改一个条件。

public class Solution {
public int maxProfit(int[] prices) {
if (prices == null || prices.length <= 1) {
return 0;
}

int max = Integer.MIN_VALUE;
int sum = 0;
for (int i = 1; i < prices.length; i++) {
int localprofit = prices[i] - prices[i - 1];
if (localprofit > 0) {
sum+=localprofit;
}
max = Math.max(max, sum);
}
return max;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: