LeetCode - BestTimetoBuyandSellStockII
2016-05-29 08:30
344 查看
/**
* 问:已知某股票的价格数组,且该股票可买卖很多次,但是只有卖了才能重新买。求最大收益。
* 解:贪心算法:只要能赚钱就卖。
*/
public class BestTimetoBuyandSellStockII {
public int maxProfit(int[] prices) {
int result = 0;
for (int i=0; i<prices.length-2; i++) {
if (prices[i+1] >prices[i])
result += prices[i+1] - prices[i];
}
return result;
}
public static void main(String[] args) {
int[] prices = {9, 1, 2, 8, 3, 7};
System.out.println("最大收益:" + new BestTimetoBuyandSellStockII().maxProfit(prices));
}
}
* 问:已知某股票的价格数组,且该股票可买卖很多次,但是只有卖了才能重新买。求最大收益。
* 解:贪心算法:只要能赚钱就卖。
*/
public class BestTimetoBuyandSellStockII {
public int maxProfit(int[] prices) {
int result = 0;
for (int i=0; i<prices.length-2; i++) {
if (prices[i+1] >prices[i])
result += prices[i+1] - prices[i];
}
return result;
}
public static void main(String[] args) {
int[] prices = {9, 1, 2, 8, 3, 7};
System.out.println("最大收益:" + new BestTimetoBuyandSellStockII().maxProfit(prices));
}
}
相关文章推荐
- 常见SQL异常及解决方法
- 常用设计模式大全-详细介绍
- 【转载】MFC键盘响应
- 第十三周进度条
- 【转载】MFC键盘响应
- Linux ACL
- DB_NAME、DBID、DB_UNIQUE_NAME、SERVICE_NAME、SID、INSTANCE_NAME、GLOBAL_DATABASE_NAME
- LeetCode - BestTimetoBuyandSellStockI
- cxf发布webservice简介 及 wsdl2java生成客户端代码
- Cocos2dx3.10在Windows平台搭建Android命令行编译apk打包
- 关系型数据库
- LeetCode - ClimbingStairs
- 第十三周学习进度情况
- 并查集入门
- windows下php:error_log()发送邮件设置
- jdbc一个connection 下的事务问题[转]
- LeetCode - RemoveElement
- myeclipse中,项目上有个叉报错,文件没有错误【解决方案】
- LeetCode - UniquePaths
- 第二次冲刺个人博客05