-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.java
35 lines (33 loc) · 941 Bytes
/
Solution.java
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
class Solution {
private int n;
private int[] nums;
public boolean circularArrayLoop(int[] nums) {
n = nums.length;
this.nums = nums;
for (int i = 0; i < n; ++i) {
if (nums[i] == 0) {
continue;
}
int slow = i, fast = next(i);
while (nums[slow] * nums[fast] > 0 && nums[slow] * nums[next(fast)] > 0) {
if (slow == fast) {
if (slow != next(slow)) {
return true;
}
break;
}
slow = next(slow);
fast = next(next(fast));
}
int j = i;
while (nums[j] * nums[next(j)] > 0) {
nums[j] = 0;
j = next(j);
}
}
return false;
}
private int next(int i) {
return (i + nums[i] % n + n) % n;
}
}