package normal; /** * @program JavaBooks * @description: 53. æå¤§ååºå * @author: mf * @create: 2019/11/10 10:37 */ /* é¢ç®ï¼https://leetcode-cn.com/problems/maximum-subarray/ ç±»åï¼å¨æè§å é¾åº¦ï¼easy */ /* è¾å ¥: [-2,1,-3,4,-1,2,1,-5,4], è¾åº: 6 è§£é: è¿ç»åæ°ç» [4,-1,2,1] çåæå¤§ï¼ä¸º 6ã */ public class MaxSubArray { public static void main(String[] args) { 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 max = nums[0]; // è®°å½å å«arr[i]çè¿ç»åæ°ç»çåçæå¤§å¼ int ans = nums[0]; // è®°å½å½åææåæ°ç»çåçæå¤§å¼ for (int i = 1; i < nums.length; i++) { max = Math.max(max + nums[i], nums[i]); ans = Math.max(max, ans); } return ans; } }