public class S053 { //kadane‘s algorithm //最大子数组第一个数肯定大于零,从最大子数组中某个数开始往前的数之和肯定大于零 public int maxSubArray(int[] nums) { if(nums.length == 0) return 0; int sum = 0; int result = Integer.MIN_VALUE; for(int i =0;i<nums.length;i++){ if(sum<0) sum=0; sum+= nums[i]; if(sum>result) result = sum; } return result; } }
时间: 2024-10-14 08:49:58