Definition at line 5 of file main.cpp.
◆ circularArrayLoop()
| bool Solution::circularArrayLoop |
( |
vector< int > & |
nums | ) |
|
|
inline |
Definition at line 26 of file main.cpp.
27 {
28 set<int> visited;
29 int size = (int)nums.size();
30
31
32 for (int i = 0; i < size; ++i)
33 {
34
35 if (visited.find(i) != visited.end())
36 continue;
37
38 int slow = i, fast = i;
39 int direction = 1;
40 if (nums[i] < 0)
41 direction = -1;
42
43 while (1)
44 {
45 visited.insert(slow);
46 visited.insert(fast);
49 if (slow == -1 || fast == -1)
50 break;
51
53 if (fast == -1)
54 break;
55
56 if (slow == fast)
57 return true;
58 }
59 }
60 return false;
61 }
int get_next_index(int index, int direction, vector< int > &nums)
References get_next_index().
Referenced by main().
◆ get_next_index()
| int Solution::get_next_index |
( |
int |
index, |
|
|
int |
direction, |
|
|
vector< int > & |
nums |
|
) |
| |
|
inlineprivate |
Definition at line 8 of file main.cpp.
9 {
10 int n = (int)nums.size();
11 int next_index = (index + nums[index]) % n;
12 while (next_index < 0)
13 next_index += n;
14 int next_direction = 1;
15 if (nums[next_index] < 0)
16 next_direction = -1;
17
18
19 if (direction != next_direction || index == next_index)
20 return -1;
21
22 return next_index;
23 }
Referenced by circularArrayLoop().
The documentation for this class was generated from the following file: