Class Solution
java.lang.Object
g0401_0500.s0457_circular_array_loop.Solution
457 - Circular Array Loop.<p>Medium</p>
<p>You are playing a game involving a <strong>circular</strong> array of non-zero integers <code>nums</code>. Each <code>nums[i]</code> denotes the number of indices forward/backward you must move if you are located at index <code>i</code>:</p>
<ul>
<li>If <code>nums[i]</code> is positive, move <code>nums[i]</code> steps <strong>forward</strong> , and</li>
<li>If <code>nums[i]</code> is negative, move <code>nums[i]</code> steps <strong>backward</strong>.</li>
</ul>
<p>Since the array is <strong>circular</strong> , 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.</p>
<p>A <strong>cycle</strong> in the array consists of a sequence of indices <code>seq</code> of length <code>k</code> where:</p>
<ul>
<li>Following the movement rules above results in the repeating index sequence <code>seq[0] -> seq[1] -> ... -> seq[k - 1] -> seq[0] -> ...</code></li>
<li>Every <code>nums[seq[j]]</code> is either <strong>all positive</strong> or <strong>all negative</strong>.</li>
<li><code>k > 1</code></li>
</ul>
<p>Return <code>true</code> <em>if there is a <strong>cycle</strong> in</em> <code>nums</code><em>, or</em> <code>false</code> <em>otherwise</em>.</p>
<p><strong>Example 1:</strong></p>
<p><strong>Input:</strong> nums = [2,-1,1,2,2]</p>
<p><strong>Output:</strong> true</p>
<p><strong>Explanation:</strong> There is a cycle from index 0 -> 2 -> 3 -> 0 -> … The cycle’s length is 3.</p>
<p><strong>Example 2:</strong></p>
<p><strong>Input:</strong> nums = [-1,2]</p>
<p><strong>Output:</strong> false</p>
<p><strong>Explanation:</strong> 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.</p>
<p><strong>Example 3:</strong></p>
<p><strong>Input:</strong> nums = [-2,1,-1,-2,-2]</p>
<p><strong>Output:</strong> false</p>
<p><strong>Explanation:</strong> 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.</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 5000</code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
<li><code>nums[i] != 0</code></li>
</ul>
<p><strong>Follow up:</strong> Could you solve it in <code>O(n)</code> time complexity and <code>O(1)</code> extra space complexity?</p>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
circularArrayLoop
public boolean circularArrayLoop(int[] arr)
-