LeetCode 122: Best Time to Buy and Sell Stock II
2015-05-11 21:16
507 查看
对比vector中前后两个数,如果后者大于前者,存下差值,所有差值之和就是结果。
class Solution {
public:
int maxProfit(vector<int>& prices) {
int size=prices.size();
int maxProfit=0;
for (int i=0;i<size-1;i++ ){
if (prices.at(i)<prices.at(i+1)){
maxProfit=maxProfit+prices.at(i+1)-prices.at(i);
}
}
return maxProfit;
}
};
class Solution {
public:
int maxProfit(vector<int>& prices) {
int size=prices.size();
int maxProfit=0;
for (int i=0;i<size-1;i++ ){
if (prices.at(i)<prices.at(i+1)){
maxProfit=maxProfit+prices.at(i+1)-prices.at(i);
}
}
return maxProfit;
}
};
相关文章推荐
- LeetCode 121, 122, 123. Best Time to Buy and Sell Stock i, ii, iii
- 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
- [leetcode 122] Best Time to Buy and Sell Stock II
- LeetCode 122 Best Time to Buy and Sell Stock II
- LeetCode122: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
- leetcode_question_122 Best Time to Buy and Sell Stock II
- LeetCode122 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
- Java [Leetcode 122]Best Time to Buy and Sell Stock II
- LeetCode-122-Best Time to Buy and Sell Stock II-E
- LeetCode【122】Best Time to Buy and Sell Stock II
- [Leetcode 122, Medium] Best Time to Buy and Sell Stock II
- leetcode122~Best Time to Buy and Sell Stock II