-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0457.cpp
More file actions
38 lines (31 loc) · 905 Bytes
/
Copy path0457.cpp
File metadata and controls
38 lines (31 loc) · 905 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
class Solution {
public:
bool circularArrayLoop(vector<int> &nums) const {
const int n = size(nums);
static int seen[5001];
const auto next = [&](int k) {
const int res = (k + nums[k] % n + n) % n;
seen[res] = true;
return res;
};
memset(seen, 0x00, sizeof(seen));
for (auto &num : nums)
num %= n;
for (int i = 0; i < n; i++) {
if (seen[i]) continue;
int t = i, h = i;
do {
t = next(t);
h = next(next(h));
} while (t != h);
const bool dir = nums[t] > 0;
do {
t = next(t);
if ((nums[t] > 0) != dir) goto next;
} while (t != h);
if ((t + nums[t] + n) % n != t) return true;
next:;
}
return false;
}
};