码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode 457. Circular Array Loop

时间:2019-08-28 13:10:59      阅读:81      评论:0      收藏:0      [点我收藏+]

标签:point   nbsp   and   val   tle   nsis   ||   for   link   

原题链接在这里:https://leetcode.com/problems/circular-array-loop/

题目:

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:

  1. -1000 ≤ nums[i] ≤ 1000
  2. nums[i] ≠ 0
  3. 1 ≤ nums.length ≤ 5000

 

Follow up:

Could you solve it in O(n) time complexity and O(1) extra space complexity?

题解:

Use walker and runner pointers to check if there is a loop.

Every time, pointer move as i + nums[i]. If it is positive, return (i + nums[i])/nums.length. If it is negative, return (i+nums[i])/nums.length + nums.length.

In order to maintain the same direction, make sure each shifted index are all positive or all negative.

When walker and runner meets, then there is a loop. 

But in case to avoid single element in the loop, check if next move is still here.

Last, if there is no loop for current routine, then mark all nums on this routine as 0, then there would not be duplicate calcuation on these numbers.

Time Complexity: O(n). n = nums.length.

Space: O(1).

AC Java:

 1 class Solution {
 2     public boolean circularArrayLoop(int[] nums) {
 3         if(nums == null || nums.length < 2){
 4             return false;
 5         }
 6         
 7         for(int i = 0; i<nums.length; i++){
 8             if(nums[i] == 0){
 9                 continue;
10             }
11             
12             int walker = i;
13             int runner = i;
14             while(nums[i]*nums[shift(runner, nums)]>0 && nums[i]*nums[shift(shift(runner, nums), nums)]>0){
15                 walker = shift(walker, nums);
16                 runner = shift(shift(runner, nums), nums);
17                 // If there is a loop, walker and runner will meet
18                 if(walker == runner){
19                     // If there is only one element in loop, break while
20                     if(walker == shift(walker, nums)){
21                         break;
22                     }
23                     
24                     return true;
25                 }
26             }
27             
28             // When there is no loop with current routine,
29             // Mark value as 0, then there would not be duplicate calculation
30             int ind = i;
31             int val = nums[ind];
32             while(val*nums[ind]>0){
33                 int nextInd = shift(ind, nums);
34                 nums[ind] = 0;
35                 ind = nextInd;
36             }
37         }
38         
39         return false;
40     }
41     
42     private int shift(int i, int [] nums){
43         int n = nums.length;
44         return i + nums[i] >= 0 ? (i + nums[i]) % n : (i + nums[i]) % n + n;
45     }
46 }

类似Linked List Cycle.

LeetCode 457. Circular Array Loop

标签:point   nbsp   and   val   tle   nsis   ||   for   link   

原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/11423136.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!