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] 题目描述 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素最多出现两次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原 ...
随机推荐
- 【转】Java Cipher类 DES算法(加密与解密)
Java Cipher类 DES算法(加密与解密) 1.加密解密类 import java.security.*; import javax.crypto.*; import java.io.*; / ...
- LINQ 组合查询 和分页查询的使用
前端代码 <%@ Page Language="C#" AutoEventWireup="true" Debug="true" Cod ...
- 解决Layui的switch样式显示问题
Layui官方文档是这么说的: <input type="checkbox" name="xxx" lay-skin="switch" ...
- Codeforces 786E. ALT 最小割+倍增
E. ALT http://codeforces.com/problemset/problem/786/E 题意: 给出一棵 n 个节点的树与 m 个工人.每个工人有一条上下班路线(简单路径),一个工 ...
- UVA 11093 Just Finish it up 环形跑道 (贪心)
有一个环形跑道,上面有n个加油站,到i号加油站可以加pi的油,跑到下一站要花费qi的油,起点任意选,问是否有一个起点可跑完整个跑道. 从i开始跑,如果遇到某个站j不能跑了,那么从i到j之间的站开始跑, ...
- CF Gym 100187M Heaviside Function(二分)
题意:给你一个函数和一些系数,给你一堆询问,求函数值. 根据s的符号,分成两组讨论,函数值与比x小的系数数量有关,二分输出答案. #include<cstdio> #include< ...
- E - Polycarp and Snakes
E - Polycarp and Snakes 题意:在一个全是点的图上开始画线,每次将一行或一列任意长度染成字母,一笔染一种字母,字母必须从a开始连续到后面某个字母可以覆盖. 问所给图案是否满足 , ...
- JavaScript——图片懒加载
前言 有一个朋友问我这个问题,刚好有时间,现在就简单的写个Demo~ github | https://github.com/wangyang0210/bky/tree/picLoadLazy 内容 ...
- window.addEventListener介绍说明
原型 public override function addEventListener(type:String, listener:Function, useCapture:Boolean = fa ...
- iOS中的数据存储方式_Plist
plist文件只能存储OC常用数据类型(NSString.NSDictionary.NSArray.NSData.NSNumber等类型)而不能直接存储自定义模型对象; 我们拿NSData举例: /* ...