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)是面对数组.链表结构的一种处理技巧.这里“指针”是泛指,不但包括通常意义上的指针,还包括索引.迭代器等可用于遍历的游标. 同方向指针 设定两个指针.从头往 ...
随机推荐
- [luoguP1901] 发射站(单调栈)
传送门 呵呵 ——代码 #include <cstdio> #include <iostream> #define N 1000010 #define LL long long ...
- noip模拟赛 abcd
[问题描述]有4个长度为N的数组a,b,c,d.现在需要你选择N个数构成数组e,数组e满足a[i]≤e[i]≤b[i]以及 并且使得 最大.[输入格式]输入文件名为abcd.in.输入文件共 N+1 ...
- Asm.Def谈笑风生
★ 输入文件:asm_talk.in 输出文件:asm_talk.out简单对比时间限制:2 s 内存限制:256 MB [题目描述] “人呐都不知道,自己不可以预料,直升机刚一出圣地亚哥 ...
- Spring Boot配置文件规则以及使用方法官方文档查找以及Spring项目的官方文档查找方法
比如要使用Spring Boot实现一个功能,最直接的方式是Google,但是往往搜索出来的都比较乱,关键是乱在不同的版本上,比如1.x版本和2.x版本的配置是不一样的.最明显区别是在使用Thymel ...
- Domino V8 在 UNIX/Linux 平台上的安装及其常见问题
在 IBM Bluemix 云平台上开发并部署您的下一个应用. 开始您的试用 Domino V8 的安装需求 Domino V8 可以支持多种平台和操作系统,表1 列出了其支持的各种 UNIX/Lin ...
- 1.4-动态路由协议OSPF⑤
OSPF的特殊区域(Stub/total Stub区域,无法引入外部路由): ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 第一种 ...
- 编程算法 - 求1+2+...+n(函数继承) 代码(C++)
求1+2+...+n(函数继承) 代码(C++) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 求1+2+...+n, 要求不能使用乘除法\for\whi ...
- 【源代码剖析】tornado-memcached-sessions —— Tornado session 支持的实现(二)
客官您最终回头了! 让我们本着探(zuo)索(si)精神把 session.py 看完吧... 首先看看须要的库: pickle 一个用于序列化反序列化的库(听不懂?你直接看成和 json 一样作用即 ...
- react State改变,页面却没有改变
react 小白编程 做项目时遇到了个问题,无论我怎么查看我的action.reducer 还是 dispatch 函数,都没有发现有什么毛病.但是 debugger 的时候,state 改变了,页面 ...
- YTU 2723: 默认参数--求圆的面积
2723: 默认参数--求圆的面积 时间限制: 1 Sec 内存限制: 128 MB 提交: 206 解决: 150 题目描述 根据半径r求圆的面积, 如果不指定小数位数,输出结果默认保留两位小数 ...