[LeetCode] 457. Circular Array Loop
作者:互联网
You are playing a game involving a circular array of non-zero integers nums
. Each nums[i]
denotes the number of indices forward/backward you must move if you are located at index i
:
- If
nums[i]
is positive, movenums[i]
steps forward, and - If
nums[i]
is negative, movenums[i]
steps backward.
Since the array is circular, you may assume that moving forward from the last element puts you on the first element, and moving backwards from the first element puts you on the last element.
A cycle in the array consists of a sequence of indices seq
of length k
where:
- Following the movement rules above results in the repeating index sequence
seq[0] -> seq[1] -> ... -> seq[k - 1] -> seq[0] -> ...
- Every
nums[seq[j]]
is either all positive or all negative. k > 1
Return true
if there is a cycle in nums
, or false
otherwise.
Example 1:
Input: nums = [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: nums = [-1,2] Output: false Explanation: The sequence from index 1 -> 1 -> 1 -> ... is not a cycle because the sequence's length is 1. By definition the sequence's length must be strictly greater than 1 to be a cycle.
Example 3:
Input: nums = [-2,1,-1,-2,-2] Output: false Explanation: The sequence from index 1 -> 2 -> 1 -> ... is not a cycle because nums[1] is positive, but nums[2] is negative. Every nums[seq[j]] must be either all positive or all negative.
Constraints:
1 <= nums.length <= 5000
-1000 <= nums[i] <= 1000
nums[i] != 0
Follow up: Could you solve it in O(n)
time complexity and O(1)
extra space complexity?
环形数组是否存在循环。
存在一个不含 0 的 环形 数组 nums ,每个 nums[i] 都表示位于下标 i 的角色应该向前或向后移动的下标个数:
如果 nums[i] 是正数,向前 移动 nums[i] 步
如果 nums[i] 是负数,向后 移动 nums[i] 步
因为数组是 环形 的,所以可以假设从最后一个元素向前移动一步会到达第一个元素,而第一个元素向后移动一步会到达最后一个元素。数组中的 循环 由长度为 k 的下标序列 seq :
遵循上述移动规则将导致重复下标序列 seq[0] -> seq[1] -> ... -> seq[k - 1] -> seq[0] -> ...
所有 nums[seq[j]] 应当不是 全正 就是 全负
k > 1
如果 nums 中存在循环,返回 true ;否则,返回 false 。来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/circular-array-loop
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
这道题的题目描述写的很不完整。判断是否有满足题意的循环需要有以下几个条件
- 循环的长度 > 1
- 参与循环的下标 index 的正负性需要一样,不是 全正 就是 全负
- 循环开始的 index 可以是任意位置
尤其是这最后一个条件,题目没有写出,导致我debug了很久都不明白为什么。
我这里给出一个快慢指针的思路。因为 nums[i] 表示的是指针在数组中的移动,nums[i] 的正负性表示移动的方向,所以我们可以用快慢指针的方式,从每一个位置开始做一遍快慢指针,如果有发现满足题意的循环则返回 true。由于快慢指针的开始位置可以是任意的,所以我们必须要逐一尝试,这会导致时间复杂度最坏到达 O(n^2)。题目的 followup 以后有机会再补充吧。
时间O(n^2)
空间O(1)
Java实现
1 class Solution { 2 int[] nums; 3 int n; 4 5 public boolean circularArrayLoop(int[] nums) { 6 this.n = nums.length; 7 this.nums = nums; 8 9 // 按照题意,起点seq[0]可能不从下标0开始 10 for(int i = 0; i < n; ++i){ 11 int slow = i, fast = next(i); 12 // 检查同向 13 while(nums[fast] * nums[slow] > 0 && nums[next(fast)] * nums[slow] > 0){ 14 if(fast == slow){ 15 if(slow == next(slow)) break; // 存在长度k为1的同向环 16 else return true; // 否则满足要求 17 } 18 // 指针移动 19 fast = next(next(fast)); 20 slow = next(slow); 21 } 22 } 23 24 return false; 25 } 26 27 private int next(int i){ 28 // 注意java下标不能像python有负数,-1需要处理成n-1 29 return ((i + nums[i]) % n + n) % n; 30 } 31 }
标签:...,slow,seq,nums,index,next,Array,LeetCode,Circular 来源: https://www.cnblogs.com/cnoodle/p/15114999.html