457. Circular Array Loop Medium

@problem@discussion
#Array#Hash Table#Two Pointers



1/**
2 * [457] Circular Array Loop
3 *
4 * 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:
5 * 
6 * 	If nums[i] is positive, move nums[i] steps forward, and
7 * 	If nums[i] is negative, move nums[i] steps backward.
8 * 
9 * 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.
10 * A cycle in the array consists of a sequence of indices seq of length k where:
11 * 
12 * 	Following the movement rules above results in the repeating index sequence seq[0] -> seq[1] -> ... -> seq[k - 1] -> seq[0] -> ...
13 * 	Every nums[seq[j]] is either all positive or all negative.
14 * 	k > 1
15 * 
16 * Return true if there is a cycle in nums, or false otherwise.
17 *  
18 * <strong class="example">Example 1:
19 * <img alt="" src="https://assets.leetcode.com/uploads/2022/09/01/img1.jpg" style="width: 402px; height: 289px;" />
20 * Input: nums = [2,-1,1,2,2]
21 * Output: true
22 * Explanation: The graph shows how the indices are connected. White nodes are jumping forward, while red is jumping backward.
23 * We can see the cycle 0 --> 2 --> 3 --> 0 --> ..., and all of its nodes are white (jumping in the same direction).
24 * 
25 * <strong class="example">Example 2:
26 * <img alt="" src="https://assets.leetcode.com/uploads/2022/09/01/img2.jpg" style="width: 402px; height: 390px;" />
27 * Input: nums = [-1,-2,-3,-4,-5,6]
28 * Output: false
29 * Explanation: The graph shows how the indices are connected. White nodes are jumping forward, while red is jumping backward.
30 * The only cycle is of size 1, so we return false.
31 * 
32 * <strong class="example">Example 3:
33 * <img alt="" src="https://assets.leetcode.com/uploads/2022/09/01/img3.jpg" style="width: 497px; height: 242px;" />
34 * Input: nums = [1,-1,5,1,4]
35 * Output: true
36 * Explanation: The graph shows how the indices are connected. White nodes are jumping forward, while red is jumping backward.
37 * We can see the cycle 0 --> 1 --> 0 --> ..., and while it is of size > 1, it has a node jumping forward and a node jumping backward, so it is not a cycle.
38 * We can see the cycle 3 --> 4 --> 3 --> ..., and all of its nodes are white (jumping in the same direction).
39 * 
40 *  
41 * Constraints:
42 * 
43 * 	1 <= nums.length <= 5000
44 * 	-1000 <= nums[i] <= 1000
45 * 	nums[i] != 0
46 * 
47 *  
48 * Follow up: Could you solve it in O(n) time complexity and O(1) extra space complexity?
49 * 
50 */
51pub struct Solution {}
52
53// problem: https://leetcode.com/problems/circular-array-loop/
54// discuss: https://leetcode.com/problems/circular-array-loop/discuss/?currentPage=1&orderBy=most_votes&query=
55
56// submission codes start here
57
58impl Solution {
59    pub fn circular_array_loop(nums: Vec<i32>) -> bool {
60        false
61    }
62}
63
64// submission codes end
65
66#[cfg(test)]
67mod tests {
68    use super::*;
69
70    #[test]
71    fn test_457() {
72    }
73}
74


Back
© 2025 bowen.ge All Rights Reserved.