leetcode - 53.Maximum Subarray

    xiaoxiao2021-03-25  71

    Maximum Subarray

    Find the contiguous subarray within an array (containing at least one number) which has the largest sum.

    For example, given the array [-2,1,-3,4,-1,2,1,-5,4],

    the contiguous subarray [4,-1,2,1] has the largest sum = 6.

    Solution:

    public int maxSubArray(int[] nums) { int max = nums[0]; int temp = nums[0]; for (int i = 1; i < nums.length; i++) { temp = Math.max(nums[i], temp + nums[i]); max = Math.max(max, temp); } return max; }
    转载请注明原文地址: https://ju.6miu.com/read-40729.html

    最新回复(0)