mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
添加0053.最大子序和(动态规划)空间复杂度为O(1)的解法 Java版本
This commit is contained in:
@ -120,6 +120,20 @@ Java:
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
```Java
|
||||||
|
//因为dp[i]的递推公式只与前一个值有关,所以可以用一个变量代替dp数组,空间复杂度为O(1)
|
||||||
|
class Solution {
|
||||||
|
public int maxSubArray(int[] nums) {
|
||||||
|
int res = nums[0];
|
||||||
|
int pre = nums[0];
|
||||||
|
for(int i = 1; i < nums.length; i++) {
|
||||||
|
pre = Math.max(pre + nums[i], nums[i]);
|
||||||
|
res = Math.max(res, pre);
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
```python
|
```python
|
||||||
|
Reference in New Issue
Block a user