leetcode 457. Circular Array Loop 数组中寻找环的存在+双指针+循环判断
2017-12-11 19:02
441 查看
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?
题意很简单,就是一个循环的判断,这道题可以使用双指针来判断,要注意的是双指针的移动要注意保持方向一致
所以在while的地方我们要求当前的和fast和fast的方向是一致的,这样才能全部AC
代码如下:
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?
题意很简单,就是一个循环的判断,这道题可以使用双指针来判断,要注意的是双指针的移动要注意保持方向一致
所以在while的地方我们要求当前的和fast和fast的方向是一致的,这样才能全部AC
代码如下:
#include <iostream> #include <vector> #include <map> #include <unordered_map> #include <set> #include <unordered_set> #include <queue> #include <stack> #include <string> #include <climits> #include <algorithm> #include <sstream> #include <functional> #include <bitset> #include <numeric> #include <cmath> #include <regex> using namespace std; class Solution { public: bool circularArrayLoop(vector<int>& a) { for (int i = 0; i < a.size(); i++) { if (a[i] == 0) continue; int slow = i, fast = getNextIndex(i, a); while (a[i] * a[fast] > 0 && a[i] * a[getNextIndex(fast, a)] > 0) { if (slow == fast) { if (slow == getNextIndex(slow, a)) break; else return true; } else { slow = getNextIndex(slow, a); fast = getNextIndex(getNextIndex(fast, a), a); } } } return false; } int getNextIndex(int i, vector<int>& a) { int size = a.size(); if (i + a[i] > 0) return (i + a[i]) % size; else return ((i + a[i]) % size + size); } };
相关文章推荐
- js判断数组key是否存在(不用循环)的简单实例
- js 单次循环判断数组中是否存在重复值
- LeetCode (27) Linked List Cycle (判断cycle存在、寻找cycle入口节点)
- leetcode 判断简单链表是否存在循环
- 每天一道LeetCode-----有序数组循环右移n位后,寻找最小值,数组中可能包含重复元素
- 不使用循环或递归判断一个数是否为3的幂(leetcode 326)
- 判断一个数组是否为单一完全循环数组
- 判断两个数组中是否存在相同的数字
- (lodash_gcy)hasAttr——判断数组中是否存在某个元素
- 寻找数组中和为指定数字的子数组,可以重复,Leetcode 39
- 给定排好序的数组A,大小为n,现给定数X,判断A中是否存在两数之和等于X
- 【leetcode】寻找两个已排序数组的中位数(类似二分)
- 5.斐波那契数组:循环和递归;输入n时判断该数是几:(for循环和递归写) 1 1 2 3 5 8 13 21 34
- Java总哈希表的运用,判断一个数组中是否存在相同的元素之间的距离在k以内!
- 如何高效的判断数组中是否存在至少两个相同的数值,存在返回true,不存在返回false
- LeetCode 16. 3Sum Closest--寻找数组中的三个元素和,该和与给定的值的差最小,输出这个和(三个元素可以不连续)
- [Leetcode,python] Find All Numbers Disappeared in an Array 寻找数组中消失的数字
- 判断数组是否存在某个值
- BAT循环判断进程是否存在 bat 如果存在则继续判断 不存在则重新运行
- LeetCode—House Robber 寻找数组不相邻组合最大值DP