-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path523.cpp
More file actions
26 lines (25 loc) · 694 Bytes
/
523.cpp
File metadata and controls
26 lines (25 loc) · 694 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
class Solution {
public:
bool checkSubarraySum(vector<int>& nums, int k) {
int n = nums.size();
if (n < 2) return false;
for (int i = 0; i < n; ++i) {
nums[i] %= k;
}
unordered_set<int> st;
int prevSum = 0;
int currentSum = 0;
for (int i = 0; i < n; ++i) {
if (i == 0) {
currentSum += nums[i];
}
if (i >= 1) {
currentSum += nums[i];
st.insert(prevSum % k);
if (st.find(currentSum % k) != st.end()) return true;
prevSum += nums[i - 1];
}
}
return false;
}
};