Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Runtime: 2 ms, faster than 99.90% of Java online submissions for Continuous Subarray Sum.
- // Memory Usage: 39.9 MB, less than 88.24% of Java online submissions for Continuous Subarray Sum.
- class Solution {
- public boolean checkSubarraySum(int[] nums, int k) {
- int len = nums.length;
- if (len == 0) return false;
- int cs = 0;
- Map<Integer, Integer> occ = new HashMap<Integer, Integer>();
- occ.put(0, -1);
- for (int i = 0; i < len; i++) {
- cs += nums[i];
- if (cs >= k && k != 0) cs %= k;
- Integer prev = occ.get(cs);
- if (prev == null) occ.put(cs, i);
- else if (i - prev > 1) return true;
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement