245、环形数组循环

题目描述:
给定一个含有正整数和负整数的环形数组 nums。 如果某个索引中的数 k 为正数,则向前移动 k 个索引。相反,如果是负数 (-k),则向后移动 k 个索引。因为数组是环形的,所以可以假设最后一个元素的下一个元素是第一个元素,而第一个元素的前一个元素是最后一个元素。

确定 nums 中是否存在循环(或周期)。循环必须在相同的索引处开始和结束并且循环长度 > 1。此外,一个循环中的所有运动都必须沿着同一方向进行。换句话说,一个循环中不能同时包括向前的运动和向后的运动。

示例 1:

输入:[2,-1,1,2,2]
输出:true
解释:存在循环,按索引 0 -> 2 -> 3 -> 0 。循环长度为 3 。
示例 2:

输入:[-1,2]
输出:false
解释:按索引 1 -> 1 -> 1 … 的运动无法构成循环,因为循环的长度为 1 。根据定义,循环的长度必须大于 1 。
示例 3:

输入:[-2,1,-1,-2,-2]
输出:false
解释:按索引 1 -> 2 -> 1 -> … 的运动无法构成循环,因为按索引 1 -> 2 的运动是向前的运动,而按索引 2 -> 1 的运动是向后的运动。一个循环中的所有运动都必须沿着同一方向进行。

提示:

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

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/circular-array-loop
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

没有什么技术含量,但是最起码AC了。

emm,比较冗余的代码,通过循环来找

class Solution {
    public boolean circularArrayLoop(int[] nums) {
        int len = nums.length;
//        判断向右走的是否可以形成
        for (int i = 0; i < nums.length; i++) {
            int num = nums[i];
            int index = i;
            Set<Integer>set = new HashSet<>();
            int tis = len;
            while (num > 0 && tis-- >= 0){
                int tem = (num + index)%len;
                num = nums[tem];
                index = tem;
                set.add(index);
            }
            if(set.size() > 1 && set.contains(i)){
                return true;
            }
        }
//        取反
        for (int i = 0; i < nums.length; i++) {
            if(nums[i] > 0){
                nums[i] = 0 - nums[i];
            }else{
                nums[i] = (len - Math.abs(nums[i]) % len) % len;
            }
        }
        for (int i = 0; i < nums.length; i++) {
            int num = nums[i];
            int index = i;
            Set<Integer>set = new HashSet<>();
            int tis = len;
            while (num > 0 && tis-- >= 0){
                int tem = (num + index)%len;
                num = nums[tem];
                index = tem;
                set.add(index);
            }
            if(set.size() > 1 && set.contains(i)){
                return true;
            }
        }
        return false;

    }
}

看看别人实现的代码:
思路转成DFS
在这里插入图片描述

class Solution {
    public boolean circularArrayLoop(int[] a) {
        int len = a.length;
        if (len == 0) {
            return false;
        }
        int r = 1000;
        for (int i = 0; i < len; i ++) {
            if (a[i] > 1000) {
                continue;
            }
            boolean isReverse = a[i] < 0;
            int length = 1;
            int nexti = (i + a[i] % len + len) % len;
            a[i] = ++r;
            while (a[nexti] <= 1000 && a[nexti] != r && isReverse == (a[nexti] < 0)) {
                int temp = nexti;
                nexti = (nexti + a[nexti] % len + len) % len;
                if (temp == nexti) {
                    length = 1;
                    break;
                }
                a[temp] = r;
                length ++;
            }
            if (a[nexti] != r && isReverse != (a[nexti] < 0)) {
                continue;
            }
            if (length > 1 && a[nexti] == r) {
                return true;
            }
        }
        return false;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值