Share my AC solution


  • 2
    S
    public boolean checkSubarraySum(int[] nums, int k) {
    	int n = nums.length;
    	int[] sums = new int[n+1];
    	for (int i = 1; i <= n; i++) {
    		sums[i] = sums[i-1] + nums[i-1];
    		for (int j = 0; j < i-1; j++) {
    			int dif = sums[i]-sums[j];
    			if (dif == 0 && k == 0) return true;
    			else if (k == 0) continue;
    			else if ((sums[i]-sums[j]) % 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.