ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

457. Circular Array Loop

2020-10-02 13:01:53  阅读:259  来源: 互联网

标签:nums color next start length Array Circular Loop cycle


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?

class Solution {
    public boolean circularArrayLoop(int[] nums) {
        int[] color = new int[nums.length];
        for(int i = 0 ; i < nums.length ; i++) {
            if(color[i] == 0 && DFS(nums, color, i)) return true;
        }
        return false;
    }
    private boolean DFS(int[] nums, int[] color, int start) {
        //return true if find cycle
        if(color[start] == 2) return false;//means cannot start a loop from here which already been proved.
        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;//bad end
            return false;
        }
        if(color[next] == 1) {
            //color[start] = 2;
            return true;
        }
        if(DFS(nums, color, next)) return true;
        color[start] = 2;
        return false;
    }
}

color = 0 : 这个点还没来过,奥里给

color = 1:这个点来过了

color = 2:这个点来过了,并且无法成为loop

标签:nums,color,next,start,length,Array,Circular,Loop,cycle
来源: https://www.cnblogs.com/wentiliangkaihua/p/13760994.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有