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