LeetCode 457. Circular Array Loop
原题链接在这里:https://leetcode.com/problems/circular-array-loop/
题目:
You are given a circular array nums
of positive and negative integers. If a number k at an index is positive, then move forward k steps. Conversely, if it's negative (-k), move backward k steps. Since the array is circular, you may assume that the last element's next element is the first element, and the first element's previous element is the last element.
Determine if there is a loop (or a cycle) in nums
. A cycle must start and end at the same index and the cycle's length > 1. Furthermore, movements in a cycle must all follow a single direction. In other words, a cycle must not consist of both forward and backward movements.
Example 1:
Input: [2,-1,1,2,2]
Output: true
Explanation: There is a cycle, from index 0 -> 2 -> 3 -> 0. The cycle's length is 3.
Example 2:
Input: [-1,2]
Output: false
Explanation: The movement from index 1 -> 1 -> 1 ... is not a cycle, because the cycle's length is 1. By definition the cycle's length must be greater than 1.
Example 3:
Input: [-2,1,-1,-2,-2]
Output: false
Explanation: The movement from index 1 -> 2 -> 1 -> ... is not a cycle, because movement from index 1 -> 2 is a forward movement, but movement from index 2 -> 1 is a backward movement. All movements in a cycle must follow a single direction.
Note:
- -1000 ≤ nums[i] ≤ 1000
- nums[i] ≠ 0
- 1 ≤ nums.length ≤ 5000
Follow up:
Could you solve it in O(n) time complexity and O(1) extra space complexity?
题解:
Use walker and runner pointers to check if there is a loop.
Every time, pointer move as i + nums[i]. If it is positive, return (i + nums[i])/nums.length. If it is negative, return (i+nums[i])/nums.length + nums.length.
In order to maintain the same direction, make sure each shifted index are all positive or all negative.
When walker and runner meets, then there is a loop.
But in case to avoid single element in the loop, check if next move is still here.
Last, if there is no loop for current routine, then mark all nums on this routine as 0, then there would not be duplicate calcuation on these numbers.
Time Complexity: O(n). n = nums.length.
Space: O(1).
AC Java:
class Solution {
public boolean circularArrayLoop(int[] nums) {
if(nums == null || nums.length < 2){
return false;
} for(int i = 0; i<nums.length; i++){
if(nums[i] == 0){
continue;
} int walker = i;
int runner = i;
while(nums[i]*nums[shift(runner, nums)]>0 && nums[i]*nums[shift(shift(runner, nums), nums)]>0){
walker = shift(walker, nums);
runner = shift(shift(runner, nums), nums);
// If there is a loop, walker and runner will meet
if(walker == runner){
// If there is only one element in loop, break while
if(walker == shift(walker, nums)){
break;
} return true;
}
} // When there is no loop with current routine,
// Mark value as 0, then there would not be duplicate calculation
int ind = i;
int val = nums[ind];
while(val*nums[ind]>0){
int nextInd = shift(ind, nums);
nums[ind] = 0;
ind = nextInd;
}
} return false;
} private int shift(int i, int [] nums){
int n = nums.length;
return i + nums[i] >= 0 ? (i + nums[i]) % n : (i + nums[i]) % n + n;
}
}
LeetCode 457. Circular Array Loop的更多相关文章
- [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】457. Circular Array Loop 环形数组是否存在循环 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题思路 快慢指针 代码 日期 题目地址:https://le ...
- [LeetCode] Circular Array Loop 环形数组循环
You are given an array of positive and negative integers. If a number n at an index is positive, the ...
- 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:Convert Sorted Array to Binary Search Tree,Convert Sorted List to Binary Search Tree
LeetCode:Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in asce ...
- [LeetCode] 805. Split Array With Same Average 用相同均值拆分数组
In a given integer array A, we must move every element of A to either list B or list C. (B and C ini ...
- [LeetCode] Shuffle an Array 数组洗牌
Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...
- [LeetCode] Sort Transformed Array 变换数组排序
Given a sorted array of integers nums and integer values a, b and c. Apply a function of the form f( ...
随机推荐
- golang 之文件操作
文件操作要理解一切皆文件. Go 在 os 中提供了文件的基本操作,包括通常意义的打开.创建.读写等操作,除此以外为了追求便捷以及性能上,Go 还在 io/ioutil 以及 bufio 提供一些其他 ...
- python_并发与通信
独立的进程内存空间与共享的服务器进程空间 知识点一: 进程间通信的限制 进程是独立的,互不干扰的独立内存空间我们想不能修改变量但是,深层次问题是,这个进程与那个进程完全失去了联系 import mul ...
- BZOJ1060或洛谷1131 [ZJOI2007]时态同步
BZOJ原题链接 洛谷原题链接 看上去就觉得是一道树形\(\mathtt{DP}\),不过到头来我发现我写了一个贪心.. 显然对越靠近根(记为\(r\))的边进行加权贡献越大,且同步的时间显然是从根到 ...
- C# vb .net实现淡色效果滤镜
在.net中,如何简单快捷地实现Photoshop滤镜组中的淡色效果呢?答案是调用SharpImage!专业图像特效滤镜和合成类库.下面开始演示关键代码,您也可以在文末下载全部源码: 设置授权 第一步 ...
- docker swarm yaml
https://www.cnblogs.com/bigberg/p/8867326.html 一.简介 Docker有个编排工具docker-compose,可以将组成某个应该的多个docker容器编 ...
- Hadoop问题解决:WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
在配置好hadoop的环境之后,命令启动./start-all.sh发现经常出现这样的一个警告: WARN util.NativeCodeLoader: Unable to load native-h ...
- LRU(Least Recently Used)算法的理解
https://blog.csdn.net/wydyd110/article/details/84023688 感谢 ,自己学习记笔记 内存里建立一个哈希表,后来数据多了,爆了.咋整呢? 一个算法,就 ...
- redux reducer笔记
踩坑一,reducer过于抽象 reducer写得没那么抽象也不会有人怪你的.^_^ reducer其实只有一个,由不同的reducer composition出来的.所以, reducer的父作用域 ...
- v8--sort 方法 源码 (2) 快速排序法
v8 sort方法部分关于快速排序法的源码: function QuickSort(a, from, to) { // Insertion sort is faster for short array ...
- Windows下MongoDB的下载安装、环境配置
下载MongoDB 1.进入MongoDB官网,Products -> 选择SOFTWARE下的MongoDB Server 2.选择下载最新版 3.选择对应的版本下载 msi安装包形式安装Mo ...