Best Time to Buy and Sell Stock II 买卖股票最好的时间II
2014-01-21 10:49
48 查看
class Solution {
public:
//贪心法,低进高出,把所有正的价格差价相加起来。
int maxProfit(vector<int> &prices) {
int n=prices.size();
if(n<2)
return 0;
int profit=0;
int minbuy=prices[0];
for(int i=1;i<prices.size();i++)
{
if(prices[i]>prices[i-1])
profit+=prices[i]-prices[i-1];
}
return profit;
}
};
public:
//贪心法,低进高出,把所有正的价格差价相加起来。
int maxProfit(vector<int> &prices) {
int n=prices.size();
if(n<2)
return 0;
int profit=0;
int minbuy=prices[0];
for(int i=1;i<prices.size();i++)
{
if(prices[i]>prices[i-1])
profit+=prices[i]-prices[i-1];
}
return profit;
}
};
相关文章推荐
- 122. Best Time to Buy and Sell Stock II 最佳买卖股票的时间
- [LeetCode] 122. Best Time to Buy and Sell Stock II 买卖股票的最佳时间 II
- LeetCode OJ 之 Best Time to Buy and Sell Stock II (买卖股票的最佳时间 2)
- Best Time to Buy and Sell Stock II 最佳时间买入卖出股票(多次买卖)@LeetCode
- Best Time to Buy and Sell Stock 买卖股票最好的时间
- 找出买卖股票的最大收益及其时间
- [LeetCode] Best Time to Buy and Sell Stock IV 买卖股票的最佳时间之四
- [LeetCode] Best Time to Buy and Sell Stock I II III IV 股票买卖
- lintcode:买卖股票的最佳时机 II
- Best Time to Buy and Sell Stock III 买卖股票的最好时间
- lintcode:买卖股票的最佳时机 II
- [LintCode] Best Time to Buy and Sell Stock 买卖股票的最佳时间
- LintCode_150_买卖股票的最佳时机 II
- Best Time to Buy and Sell Stock 【最佳买卖股票的时间】【容易】
- [LeetCode] Best Time to Buy and Sell Stock 买卖股票的最佳时间
- Leetcode#122. Best Time to Buy and Sell Stock II(股票多次买卖的最佳时间--贪心)
- 最佳买卖股票时间
- 买卖股票的最佳时机 II
- LintCode--best-time-to-buy-and-sell-stock-ii(买卖股票的最佳时机 II)
- LintCode--买卖股票的最佳时机 II