-
Notifications
You must be signed in to change notification settings - Fork 0
/
CircularArrayLoop.java
44 lines (37 loc) · 1.16 KB
/
CircularArrayLoop.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
37
38
39
40
41
42
43
44
package arrays;
//https://leetcode.com/problems/circular-array-loop/
import utils.LeetCode;
import utils.Level;
@LeetCode(no =474 ,
level = Level.MEDIUM ,
title = "Circular Array Loop",
url="https://leetcode.com/problems/circular-array-loop/")
public class CircularArrayLoop {
public boolean circularArrayLoop(int[] nums) {
for (int i = 0; i < nums.length; i++) {
int slow=i;
int fast=i;
boolean forward=nums[i]>=0;
do{
slow=move(slow,forward,nums);
fast=move(fast,forward,nums);
if(fast!=-1)
fast=move(fast,forward,nums);
}while(slow!=fast && slow!=-1 && fast!=-1);
if(slow!=-1 && slow==fast)
return true;
}
return false;
}
int move(int pos,boolean direction, int[] nums){
boolean currentDirection=nums[pos]>=0;
if(currentDirection!=direction)
return -1;
int newPos= (pos+nums[pos])%nums.length;
if(newPos<0)
newPos+=nums.length;
if(newPos==pos)
return -1;
return newPos;
}
}