leetcode-122. Best Time to Buy and Sell Stock II
2017-01-04 17:04
316 查看
leetcode-122. Best Time to Buy and Sell Stock II
题目: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) { if(prices.length<2) return 0; int ret = 0; for(int i = 1 ; i< prices.length ; i++){ ret += prices[i]>prices[i-1] ? prices[i]-prices[i-1] : 0; } return ret; } }
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- Debian 7.x 安装Oracle JAVA
- springmvc实现url路由功能
- spring boot 配置 druid/** * 配置druid * Created by adam on 4/11/16. */ @Configuration public class D