-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path53Maximum_Subarray.java
More file actions
37 lines (31 loc) · 973 Bytes
/
53Maximum_Subarray.java
File metadata and controls
37 lines (31 loc) · 973 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import java.lang.Math;
class Solution {
public int maxSubArray(int[] nums) {
if(nums.length == 0){
return 0;
}
int max = nums[0]; //最大值,初始化为nums[0],切忌设置为0,或者设置成min
int temp = nums[0]; //局部最大值,不断更新
for( int i = 1; i< nums.length; i++){
if(temp + nums[i] >= nums[i]){
temp = temp + nums[i];
}else{
temp = nums[i];
}
if(temp > max){
max = temp;
}
}
return max;
}
}
/* dp 动态规划
int res = nums[0];
int[] dp = new int[nums.length];
dp[0] = nums[0]; //base case
for (int i = 1; i < nums.length; i++) {
dp[i] = Math.max(dp[i - 1] + nums[i], nums[i]);
res = res > dp[i] ? res : dp[i];
}
return res;
*/