Skip to content

Commit fc56c8a

Browse files
authored
Update Solution.java
add easy solution
1 parent 482bbf4 commit fc56c8a

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
class Solution {
22
public int maxSubArray(int[] nums) {
3-
int ans = nums[0];
4-
for (int i = 1, f = nums[0]; i < nums.length; ++i) {
5-
f = Math.max(f, 0) + nums[i];
6-
ans = Math.max(ans, f);
3+
int pre = 0, maxAns = nums[0];
4+
for (int x : nums) {
5+
pre = Math.max(pre + x, x);
6+
maxAns = Math.max(maxAns, pre);
77
}
8-
return ans;
8+
return maxAns;
99
}
10-
}
10+
}

0 commit comments

Comments
 (0)