Skip to content

Commit a9cde93

Browse files
authored
Create 523. Continuous Subarray Sum (#498)
2 parents 76908ed + 720c1e2 commit a9cde93

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

523. Continuous Subarray Sum

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public:
3+
bool checkSubarraySum(vector<int>& nums, int k) {
4+
5+
int n = nums.size();
6+
int sum = 0;
7+
map<int,int>ma;
8+
9+
ma[0] = -1;
10+
11+
for(int i=0;i<n;i++){
12+
sum+=nums[i];
13+
14+
if(ma.find(sum%k)!=ma.end()){
15+
16+
int foundAt = ma[sum%k];
17+
if(i-foundAt>=2){
18+
return true;
19+
}
20+
}
21+
else{
22+
ma[sum%k] = i;
23+
}
24+
}
25+
return false;
26+
}
27+
};

0 commit comments

Comments
 (0)