Simple n^2 DP solution


  • 0
    L
    public boolean checkSubarraySum(int[] nums, int k) {
            if(nums==null || nums.length<=1) {
                return false;
            }
            for(int i=0;i<nums.length;i++) {
                int sum=nums[i];
                for(int j=i+1;j<nums.length;j++) {
                    sum += nums[j];
                    if(sum==0 || (k!=0 && sum%k==0)) {
                        return true;
                    }
                }
            }
            return false;
       }
    

Log in to reply
 

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