leetcode 122. Best Time to Buy and Sell Stock II ----- java
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 len = prices.length;
if( len < 2)
return 0;
int result = 0;
int start = 0;
while( start < len ){
start = getStart(prices,start);
if( start == len-1)
break;
result+=(prices[start+1]-prices[start]);
start++;
}
return result;
} public int getStart(int[] prices,int start){
int buy = prices[start];
while( start < prices.length ){
if( buy >= prices[start]){
buy = prices[start];
start++;
}else
break;
}
return start-1; }
}
leetcode 122. Best Time to Buy and Sell Stock II ----- java的更多相关文章
- 31. leetcode 122. Best Time to Buy and Sell Stock II
122. Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price ...
- [LeetCode] 122. Best Time to Buy and Sell Stock II 买卖股票的最佳时间 II
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- LeetCode 122. Best Time to Buy and Sell Stock II (stock problem)
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 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 al ...
- Java [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 ...
- 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 ...
- [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 al ...
- Java for 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 al ...
- LeetCode 122 Best Time to Buy and Sell Stock II(股票买入卖出的最佳时间 II)
翻译 话说你有一个数组,当中第i个元素表示第i天的股票价格. 设计一个算法以找到最大利润. 你能够尽可能多的进行交易(比如.多次买入卖出股票). 然而,你不能在同一时间来多次交易. (比如.你必须在下 ...
随机推荐
- iOS9/iOS8界面对比 XCode7
Xcde7 bate 无需开发这账号(99¥)可以调试程序 目前是测试版 iOS9/iOS8界面对比 (注:左边为iOS8界面,右边为iOS9界面.) 1.新字体 苹果在 iOS9 中使用旧金山字体取 ...
- Spring学习笔记之模块简介
1.Core Container(Application context) module 这个是Spring最基本的模块,它提供了spring框架最基本的功能.BeanFactory 是任何基于Spr ...
- jQuery 通用表单方法
表单验证一直是一个麻烦的事情,让很多人望而退步,之前想过一个验证的好方法,但是有bug,昨晚请教了juyling.com的王员外,顺利解决. 以下是js代码 function mySubmit ...
- 未能加载文件或程序集“System.WEB.DataVisualization, Version=3.5.0.0, Culture=neutral
项目打开 提示 如题错误. 最近用VS2010 + .NET Framework3.5SP1开发程序,使用了MsChart,但是部署到服务器的时候提示如下错误: 分析器错误消息: 未能加载文件或程序集 ...
- JVM-class文件完全解析-魔数
魔数(Magic Number) 魔数和Class文件的版本. 一个文件能否被Java虚拟机接受,不是通过文件的扩展名来进行识别的,而是通过魔数来进行识别.这主要是基于安全方面的考虑,因为文件的扩展名 ...
- JAVA SERVLET专题(下)
HTTP简介 ·WEB浏览器与WEB服务器之间的一问一答的交互过程必须遵守一定的规则,这个规则就是HTTP协议. ·HTTP是hypertext transfer protocol(超文本传输协议)的 ...
- hdu1005 矩阵
//Accepted hdu1005 0MS 248K #include <cstdio> #include <cstring> #include <iostream&g ...
- 12-26 tableView的学习心得
一:基础部分 UITableView的两种样式: 注意是只读的 1.UITableViewStytlePlain(不分组的) n 2.UITableViewStyleGrouped(分组的) 二:如何 ...
- Python的"数组"储存
import time member=["小甲鱼","小布丁","黑夜","迷途","伊静"] pr ...
- BZOJ 2393 Cirno的完美算数教室
就是爆搜嘛. 先从大到小排个序能减去dfs树上很大的一部分.这个技巧要掌握. #include<iostream> #include<cstdio> #include<c ...