Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
30 lines (29 loc) · 865 Bytes

File metadata and controls

30 lines (29 loc) · 865 Bytes
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package com.yangchd.leetcode.easy;
/**
* @author yangchd 2018/10/10.
*
* 53. Maximum Subarray
*
* Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
*
* Example:
* Input: [-2,1,-3,4,-1,2,1,-5,4],
* Output: 6
* Explanation: [4,-1,2,1] has the largest sum = 6.
*
* Follow up:
* If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.
*/
public class MaximumSubarray {
class Solution {
public int maxSubArray(int[] nums) {
int cur = 0;
int large = Integer.MIN_VALUE;
for (int num : nums) {
cur = Math.max(cur + num, num);
large = Math.max(large, cur);
}
return large;
}
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.