Skip to content

Instantly share code, notes, and snippets.

@wushbin
Created April 1, 2020 04:46
Show Gist options
  • Select an option

  • Save wushbin/7e72c41613719fd0bfb7c73990c2080d to your computer and use it in GitHub Desktop.

Select an option

Save wushbin/7e72c41613719fd0bfb7c73990c2080d to your computer and use it in GitHub Desktop.
class Solution {
public boolean checkSubarraySum(int[] nums, int k) {
Map<Integer, Integer> map = new HashMap<>();
int sum = 0;
map.put(0, -1);
for (int i = 0; i < nums.length; i++) {
sum += nums[i];
if (k != 0) {
sum %= k;
}
if (map.containsKey(sum)) {
if (map.get(sum) < i - 1) return true;
} else {
map.put(sum, i);
}
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment