-
Notifications
You must be signed in to change notification settings - Fork 0
/
523ContinuousSubarraySum.java
46 lines (43 loc) · 1.24 KB
/
523ContinuousSubarraySum.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
public class Solution {
public boolean checkSubarraySum(int[] nums, int k) {
if(nums == null || nums.length == 0){
return false;
}
for(int i=0; i<nums.length ; i++){
int sum = nums[i];
for(int j= i+1 ; j<nums.length;j++){
sum = sum+ nums[j];
if(k!= 0 && isMultiple(sum,k)){
return true;
}else{
if(k == 0){
if(sum == 0){
return true;
}
}
}
}
}
return false;
}
private boolean isMultiple(int sum, int k){
return sum%k==0;
}
public boolean checkSubarraySum2(int[] nums, int k){
Map<Integer, Integer> map = new HashMap<Integer,Integer>();
map.put(0,-1);
int runningSum = 0;
for(int i=0;i<nums.length;i++){
runningSum += nums[i];
if(k!= 0){
runningSum %=k;
}
Integer pre = map.get(runningSum);
if( pre != null && i-pre>1){
return true;
}
map.put(runningSum,i);
}
return false;
}
}