-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
38 lines (34 loc) · 1.16 KB
/
Solution.java
File metadata and controls
38 lines (34 loc) · 1.16 KB
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
38
package Array.No_53_Maximum_Subarray;
/**
* FileName: Solution
* Author: EdisonLi的家用MacBook Pro
* Date: 2019-02-09 18:48
* Description: Maximum Subarray
* <p>
* Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
* <p>
* Example:
* <p>
* Input: [-2,1,-3,4,-1,2,1,-5,4],
* Output: 6
* Explanation: [4,-1,2,1] has the largest sum = 6.
* Follow up:
* <p>
* 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 Solution {
public int maxSubArray(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
int sum = nums[0];
int max = nums[0];
//分治法
//判断相加后的数值 与 下一个数字的大小进行比较 若之后的数大于之前的数值 那么之前的数就不必要了 从这个数开始继续往后找
for (int i = 1; i < nums.length; i++) {
sum = Math.max(sum + nums[i], nums[i]);
max = Math.max(max, sum);
}
return max;
}
}