优化0122.买卖股票的最佳时机II Java代码
This commit is contained in:
parent
d6531b0f2b
commit
48ab781c4e
|
|
@ -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 sum;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue