My Java solution with O(n)


  • 0
    M
    public class Solution {
        public int maxSubArray(int[] nums) {
            if(nums==null||nums.length==0)
              return 0;
            int sum=nums[0];
            int temp=0;
            for(int i=0;i<nums.length;i++){
                temp+=nums[i];
                if(temp>sum)
                 sum=temp;
                if(temp<=0){
                    temp=0;
                }
            }
            return sum;
        }
    }

Log in to reply
 

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