题目:
题解:
int next(int* nums, int numsSize, int cur) {
return ((cur + nums[cur]) % numsSize + numsSize) % numsSize; // 保证返回值在 [0,n) 中
}
bool circularArrayLoop(int* nums, int numsSize) {
for (int i = 0; i < numsSize; i++) {
if (!nums[i]) {
continue;
}
int slow = i, fast = next(nums, numsSize, i);
// 判断非零且方向相同
while (nums[slow] * nums[fast] > 0 && nums[slow] * nums[next(nums, numsSize, fast)] > 0) {
if (slow == fast) {
if (slow != next(nums, numsSize, slow)) {
return true;
} else {
break;
}
}
slow = next(nums, numsSize, slow);
fast = next(nums, numsSize, next(nums, numsSize, fast));
}
int add = i;
while (nums[add] * nums[next(nums, numsSize, add)] > 0) {
int tmp = add;
add = next(nums, numsSize, add);
nums[tmp] = 0;
}
}
return false;
}