leetcode 122. Best Time to Buy and Sell Stock II
2016-05-23 14:31
141 查看
题目
Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
解
public class Solution { public int maxProfit(int[] prices) { int ret=0; if(prices.length<2){ return ret; } for(int i=1;i<prices.length;i++){ if(prices[i]>prices[i-1]){ ret+=(prices[i]-prices[i-1]); } } return ret; } }
相关题目的参考链接
http://liangjiabin.com/blog/2015/04/leetcode-best-time-to-buy-and-sell-stock.html相关文章推荐
- 使用开单引号对SQL语句中的表字段名转义
- Cf #353 D. Tree Construction
- c# 导入c++ dll
- 制作个人网页
- python中给for循环增加索引
- 【CSS】——三大基础选择器以及优先级
- TensorFlow 从入门到精通(六):tensorflow.nn 详解
- Struts2拦截器
- windows平台上的NDK开发
- iOS学习之——prepareForSegue:sender:
- Ant工具介绍
- java.lang.OutOfMemoryError: unable to create new native thread
- Android 头部设置上下滚动的消息列表 TextView (TextSwitcher运用)
- Apache HAWQ集群的安装部署
- 使用存储过程
- Android视图切换动画库SwitchLayout的简单使用
- [POJ3070]Fibonacci(矩阵快速幂)
- VS2010静态编译生成.exe可执行文件
- solr4.7新建core
- arm交叉变异环境安装