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的更多相关文章

  1. [LeetCode] Circular Array Loop 环形数组循环

    You are given an array of positive and negative integers. If a number n at an index is positive, the ...

  2. [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 ...

  3. LeetCode 457. Circular Array Loop

    原题链接在这里:https://leetcode.com/problems/circular-array-loop/ 题目: You are given a circular array nums o ...

  4. 【LeetCode】457. Circular Array Loop 环形数组是否存在循环 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题思路 快慢指针 代码 日期 题目地址:https://le ...

  5. [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 ...

  6. C++ STL@ list 应用 (leetcode: Rotate Array)

    STL中的list就是一双向链表,可高效地进行插入删除元素. List 是 C++标准程式库 中的一个 类 ,可以简单视之为双向 连结串行 ,以线性列的方式管理物件集合.list 的特色是在集合的任何 ...

  7. [LeetCode] Split Array Largest Sum 分割数组的最大值

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

  8. [LeetCode] Patching Array 补丁数组

    Given a sorted positive integer array nums and an integer n, add/patch elements to the array such th ...

  9. [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  ...

随机推荐

  1. zorka源码解读之Instrument实现原理

    主要用到三方面技术: beanshell来实现可扩展:告诉zorkaAgent插桩的具体需求,包括插桩的方法和值.插桩的时机.插桩追踪记录方式等. Instrument来通过代理的方式访问JVM,实现 ...

  2. JavaScript调用函数的方法

    摘要:这篇文章详细的介绍了Javascript中各种函数调用的方法及其原理,对于理解JavaScript的函数有很大的帮助! 一次又一次的,我发现,那些有bug的Javascript代码是由于没有真正 ...

  3. js 无缝滚动效果学习

    <!DOCTYPE html> <html> <head> <title>无缝滚动测试</title> <meta http-equi ...

  4. Django分析之国际化处理

    最近在公司终于开始做web开发了,本以为会是简单的首页之类的小规模项目,结果上来就是一个处理大数据分析的项目,一个关于油品分析的系统,不过我接到的第一个任务是做这个网站的国际化处理,虽然项目还没有上线 ...

  5. 五分钟理解一致性哈希算法(consistent hashing)

    转载请说明出处:http://blog.csdn.net/cywosp/article/details/23397179 一致性哈希算法在1997年由麻省理工学院提出的一种分布式哈希(DHT)实现算法 ...

  6. <二>JDBC_通过ResultSet执行查询操作

    一.ResultSet: 结果集. 封装了使用 JDBC 进行查询的结果.  1. 调用 Statement 对象的 executeQuery(sql) 可以得到结果集. 2. ResultSet 返 ...

  7. HDU1242 BFS+优先队列

    Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  8. js入门篇之正则表达式基础

    定义:正则用于规定在文本中检索的内容,它是对字符串执行模式匹配的强大工具 RegExp(正则表达式) 对象的语法: new RegExp(pattern, attributes); pattern为一 ...

  9. C++ 画星号图形——空心三角形(星号居中对齐)(核心代码介绍)

    //输出另外一种由星号组成的三角形(星号居中对齐) int a;//控制组成三角形的星号的行数 cout<<"请输入要组成三解形的星号的行数n(n>=2):\n" ...

  10. __DATE__ 与 __TIME__转换为标准格式时间字符串的方法

    // Example of __DATE__ string: "Jul 27 2012"// 01234567890 #define BUILD_YEAR_CH0 (__DATE_ ...