public class Solution {
public boolean checkSubarraySum(int[] nums, int k) {
if (nums == null  nums.length == 0) return false;
int[] preSum = new int[nums.length+1];
for (int i = 1; i <= nums.length; i++) {
preSum[i] = preSum[i1] + nums[i1];
}
for (int i = 0; i < nums.length; i++) {
for (int j = i+2; j <= nums.length; j++) {
if (k == 0) {
if (preSum[j]  preSum[i] == 0) {
return true;
}
} else if ((preSum[j]  preSum[i]) % k == 0) {
return true;
}
}
}
return false;
}
}
Not smart solution, but easy to understand


@yl right, that's why I said it's not a smart solution. Still need more pratice to come up with a better solution :)

@benny201 Same here. I think around half of the 2D array is wasted anyway. Instead, I used a HashMap to track the progressive sum starting at each index (and also check if it is divisible by the k).