Leetcode 457.环形数组循环
环形数组循环
给定一组含有正整数和负整数的数组。如果某个索引中的 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.环形数组循环的更多相关文章
- Java实现 LeetCode 457 环形数组循环
457. 环形数组循环 给定一个含有正整数和负整数的环形数组 nums. 如果某个索引中的数 k 为正数,则向前移动 k 个索引.相反,如果是负数 (-k),则向后移动 k 个索引.因为数组是环形的, ...
- [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 ...
- [LeetCode] Circular Array Loop 环形数组循环
You are given an array of positive and negative integers. If a number n at an index is positive, the ...
- [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 ...
- 【LeetCode】457. Circular Array Loop 环形数组是否存在循环 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题思路 快慢指针 代码 日期 题目地址:https://le ...
- LeetCode 622:设计循环队列 Design Circular Queue
LeetCode 622:设计循环队列 Design Circular Queue 首先来看看队列这种数据结构: 队列:先入先出的数据结构 在 FIFO 数据结构中,将首先处理添加到队列中的第一个元素 ...
- [CareerCup] 14.6 CircularArray 环形数组
14.6 Implement a CircularArray class that supports an array-like data structure which can be efficie ...
- Task 4.3 求环形数组的最大子数组和
任务要求:输入一个整形数组,数组里有正数也有负数. 数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和. 如果数组A[0]……A[j-1]首尾相邻,允许A[i-1], …… A[n- ...
- LeetCode:删除排序数组中的重复项||【80】
LeetCode:删除排序数组中的重复项||[80] 题目描述 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素最多出现两次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原 ...
随机推荐
- class类型重定义,防止头文件重复加载
今天调用自己写的一个类,出现了class类型重定义问题,上网查了相关资料,发现是头文件重复include引起的问题. 防止头文件重复加载: 系统那些头文件,无论怎么include都没事,因为一般都用了 ...
- GoAccess安装和使用介绍
使用文档参考地址:https://my.oschina.net/mrco/blog/181737https://www.fanhaobai.com/2017/06/go-access.html goa ...
- 机器学习&深度学习资料分享
感谢:https://github.com/ty4z2008/Qix/blob/master/dl.md <Brief History of Machine Learning> 介绍:这是 ...
- line-block,white-space,overflow
line-block:设置行间的距离(行高),只能控制块级元素,span这样的行内元素无法控制,并且当块级元素 中包含span的时候设置line-block会使span的自适应高度小于块级元素的高度, ...
- 在Vue将第三方JS库封装为组件使用
第三方JS库地址:https://github.com/inorganik/CountUp.js 使用NPM进行安装: npm install --save countup 根据官方回答,CountU ...
- java中插入myslq的datetime类型的
java.util.Date d = new java.util.Date(); Timestamp tt=new Timestamp(d.getTime()); ps.setTimestamp(4, ...
- Unicode与ASCiI之间有什么区别?java当中的转义字符 Character类的使用 String类的使用
ASCII码 称为 美国标准信息交换码 (American standard code of Information Interchange) 其中一共有多少个码?2的7次幂 128个 Unicode ...
- iOS中的数据存储方式_Plist
plist文件只能存储OC常用数据类型(NSString.NSDictionary.NSArray.NSData.NSNumber等类型)而不能直接存储自定义模型对象; 我们拿NSData举例: /* ...
- pandas中数据聚合【重点】
数据聚合 数据聚合是数据处理的最后一步,通常是要使每一个数组生成一个单一的数值. 数据分类处理: 分组:先把数据分为几组 用函数处理:为不同组的数据应用不同的函数以转换数据 合并:把不同组得到的结果合 ...
- 【Git版本控制】idea中使用git进行项目管理
转载博文:完整教程-idea使用git进行项目管理(总结版)