其他分享
首页 > 其他分享> > 力扣 457. 环形数组是否存在循环(中等)

力扣 457. 环形数组是否存在循环(中等)

作者:互联网

题目

存在一个不含 0 的 环形 数组 nums ,每个 nums[i] 都表示位于下标 i 的角色应该向前或向后移动的下标个数:

因为数组是环形的,所以可以假设从最后一个元素向前移动一步会到达第一个元素,而第一个元素向后移动一步会到达最后一个元素。
数组中的 循环 由长度为 k 的下标序列 seq :

如果 nums 中存在循环,返回 true ;否则,返回 false 。
示例 1:

输入:nums = [2,-1,1,2,2]
输出:true
解释:存在循环,按下标 0 -> 2 -> 3 -> 0 。循环长度为 3 。

示例 2:

输入:nums = [-1,2]
输出:false
解释:按下标 1 -> 1 -> 1 ... 的运动无法构成循环,因为循环的长度为 1 。根据定义,循环的长度必须大于 1 。

示例 3:

输入:nums = [-2,1,-1,-2,-2]
输出:false
解释:按下标 1 -> 2 -> 1 -> ... 的运动无法构成循环,因为 nums[1] 是正数,而 nums[2] 是负数。
所有 nums[seq[j]] 应当不是全正就是全负。

提示:

题解

方法一:哈希表

class Solution {
    Set<Integer> check = new HashSet<>();   // 保存独自循环的结点下标
    public boolean circularArrayLoop(int[] nums) {
        int n = nums.length;
        
        for(int i = 0; i < n; i++){
            if(Math.abs(nums[i]) == n)
                check.add(i);
        }

        for(int i = 0; i < n; i++){
            Set<Integer> visited = new HashSet<>();
            boolean ans = isCircular(nums, i, visited);
            if(ans == true) 
                return true;
        }
        return false;
    }

    public boolean isCircular(int[] nums, int start, Set<Integer> visited){
        if(check.contains(start)) return false;
        if(visited.contains(start)){   // 说明有了重复点
            if(visited.size() == 1) return false;
            int count = 0;
            for(int x : visited){
                if(nums[x] > 0)
                    count++;
            }
            if(count == 0 || count == visited.size())  // 全负或者全正
                return true;
            else 
                return false;
        }

        visited.add(start);
        int next = start + nums[start];    
        // 修改next,保持0 <= next < nums.length 
        while(next < 0){
            next += nums.length;
        }
        while(next >= nums.length){
            next -= nums.length;
        }
        
        return isCircular(nums, next, visited);
    }   
}

方法二:快慢指针

class Solution {
    public boolean circularArrayLoop(int[] nums) {
        int n = nums.length;
        for (int i = 0; i < n; i++) {
            if (nums[i] == 0) {
                continue;
            }
            int slow = i, fast = next(nums, i);
            // 判断非零且方向相同
            while (nums[slow] * nums[fast] > 0 && nums[slow] * nums[next(nums, fast)] > 0) {
                if (slow == fast) {
                    if (slow != next(nums, slow)) {
                        return true;
                    } else {
                        break;
                    }
                }
                slow = next(nums, slow);
                fast = next(nums, next(nums, fast));
            }
            int add = i;
            while (nums[add] * nums[next(nums, add)] > 0) {
                int tmp = add;
                add = next(nums, add);
                nums[tmp] = 0;
            }
        }
        return false;
    }

    public int next(int[] nums, int cur) {
        int n = nums.length;
        return ((cur + nums[cur]) % n + n) % n; // 保证返回值在 [0,n) 中
    }
}

标签:slow,return,nums,int,环形,next,力扣,visited,457
来源: https://blog.csdn.net/weixin_41317766/article/details/119490621