-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
31 lines (29 loc) · 880 Bytes
/
index.js
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
/**
* @param {number[]} nums
* @return {boolean}
*/
const circularArrayLoop = function (nums) {
for (let i = 0; i < nums.length; ++i) {
let idx = i, idx2 = i;
let direction = nums[idx] > 0 ? 1 : -1;
while (true) {
idx = getNext(nums, idx);
let t2 = getNext(nums, idx2);
idx2 = getNext(nums, t2);
if (direction === 1 && (nums[t2] < 0 || nums[idx2] < 0) ||
direction === -1 && (nums[t2] > 0 || nums[idx2] > 0) ||
nums[idx2] % nums.length === 0) {
break;
}
if (idx === idx2) {
return true;
}
}
}
return false;
};
function getNext(nums, idx) {
let target = (idx + nums[idx]) % nums.length;
return target >= 0 ? target : nums.length + target;
}
module.exports = circularArrayLoop;