O(n) Java Solution , esay to understand


  • 0
    G
    public static int maxSubArray(int[] nums) {
        int res = nums[0];
        int sum = nums[0];
        for(int i = 1;i<nums.length; i++){
        	if(nums[i]<sum||sum>=0){
        		sum += nums[i];
        	}else{
        		sum = nums[i];
        	}
        	res = Math.max(res, sum);
        }
        return res;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.