Leetcode: Circular Array Loop
You are given an array of positive and negative integers. If a number n at an index is positive, then move forward n steps. Conversely, if it's negative (-n), move backward n steps. Assume the first element of the array is forward next to the last element, and the last element is backward next to the first element. Determine if there is a loop in this array. A loop starts and ends at a particular index with more than 1 element along the loop. The loop must be "forward" or "backward'. Example 1: Given the array [2, -1, 1, 2, 2], there is a loop, from index 0 -> 2 -> 3 -> 0. Example 2: Given the array [-1, 2], there is no loop. Note: The given array is guaranteed to contain no element "0". Can you do it in O(n) time complexity and O(1) space complexity?
注意The loop must be "forward" or "backward'. 所以这就是为什么[-2, 1, -1, -2, -2]是false的原因
Just think it as finding a loop in Linkedlist, except that loops with only 1 element do not count. Use a slow and fast pointer, slow pointer moves 1 step a time while fast pointer moves 2 steps a time. If there is a loop (fast == slow), we return true, just except there's only 1 element in the loop. else if we meet element with different directions, then the search fail, we set all elements along the way to 0. Because 0 is fail for sure so when later search meet 0 we know the search will fail.
public class Solution {
int[] arr;
public boolean circularArrayLoop(int[] nums) {
arr = nums;
for (int i=0; i<nums.length; i++) {
if (nums[i] == 0) continue;
//two pointers
int j=i, k=shift(i);
while (nums[i]*nums[k]>0 && nums[i]*nums[shift(k)]>0) {
if (j == k) {
// check for loop with only one element
if (j == shift(j)) break;
return true;
}
j = shift(j);
k = shift(shift(k));
}
// loop not found, set all element along the way to 0
j = i;
int val = nums[i];
while (nums[j]*val > 0) {
int next = shift(j);
nums[j] = 0;
j = next;
}
}
return false;
}
public int shift(int pos) {
return (pos+arr[pos]+arr.length) % arr.length;
}
}
Solution 2: 更清晰,没有找到Loop就set为0不是必需的
public class Solution {
int[] arr;
public boolean circularArrayLoop(int[] nums) {
arr = nums;
for (int i=0; i<nums.length; i++) {
if (nums[i] == 0) continue;
//two pointers
int j=i, k=i;
while (nums[i]*nums[shift(k)]>0 && nums[i]*nums[shift(shift(k))]>0) {
j = shift(j);
k = shift(shift(k));
if (j == k) {
// check for loop with only one element
if (j == shift(j)) break;
return true;
}
}
// loop not found, set all element along the way to 0, not necessary
}
return false;
}
public int shift(int pos) {
return (pos+arr[pos]+arr.length) % arr.length;
}
}
Leetcode: Circular Array Loop的更多相关文章
- [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] 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
原题链接在这里:https://leetcode.com/problems/circular-array-loop/ 题目: You are given a circular array nums o ...
- 【LeetCode】457. Circular Array Loop 环形数组是否存在循环 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题思路 快慢指针 代码 日期 题目地址:https://le ...
- [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 ...
- C++ STL@ list 应用 (leetcode: Rotate Array)
STL中的list就是一双向链表,可高效地进行插入删除元素. List 是 C++标准程式库 中的一个 类 ,可以简单视之为双向 连结串行 ,以线性列的方式管理物件集合.list 的特色是在集合的任何 ...
- [LeetCode] Split Array Largest Sum 分割数组的最大值
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
- [LeetCode] Patching Array 补丁数组
Given a sorted positive integer array nums and an integer n, add/patch elements to the array such th ...
- [LeetCode] Rotate Array 旋转数组
Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...
随机推荐
- self.automaticallyAdjustsScrollViewInsets
导航视图内Push进来的以“TableView”(没有ScrollView截图,就将就一下)为主View的视图,本来我们的cell是放在(0,0)的位置上的,但是考虑到导航栏.状态栏会挡住后面的主视图 ...
- Is It A Tree?[HDU1325][PKU1308]
Is It A Tree? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- BFC给我的帮助以及对hasLayout的认识
布局的时候经常想让一个或几个元素并并排的放在一起,有时给其中的一个浮动,元素是在一行了,可还是都左边重叠了,总是这样那样改来改去,小白的我也是醉了! 今天偶然间看到了了BFC这个东东,虽然现在还是不是 ...
- CSV格式数据如何导入SqlServer?
一.使用微软数据库IDE管理软件:Microsoft SQL Server Management Studio 1.标准的CSV文件格式如下: 2.建数据表 3.在需要导入的数据库右键点击“任务”,选 ...
- wordexpress
登陆数据库:mysql -uroot -p 创建数据库:CREATE DATABASE wordpress; 创建数据库用户:CREATE USER wordpress@localhost IDENT ...
- *HDU1969 二分
Pie Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...
- php操作数据库的简单示例
放假期间自己又写了几个简单的网页,但在服务器中打开时和在网站上打开时不一样,在服务器中打开的出现了错误,字体比一般的腰大好多,页面也相应地变大了,一些块即使用了浮动和clear浮动还是被遮住了,我只好 ...
- php取整函数ceil,floor,round,intval函数的区别
开发过程中,遇到数据处理取整的时候,你会用哪个呢,小涛来介绍一下:PHP取整函数有ceil,floor,round,intval,下面详细介绍一下: 1.ceil — 进一法取整说明float cei ...
- SpringBoot list查询方法
SpringBoot中根据不同查询条件,获取list列表.@Componentpublic class QuerySpecifications {} (1)根据商户,查询商品全部列表 public S ...
- 总结-Hibernate
JPA 全称 Java Persistence API @Entity @Table(name = "user") public class User { @Id @Generat ...