-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContinuous Subarray Sum.java
More file actions
37 lines (35 loc) · 908 Bytes
/
Copy pathContinuous Subarray Sum.java
File metadata and controls
37 lines (35 loc) · 908 Bytes
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
class Solution {
public boolean checkSubarraySum(int[] nums, int k) {
Map<Integer, Integer> map = new HashMap<>();
k = Math.abs(k);
int sum = 0;
int flag = 0;
map.put(0,-1);
for(int i = 0; i<nums.length; i++)
{
if(nums[i]!= 0)
flag = 1;
}
if(k == 0 && flag == 1)
return false;
if(nums.length >= 2)
{
for(int i = 0; i<nums.length; i++)
{
sum+=nums[i];
if(k!=0)
sum%=k;
if(map.containsKey(sum))
{
if(i - map.get(sum) > 1)
return true;
}
else
map.put(sum, i);
}
return false;
}
else
return false;
}
}