122. Best Time to Buy and Sell Stock II
来源:互联网 发布:淘宝预售发货时间 编辑:程序博客网 时间:2024/06/05 10:51
class Solution {
public:
int maxProfit(vector<int>& prices) {
if(prices.size()<2) return 0;
int min=prices[0],maxpro=0,max=0;
for (int i=1;i<prices.size();i++){
if(prices[i-1]>prices[i]){
min=prices[i];
maxpro+=max;
max=0;
}
else{
if(max<prices[i]-min)
max=prices[i]-min;
}
}
return maxpro+max;
}
};
/*class Solution {
public:
int maxProfit(vector &a) {
int profit = 0;
for (int i = 1; i < a.size(); ++i) {
if (a[i] > a[i-1]) {
profit += a[i]-a[i-1];
}
}
return profit;
}
};*/
public:
int maxProfit(vector<int>& prices) {
if(prices.size()<2) return 0;
int min=prices[0],maxpro=0,max=0;
for (int i=1;i<prices.size();i++){
if(prices[i-1]>prices[i]){
min=prices[i];
maxpro+=max;
max=0;
}
else{
if(max<prices[i]-min)
max=prices[i]-min;
}
}
return maxpro+max;
}
};
/*class Solution {
public:
int maxProfit(vector &a) {
int profit = 0;
for (int i = 1; i < a.size(); ++i) {
if (a[i] > a[i-1]) {
profit += a[i]-a[i-1];
}
}
return profit;
}
};*/
0 0
- 122. Best Time to Buy and Sell Stock II
- LeetCode 122. Best Time to Buy and Sell Stock II
- 122.Best Time to Buy and Sell Stock II
- [Leetcode] 122. Best Time to Buy and Sell Stock II
- [leetcode] 122.Best Time to Buy and Sell Stock II
- 【leetcode】122.Best Time to Buy and Sell Stock II
- 122. Best Time to Buy and Sell Stock II
- 【LeetCode】122.Best Time to Buy and Sell Stock II
- [leetcode] 122. Best Time to Buy and Sell Stock II
- LeetCode #122. Best Time to Buy and Sell Stock II
- 122. Best Time to Buy and Sell Stock II LeetCode
- 122. Best Time to Buy and Sell Stock II
- 122. Best Time to Buy and Sell Stock II
- 122. Best Time to Buy and Sell Stock II
- [LeetCode]122. Best Time to Buy and Sell Stock II
- 122. Best Time to Buy and Sell Stock II
- Leetcode 122. Best Time to Buy and Sell Stock II
- leetcode 122. Best Time to Buy and Sell Stock II
- L1-002. 打印沙漏(C++)
- Sql server 2005 行列与转
- leetcode 260.Single Number
- 1*1
- 平衡二叉树
- 122. Best Time to Buy and Sell Stock II
- 2016SDAU课程练习三1001
- TinyXML的TiXmlElement::GetText()返回NULL
- 浅谈MVP in Android
- java Socket实现简单在线聊天(二)
- 注册码生成器 略屌略屌
- apache支持php解析(php安装后无法解析脚本)
- PHP的命名空间
- commons-httpclient post请求乱码问题记录(非编码问题,gzip格式问题)