mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 20:40:39 +08:00
优化0122.买卖股票的最佳时机II Java代码
This commit is contained in:
@ -139,17 +139,11 @@ Java:
|
||||
// 贪心思路
|
||||
class Solution {
|
||||
public int maxProfit(int[] prices) {
|
||||
int sum = 0;
|
||||
int profit = 0;
|
||||
int buy = prices[0];
|
||||
int result = 0;
|
||||
for (int i = 1; i < prices.length; i++) {
|
||||
profit = prices[i] - buy;
|
||||
if (profit > 0) {
|
||||
sum += profit;
|
||||
}
|
||||
buy = prices[i];
|
||||
result += Math.max(prices[i] - prices[i - 1], 0);
|
||||
}
|
||||
return sum;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
Reference in New Issue
Block a user