-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path523.cpp
More file actions
39 lines (38 loc) · 959 Bytes
/
523.cpp
File metadata and controls
39 lines (38 loc) · 959 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
38
39
class Solution {
public:
bool checkSubarraySum(vector<int>& nums, int k) {
int sum = 0;
unordered_map<int,int> ump;
ump[0]=-1;
for(int i=0; i<nums.size();i++){
sum += nums[i];
if(k!=0)
sum = sum%k;
if(ump.count(sum)){
if(i-ump[sum]>1)
return true;
}
else
ump[sum] = i;
}
return false;
}
};
class Solution {
public:
bool checkSubarraySum(vector<int>& nums, int k) {
int n = nums.size();
int dp[n+1];
memset(dp,0,sizeof(dp));
for(int i=1; i<=n; i++)
dp[i] = dp[i-1] + nums[i-1];
for(int i=1; i<=n; i++){
for(int j=i+1; j<=n; j++){
int sum = dp[j]-dp[i-1];
if(sum==k || (k!=0 && sum%k==0))
return true;
}
}
return false;
}
};