-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathCircular Array Loop.java
36 lines (35 loc) · 1.16 KB
/
Circular Array Loop.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
36
// Runtime: 16 ms (Top 46.1%) | Memory: 40.11 MB (Top 41.3%)
class Solution {
public boolean circularArrayLoop(int[] nums) {
for (int i=0; i<nums.length; i++) {
boolean isForward = nums[i] > 0;
int slow = i;
int fast = i;
do {
slow = findNextIndex(nums, isForward, slow);
fast = findNextIndex(nums, isForward, fast);
if (fast != -1) {
fast = findNextIndex(nums, isForward, fast);
}
} while (slow != -1 && fast != -1 && slow != fast);
if (slow != -1 && slow == fast) {
return true;
}
}
return false;
}
private int findNextIndex(int[] arr, boolean isForward, int currentIndex) {
boolean direction = arr[currentIndex] >= 0;
if (isForward != direction) {
return -1;
}
int nextIndex = (currentIndex + arr[currentIndex]) % arr.length;
if (nextIndex < 0) {
nextIndex += arr.length;
}
if (nextIndex == currentIndex) {
nextIndex = -1;
}
return nextIndex;
}
}