You are given a circular array nums of positive and negative integers. If a number k at an index is positive, then move forward k steps. Conversely, if it's negative (-k), move backward k steps. Since the array is circular, you may assume that the last element's next element is the first element, and the first element's previous element is the last element.
Determine if there is a loop (or a cycle) in nums. A cycle must start and end at the same index and the cycle's length > 1. Furthermore, movements in a cycle must all follow a single direction. In other words, a cycle must not consist of both forward and backward movements.
Example 1:
Input: [2,-1,1,2,2]
Output: true
Explanation: There is a cycle, from index 0 -> 2 -> 3 -> 0. The cycle's length is 3.
Example 2:
Input: [-1,2]
Output: false
Explanation: The movement from index 1 -> 1 -> 1 ... is not a cycle, because the cycle's length is 1. By definition the cycle's length must be greater than 1.
Example 3:
Input: [-2,1,-1,-2,-2]
Output: false
Explanation: The movement from index 1 -> 2 -> 1 -> ... is not a cycle, because movement from index 1 -> 2 is a forward movement, but movement from index 2 -> 1 is a backward movement. All movements in a cycle must follow a single direction.
Note:
-1000 ≤ nums[i] ≤ 1000
nums[i] ≠ 0
1 ≤ nums.length ≤ 5000
Follow up:
Could you solve it in O(n) time complexity and O(1) extra space complexity?
代码
Approach 1: Slow/Fast Pointer
Just think it as finding a loop in Linkedlist, except that loops with only 1 element do not count. Use a slow and fast pointer, slow pointer moves 1 step a time while fast pointer moves 2 steps a time. If there is a loop (fast == slow), we return true, else if we meet element with different directions, then the search fail, we set all elements along the way to 0. Because 0 is fail for sure so when later search meet 0 we know the search will fail.
classSolution {publicbooleancircularArrayLoop(int[] nums) {int n =nums.length;for (int i =0; i < n; i++) {if (nums[i] ==0) continue;int slow = i;int fast =getIndex(nums, i);// 1. 同方向while (nums[fast] * nums[i] >0&& nums[getIndex(nums, fast)] * nums[i] >0) {if (slow == fast) {if (slow ==getIndex(nums, slow)) break; // 1-element loopreturntrue; } slow =getIndex(nums, slow); fast =getIndex(nums, getIndex(nums, fast)); }// 2. 不同方向,置为0 slow = i;int val = nums[i];while (nums[slow] * val >0) { // nums[i] * nums[i] at the first timeint next =getIndex(nums, slow); nums[slow] =0; slow = next; } }returnfalse; }publicintgetIndex(int[] nums,int i) {int n =nums.length;if (i + nums[i] >=0) {return (i + nums[i]) % n; } else {return n + (i + nums[i]) % n; } }}
Approach #2 DFS
classSolution {publicbooleancircularArrayLoop(int[] nums) {int[] color =newint[nums.length];for (int i =0; i <nums.length; i++) {if (color[i] ==0&&DFS(nums, color, i)) returntrue; }returnfalse; }privatebooleanDFS(int[] nums,int[] color,int start) {// return true if find cycleif (color[start] ==2) returnfalse; color[start] =1;int next = start + nums[start]; next = next %nums.length+nums.length; next %=nums.length;if (next == start || nums[next] * nums[start] <0) { color[start] =2;returnfalse; }if (color[next] ==1) { color[start] =2;returntrue; }if (DFS(nums, color, next)) returntrue; color[start] =2;returnfalse; }}