-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContinuousSubarraySum.java
59 lines (53 loc) · 2.2 KB
/
ContinuousSubarraySum.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import java.util.HashMap;
import java.util.Map;
/**
* LeetCode
* 523. Continuous Subarray Sum
* https://leetcode.com/problems/continuous-subarray-sum/
* #Meduim #DP
*/
public class ContinuousSubarraySum {
public static void main(String[] args) {
ContinuousSubarraySum sol = new ContinuousSubarraySum();
System.out.println(sol.checkSubarraySum(new int[]{23, 2, 4, 6, 7}, 6)); // true
System.out.println(sol.checkSubarraySum(new int[]{23, 2, 6, 4, 7}, 6)); // true
System.out.println(sol.checkSubarraySum(new int[]{23, 23}, 6)); // false
System.out.println(sol.checkSubarraySum(new int[]{23, 2, 4, 6, 7}, -6)); // true
System.out.println(sol.checkSubarraySum(new int[]{23, 2, 4, 6, 7}, 0)); // false
System.out.println(sol.checkSubarraySum(new int[]{0, 0}, 0)); // true
System.out.println(sol.checkSubarraySum(new int[]{0, 0}, -1)); // true
System.out.println(sol.checkSubarraySum(new int[]{1, 0}, 2)); // false
System.out.println(sol.checkSubarraySum(new int[]{5, 0, 0}, 0)); // true
System.out.println(sol.checkSubarraySum(new int[]{1, 1}, 2)); // true
}
public boolean checkSubarraySum(int[] nums, int k) {
if (nums == null || nums.length <= 1) return false;
k = Math.abs(k);
int n = nums.length;
int prefixSum = 0;
boolean prevZero = false;
Map<Integer, Integer> modIndex = new HashMap<>();
for (int i = 0; i < n; i++) {
if (nums[i] == 0) {
if (prevZero) return true;
else prevZero = true;
} else {
prevZero = false;
}
prefixSum += nums[i];
if (k != 0) prefixSum %= k;
if (prefixSum == 0 && i > 0) {
return true;
}
if (modIndex.containsKey(prefixSum)) {
int idx = modIndex.get(prefixSum);
// System.out.println(Map.of("i", i, "idx", idx));
if (i - idx >= 2) return true;
} else {
modIndex.put(prefixSum, i);
}
// System.out.println(Map.of("modIndex", modIndex, "prefixSum", prefixSum));
}
return false;
}
}