Skip to content

Commit

Permalink
from josonlee:增添数组最大子序和
Browse files Browse the repository at this point in the history
  • Loading branch information
josonle committed May 9, 2019
1 parent 0690abf commit 17a665c
Showing 1 changed file with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package Coding190509;

/**
* @author josonlee
* Topic53:https://leetcode-cn.com/problems/maximum-subarray/
* 最大子序和
* 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。
*/
public class maximumSubarray {

public static void main(String[] args) {
// TODO Auto-generated method stub
int[] nums = {-2,1,-3,4,-1,2,1,-5,4};
System.out.println(maxSubArray(nums));
}

public static int maxSubArray(int[] nums) {
if(nums==null||nums.length==0) return 0;
int result = nums[0];
int sum = 0;
// for(int a : nums) {
// if(sum>=0)
// sum += a;
// else //之前和sum<0,a<0时加上a只能更小,a>0时加上还是比a小,不如就等于a
// sum = a;
// result = Math.max(sum, result);
// }
for (int i : nums) {//这样更快一点,but ???
if(sum<0)
sum = 0;
sum += i;
result = Math.max(sum, result);
}
return result;
}
}

0 comments on commit 17a665c

Please sign in to comment.