My clean Java solution


  • 0
    L

    public class Solution {
    public int maxSubArray(int[] nums) {
    int[] res = new int[nums.length];
    res[0] = nums[0];
    for(int i = 1; i < res.length; i++){
    if(res[i - 1] > 0){
    res[i] = res[i - 1] + nums[i];
    }
    else{
    res[i] = nums[i];
    }
    }
    int result = Integer.MIN_VALUE;
    for(int i = 0; i < res.length; i++){
    if(res[i] > result){
    result = res[i];
    }
    }
    return result;
    }
    }


Log in to reply
 

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