01.18 Amazon 电面 Phone Interview 第一轮

原帖地址:一亩三分地

结果还不知道 刚面完~ 直接上题
Coding Question:
Given an array with relative indexes, determine if there is a single complete cycle or not.
For example:
x = [3, 1, 2, -1] – false
y = [-5, 1, 4, 6, 4, 2, -1, 5] – true

Solution:
static boolean isCycle(int []arr){
if(arr.length==0)
{ return false;
}
boolean []visited= new boolean[arr.length];
int current=0;
int count=0;
while(count=0)
{ current=(current+arr[current])%arr.length;
}
if(current<0) { current=current+arr.length; } visited[current]=true; count++; } for(int i=0;i