mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
格式化了122的一维数组java代码,其实就是加了空格
This commit is contained in:
@ -171,15 +171,15 @@ class Solution { // 动态规划
|
||||
// 优化空间
|
||||
class Solution {
|
||||
public int maxProfit(int[] prices) {
|
||||
int[] dp=new int[2];
|
||||
int[] dp = new int[2];
|
||||
// 0表示持有,1表示卖出
|
||||
dp[0]=-prices[0];
|
||||
dp[1]=0;
|
||||
for(int i=1; i<=prices.length; i++){
|
||||
dp[0] = -prices[0];
|
||||
dp[1] = 0;
|
||||
for(int i = 1; i <= prices.length; i++){
|
||||
// 前一天持有; 或当天卖出然后买入
|
||||
dp[0]=Math.max(dp[0], dp[1]-prices[i-1]);
|
||||
dp[0] = Math.max(dp[0], dp[1] - prices[i-1]);
|
||||
// 前一天卖出; 或当天卖出,当天卖出,得先持有
|
||||
dp[1]=Math.max(dp[1], dp[0]+prices[i-1]);
|
||||
dp[1] = Math.max(dp[1], dp[0] + prices[i-1]);
|
||||
}
|
||||
return dp[1];
|
||||
}
|
||||
|
Reference in New Issue
Block a user