环形数组循环

给定一组含有正整数和负整数的数组。如果某个索引中的 n 是正数的,则向前移动 n 个索引。相反,如果是负数(-n),则向后移动 n 个索引。

假设数组首尾相接。判断数组中是否有环。环中至少包含 2 个元素。环中的元素一律"向前"或者一律"向后"。

示例 1:给定数组 [2, -1, 1, 2, 2], 有一个循环,从索引 0 -> 2 -> 3 -> 0。

示例 2:给定数组[-1, 2], 没有循环。

注意:给定数组保证不包含元素"0"。

你能写出时间复杂度为 O(n) 且空间复杂度为 O(1) 的算法吗?

思路

就是一个循环的判断,这道题可以使用双指针来判断,要注意的是双指针的移动要注意保持方向一致

所以在while的地方我们要求当前的和fast和fast的方向是一致的

 class Solution {
public static boolean circularArrayLoop(int[] nums) {
boolean retBoolean = false;
for (int i = 0; i < nums.length; i++) {
int j = i, k = getNextIndex(nums, i);
while (nums[i] * nums[j] > 0 && nums[i] * nums[k] > 0 && nums[i] * nums[getNextIndex(nums, k)] > 0) {
if (j == k) {
if (j == getNextIndex(nums, j)) {
break;
}
return true;
}
j = getNextIndex(nums, j);
k = getNextIndex(nums, getNextIndex(nums, k));
}
}
return retBoolean;
} private static int getNextIndex(int[] nums, int i) {
int length = nums.length;
int nextPosition = i + nums[i];
return nextPosition >= 0 ? nextPosition % length : length + (nextPosition % length);
}
}

Leetcode 457.环形数组循环的更多相关文章

  1. Java实现 LeetCode 457 环形数组循环

    457. 环形数组循环 给定一个含有正整数和负整数的环形数组 nums. 如果某个索引中的数 k 为正数,则向前移动 k 个索引.相反,如果是负数 (-k),则向后移动 k 个索引.因为数组是环形的, ...

  2. [LeetCode] 457. Circular Array Loop 环形数组循环

    You are given a circular array nums of positive and negative integers. If a number k at an index is ...

  3. [LeetCode] Circular Array Loop 环形数组循环

    You are given an array of positive and negative integers. If a number n at an index is positive, the ...

  4. [Swift]LeetCode457. 环形数组循环 | Circular Array Loop

    You are given an array of positive and negative integers. If a number n at an index is positive, the ...

  5. 【LeetCode】457. Circular Array Loop 环形数组是否存在循环 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题思路 快慢指针 代码 日期 题目地址:https://le ...

  6. LeetCode 622:设计循环队列 Design Circular Queue

    LeetCode 622:设计循环队列 Design Circular Queue 首先来看看队列这种数据结构: 队列:先入先出的数据结构 在 FIFO 数据结构中,将首先处理添加到队列中的第一个元素 ...

  7. [CareerCup] 14.6 CircularArray 环形数组

    14.6 Implement a CircularArray class that supports an array-like data structure which can be efficie ...

  8. Task 4.3 求环形数组的最大子数组和

    任务要求:输入一个整形数组,数组里有正数也有负数. 数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和.    如果数组A[0]……A[j-1]首尾相邻,允许A[i-1], …… A[n- ...

  9. LeetCode:删除排序数组中的重复项||【80】

    LeetCode:删除排序数组中的重复项||[80] 题目描述 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素最多出现两次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原 ...

随机推荐

  1. 织梦channel标签内调用子栏目内容

    文件:include\taglib\channel.lib.php 把代码 SELECT id,typename,typedir,isdefault,ispart,defaultname,nameru ...

  2. Python+selenium之带unittest的脚本分析

    from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.c ...

  3. PostgreSQL缓存

    目录[-] pg_buffercache pgfincore pg_prewarm dstat Linux ftools 使用pg_prewarm预加载关系/索引: pgfincore 输出: 怎样刷 ...

  4. UWP开发:应用文件存储

    应用设置由于数据量和数据类型的限制,有很大的局限性,所以还需要应用文件存储,以文件的方式存储数据.在每个应用的应用数据存储中,该应用拥有系统定义的根目录:一个用于本地文件,一个用于漫游文件,还有一个用 ...

  5. GWT-2.5.1离线安装

    GWT官方离线包下载地址 http://dl.google.com/eclipse/plugin/3.7/zips/gpe-e37-latest-updatesite.zip 以下是GWTDesign ...

  6. Java 集合框架_下

    Map接口 特点: [1]Map接口称为键值对集合或者映射集合,其中的元素(entry)是以键值对(key-value)的形式存在. [2]Map 容器接口中提供了增.删.改.查的方式对集合进行操作. ...

  7. [论文理解] Rapid-Object-Detection-using-a-Boosted-cascade-of-simple-features

    Rapid-Object-Detection-using-a-Boosted-cascade-of-simple-features 简介 文章是2001年发表的,是一篇很经典的Object Detec ...

  8. 一、git提交代码步骤

    git add .   //添加到本地缓存区 git commit -m '备注 ' //提交代码到本地仓库 git push  //将本地代码推送到develop分支上 git checkout m ...

  9. python_87_shelve模块

    'shelve模块是一个简单的key,value将内存数据通过文件持久化的模块,可以持久化任何pickle可支持的python数据格式(只支持pickle)' #序列化,将数据写入文件 import ...

  10. win7便笺元数据损坏,最新解决办法

    Windows7系统开机时出现“部分便笺的元数据已被破坏,便笺已将其恢复为默认值.”问题,最新解决办法,图文说明,亲测,希望可以帮到大家 工具/原料   Windows7系统 InkObj.dll.T ...