Leetcode0457--Circular Array Loop
【转载请注明】https://www.cnblogs.com/igoslly/p/9339478.html
class Solution {
public:
bool circularArrayLoop(vector<int>& nums) {
int index=,size = nums.size(),step=;
// 处理index
while(step<size){
if(nums[index]>){
index = (index+nums[index])%size;
}else if(nums[index]<){
index = (index-nums[index])%size;
}
// 判断index返回 0
if(index==){
return true;
}
step++;
}
return false;
}
};
class Solution {
public:
bool circularArrayLoop(vector<int>& nums) {
int index=,size = nums.size(),step=;
if(size<=){
return false;
}
while(step<size){
// 修改元素值为0,设flag
if(nums[index]==){
return true;
}
int temp= nums[index];
nums[index]=;
// 进行index操作
if(nums[index]>){
index = (index+temp)%size;
}else if(nums[index]<){
index = (index-temp)%size;
}
step++;
}
return false;
}
};
class Solution {
public:
bool circularArrayLoop(vector<int>& nums) {
int index=,size = nums.size(),step=;
if(size<=){
return false;
}
while(step<=size){
int temp= nums[index];
int preindex=index;
// 进行index操作
if(nums[index]>){
if(index==(index+temp)%size){
return false;
}
index = (index+temp)%size;
}else if(nums[index]<){
if(index==(index-temp)%size){
return false;
}
index = (index-temp)%size;
}
// 修改元素值为0,设flag
if(nums[preindex]==){
return true;
}
nums[preindex]=;
step++;
}
return false;
}
};
class Solution {
public:
bool circularArrayLoop(vector<int>& nums) {
int index=,size = nums.size(),step=;
if(size<=){
return false;
}
for(int i=;i<size;i++){
if(nums[i]==) continue;
// two pointers
int j=i,k = (i+nums[i]+size)%size;
while(nums[i]*nums[k]> && nums[i]*nums[(k+nums[k]+size)%size]>){
if(j==k){
// check for loop with only one element
if(j = (j+nums[j]+size)%size) break;
return true;
}
j = (j+nums[j]+size)%size;
k = (k+nums[k]+size)%size;
k = (k+nums[k]+size)%size;
}
// loop not found
j =i;
int val = nums[i];
while(nums[j]*val>){
int next = (j+nums[j]+size)%size;
nums[j]=;
j=next;
}
}
return false;
}
};
Leetcode0457--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 ...
- 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] 918. Maximum Sum Circular Subarray 环形子数组的最大和
Given a circular array C of integers represented by A, find the maximum possible sum of a non-empty ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- 算法与数据结构基础 - 双指针(Two Pointers)
双指针基础 双指针(Two Pointers)是面对数组.链表结构的一种处理技巧.这里“指针”是泛指,不但包括通常意义上的指针,还包括索引.迭代器等可用于遍历的游标. 同方向指针 设定两个指针.从头往 ...
随机推荐
- COJ 1156 Switching bulbs
一道模拟题目 对于所有0 还是 1 我们都可以想象做均为 0 的状态 v[i]表示原来的值 但是对于原来为1的要加上其所在的值作为初始值 然后转化后 a[i] = -v[i] , 如果原来为0 , ...
- hdu 2224 双调欧几里得旅行商问题tsp
/* 题意:平面上n个点,确定一条连接各点的最短闭合旅程且每个点仅用一次.这个解的一般形式为NP的(在多项式时间内可以求出) 建议通过只考虑双调旅程(bitonictour)来简化问题,这种旅程即为从 ...
- - > 贪心基础入门讲解二——活动安排问题
有若干个活动,第i个开始时间和结束时间是[Si,fi),只有一个教室,活动之间不能交叠,求最多安排多少个活动? 分析: 我们就是想提高教室地利用率,尽可能多地安排活动.考虑容易想到的几种贪心策略: ( ...
- 链表中倒数第N个元素——剑指Offer
https://www.nowcoder.net/practice/529d3ae5a407492994ad2a246518148a?tpId=13&tqId=11167&tPage= ...
- MQTT---HiveMQ源代码具体解释(一)概览
源博客地址:http://blog.csdn.net/pipinet123 MQTT交流群:221405150 面向群体 想自己实现MQTT Broker的朋友 对现有开源的MQTT Broker或多 ...
- shell apt install 按tab键自动补全
insert if [ -f /etc/bash_completion ]; then . /etc/bash_completion fi to ~/.bashrc
- 如何快速查看EPS,AI等矢量文件
使用Adobe Bridge可以快速查看所有这些格式的资源 查看EPS格式图片: 查看AI格式: 某些AI文件则无法预览(此外还有一些CDR的格式) 相比之下,ACDSee的效果则不如Adob ...
- using gdb to debug c program
#include <stdio.h> static void display(int i, int *ptr); int main(void) { int x = 5; int *xptr ...
- MICRO SIM卡(SIM小卡)尺寸图及剪卡图解
如今使用MICRO SIM卡的手机越来越多.近期刚刚买了一个手机到手才发现尼马使用的是MICRO SIM卡.再去买剪卡器吧,十几二十块用一次就废了,去营业厅吧.又比較远,懒的出门.怎么办呢,自己剪!这 ...
- ZOJ 3209 Treasure Map (Dancing Links 精确覆盖 )
题意 : 给你一个大小为 n * m 的矩形 , 坐标是( 0 , 0 ) ~ ( n , m ) .然后给你 p 个小矩形 . 坐标是( x1 , y1 ) ~ ( x2 , y2 ) , 你选 ...