Java solution for Maximum Subarray


  • 0
    C
    public int maxSubArray(int[] nums) {
            int max=nums[0];
            for(int i=0;i<nums.length;i++){             
                if (max < nums[i]) {
                        max = nums[i];   
                    }
                    int sum=0;
                    for(int j=i;j<=nums.length-1;j++){  
                        sum=sum+nums[j];
                        if (max < sum) {
                        max = sum;   
                       }
                    }
                
            } 
            return max;
        }
    

Log in to reply
 

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