g0501_0600.s0523_continuous_subarray_sum.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java21 Show documentation
Show all versions of leetcode-in-java21 Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
package g0501_0600.s0523_continuous_subarray_sum;
// #Medium #Array #Hash_Table #Math #Prefix_Sum
// #2022_07_28_Time_37_ms_(41.45%)_Space_109.7_MB_(5.07%)
import java.util.HashMap;
import java.util.Map;
public class Solution {
public boolean checkSubarraySum(int[] nums, int k) {
Map map = new HashMap<>();
int sum = 0;
map.put(0, -1);
for (int i = 0; i < nums.length; i++) {
sum += nums[i];
int remainder = sum % k;
if (map.containsKey(remainder)) {
if (map.get(remainder) + 1 < i) {
return true;
}
} else {
map.put(remainder, i);
}
}
return false;
}
}