环形数组循环

给定一组含有正整数和负整数的数组。如果某个索引中的 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. iOS开发 - Protocol协议及委托代理(Delegate)

    因为Object-C是不支持多继承的,所以很多时候都是用Protocol(协议)来代替.Protocol(协议)只能定义公用的一套接口,但不能提供具体的实现方法.也就是说,它只告诉你要做什么,但具体怎 ...

  2. MySQL基础环境_安装配置教程(Windows7 64或Centos7.2 64、MySQL5.7)

    MySQL基础环境_安装配置教程(Windows7 64或Centos7.2 64.MySQL5.7) 安装包版本 1)     VMawre-workstation版本包 地址: https://m ...

  3. SQL Server数据库所有表重建索引

    USE My_Database;DECLARE @name varchar(100) DECLARE authors_cursor CURSOR FOR  Select [name]   from s ...

  4. LibreOJ #6208. 树上询问

    内存限制:512 MiB 时间限制:500 ms 标准输入输出 题目类型:传统 评测方式:文本比较 上传者: 匿名 树链剖分+线段树 屠龙宝刀点击就送 #include <vector> ...

  5. method 'redisConnectionFactory' threw exception; nested exception is java.lang.NoClassDefFoundError

    spring boot 整合redis是报了如下错误 org.springframework.beans.factory.UnsatisfiedDependencyException: Error c ...

  6. 从Docker到Kubernetes进阶

    分享个网站,k8s技术圈阳明大佬的网站 现在基本都用有道云笔记了,比较方便,所以准备弃用博客园了...

  7. java基础—线程(一)

    一.线程的基本概念

  8. Linux运维笔记--第二部

    第2部-重要目录结构详解 1.回顾Linux目录结构知识 /dev/            设备目录 /etc/             系统配置及服务配置文件,启动命令的目录 /proc       ...

  9. Python Web 架构

    1. Django(全能型)2. Tornado3. BottlePython+Bottle+Sina SAE快速构建网站http://www.cnblogs.com/Xjng/p/3511983.h ...

  10. C# WPF 粘贴板记录器

    工作学习中需要搜索很多资料,有建立文档对遇到过的问题进行记录,但是一来麻烦,二来有些当时认为不重要的事情,也许一段时间后认为是重要的,需要记录的,却又一时找不到,浪费时间做重复的事情.正好借着这个机会 ...