Best Time to Buy and Sell Stock II ---- LeetCode
来源:互联网 发布:安卓传感器编程 编辑:程序博客网 时间:2024/06/02 06:05
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 max=0;
int len=prices.length;
if(len==0||len==1){
return 0;
}
int low=prices[0];
int profit=0;
int i=0;
boolean flag=false;
while(i<len){
low=prices[i];
i++;
while(i<len&&prices[i]>=prices[i-1]){
i++;
flag=true;
}
if(flag){
profit=prices[i-1]-low;
max=max+profit;
flag=false;
}
}
return max;
}
}
- [LeetCode]Best Time to Buy and Sell Stock II
- LeetCode Best Time to Buy and Sell Stock II
- Leetcode: Best Time to Buy and Sell Stock II
- [Leetcode] Best Time to Buy and Sell Stock II
- LeetCode : Best Time to Buy and Sell Stock II
- LeetCode Best Time to Buy and Sell Stock II
- [LeetCode] Best Time to Buy and Sell Stock II
- leetcode 61: Best Time to Buy and Sell Stock II
- [LeetCode] Best time to buy and sell a stock II
- 【leetcode】Best Time to Buy and Sell Stock II
- LeetCode - Best Time to Buy and Sell Stock II
- LeetCode之Best Time to Buy and Sell Stock II
- [LeetCode]Best Time to Buy and Sell Stock II
- [Leetcode]Best Time to Buy and Sell Stock II
- [leetcode]Best Time to Buy and Sell Stock II
- [LeetCode] Best Time to Buy and Sell Stock II
- LeetCode-Best Time to Buy and Sell Stock II
- [leetcode] Best Time to Buy and Sell Stock II
- Android中Root权限获取的简单代码(3)
- 丰富的IOS界面元素
- 1501081232-hpu-1016: 【C语言训练】大、小写问题
- 织梦DedeCMS TAG标签出现"系统无此标签,可能已经移除"的解决方法
- [Windows]_[初级]_[Windows SDK实用工具简介][持续更新]
- Best Time to Buy and Sell Stock II ---- LeetCode
- C语言基础:C语言变量类型 - 全局变量和局部变量
- ORACLE PL/SQL编程之四:把游标说透
- linux之cut用法
- Afinal框架FinalBitmap使用方法
- 程序员 之对于个人开发的核心要素
- C++笔记:STL扩展hash_map
- CentOS命令行中执行Java代码的一些操作
- Adobe Photoshop CC多张图片制作gif教程