Skip to content

Commit 3663a66

Browse files
authored
add leetcode
1 parent 0a9ddf4 commit 3663a66

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

algorithm/53. Maximum Subarray.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.
2+
3+
For example, given the array `[-2,1,-3,4,-1,2,1,-5,4]`,
4+
the contiguous subarray `[4,-1,2,1]` has the largest sum = `6`.
5+
6+
**代码**
7+
```
8+
class Solution {
9+
public int maxSubArray(int[] nums) {
10+
int sum = nums[0];
11+
int t = sum;
12+
for(int i=1; i<nums.length; i++) {
13+
if (t < 0) t = 0;
14+
t += nums[i];
15+
if (t>sum) sum = t;
16+
}
17+
return sum;
18+
}
19+
}
20+
```

0 commit comments

Comments
 (0)