EASY UNDERSTAND(JAVA)


  • 0
    M
    public class Solution {
        public int maxSubArray(int[] nums) {
            int ret=nums[0];// the final result
            int tmp=0;//the tmp result
            for(int n:nums){
                tmp+=n;
                if(tmp>ret)ret=tmp;
                if(tmp<0)tmp=0;//tmp<0 means the maximum must start from the next index
            }
            return ret;
        }
    }
    

Log in to reply
 

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