Java O(n) sample idea


  • 0
    F
    class Solution {
        public int maxSubArray(int[] nums) {
            if (nums == null || nums.length == 0) return 0;
            int sum = 0;
            int result = nums[0];
            for (int i = 0; i < nums.length; i++) {
                if (sum < 0) sum = nums[i];
                else sum += nums[i];
                result = Math.max(result, sum);
            }
            return result;
        }
    }

Log in to reply
 

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