-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path122.买卖股票的最佳时机-ii.cpp
More file actions
41 lines (39 loc) · 987 Bytes
/
122.买卖股票的最佳时机-ii.cpp
File metadata and controls
41 lines (39 loc) · 987 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*
* @lc app=leetcode.cn id=122 lang=cpp
*
* [122] 买卖股票的最佳时机 II
*/
// @lc code=start
class Solution {
public:
int maxProfit(vector<int>& prices) {
int result = 0;
int buy = -1;
// prices 容器为空时出错,prices.size()是一个无符号整数。
for (int i = 0; i < int(prices.size()) - 1; i++)
{
if (prices.at(i) < prices.at(i + 1)) //
{
if (buy == -1) // 买入。
{
buy = prices.at(i);
}
}
else
{
if (buy != -1) // 卖出
{
result += prices.at(i) - buy;
buy = -1; // 清仓。
}
}
}
if (buy != -1) // 仓库剩余。
{
result += prices.at(prices.size() - 1) - buy;
buy = -1;
}
return result;
}
};
// @lc code=end