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( ...
随机推荐
- 通过Fastdfs进行文件上传服务(文件和图片的统一处理)
1.文件上传简单流程分析图: 2.Fastdfs介绍: Fastdfs由两个角色组成: Tracker(集群):调度(帮你找到有空闲的Storage) Storage(集群):文件存储(帮你保存文件或 ...
- HTML table 表格边框
一.总体思路: 1.表格无边框,背景颜色设置一种颜色(#DCDFE6),这样表格的边框的颜色就是表格的背景颜色: 2.单元格间距为1px,背景颜色设置为白色(#FFFFFF) // CSS table ...
- Html 对象的常用事件列举
事件名称 触发时间 对象例举 OnBlur 对象失去输入焦点 窗口和所有的表单对象 OnChange 用户改变对象的值 文本框.文本区域.选择列表等 OnClick 用户鼠标点击 链接.按钮.单选钮. ...
- Win10各个PC版本的差别
新的电脑有的已经不能够支持安装win7了,也就是说新电脑的硬件支持只能安装win10.那么win10有都有那些版本呢?这些版本之间又有什么样区别?我相信很多人并不是很了解.今天就来整理一下这些资料.据 ...
- 刨根究底字符编码之十六——Windows记事本的诡异怪事:微软为什么跟联通有仇?(没有BOM,所以被误判为UTF8。“联通”两个汉字的GB内码,其第一第二个字节的起始部分分别是“110”和“10”,,第三第四个字节也分别是“110”和“10”)
1. 当用一个软件(比如Windows记事本或Notepad++)打开一个文本文件时,它要做的第一件事是确定这个文本文件究竟是使用哪种编码方式保存的,以便于该软件对其正确解码,否则将显示为乱码. 一般 ...
- POI2015 WYC
也许更好的阅读体验 \(\mathcal{Description}\) 给定一张n个点m条边的带权有向图,每条边的边权只可能是1,2,3中的一种.将所有可能的路径按路径长度排序,请输出第k小的路径的长 ...
- IntelliJ IDEA 换背景免费酷炫的插件(转)
一.插件的安装 打开setting文件选择Plugins选项 Ctrl + Alt + S File -> Setting 分别是安装JetBrains插件,第三方插件,本地已下载的插件包. 二 ...
- 两个div并排显示,当浏览器界面缩小时会出现换行
解决:规定两个子div的父div的宽 <div id="showDataDiv" style="width: 1000px"> <div st ...
- java之mybatis之helloworld
1. MyBatis 是一款一流的支持自定义SQL.存储过程和高级映射的持久化框架. MyBatis几乎消除了所有的 JDBC 代码,也基本不需要手工去设置参数和获取检索结果. MyBatis几乎能够 ...
- 第一个 macOS 64位 kbmmw 服务器
前几天,Delphi 10.3.2 正式发布,这个小版本升级却增加了一个非常大的平台支持,增加了 macos 64位的支持,今天做一个macOS 64位的kbmmw应用,让kbmmw 服务器的应用更广 ...