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)是面对数组.链表结构的一种处理技巧.这里“指针”是泛指,不但包括通常意义上的指针,还包括索引.迭代器等可用于遍历的游标. 同方向指针 设定两个指针.从头往 ...
随机推荐
- Python学习笔记 (1)Hello World(环境搭建+输出Hello World!)
随想 高考发挥失常.科三遇火车发挥失常,各种不顺……突然发现假期都快没了,才想起高考前想象的这个假期要做的一堆事,现在来多完成一件吧. 这几篇博客仅只是我的学习笔记,凑合看吧.我这个python小白看 ...
- noip模拟赛 蒜头君打地鼠
分析:直接一个一个地去暴力枚举分数比较少,我们需要一种比较快的统计一定空间内1的数量,标准做法是前缀和,但是二维前缀和维护的是一个矩形内的值,这个是旋转过的该怎么办?可以把图旋转45°,不过这样比较考 ...
- IM聊实现客户端之间信息交互需求文档
终于放假啦~之前学习太忙很多知识点都没有写博客,可能自己学会了但没有分享给大家,接下来几天我可能把一些学过的东西整理成博客发出来供大家相互学习交流. 需求分析说明书 HuaXinIM聊软件 潘浩 20 ...
- 洛谷—— P2196 挖地雷
https://www.luogu.org/problem/show?pid=2196 题目背景 NOIp1996提高组第三题 题目描述 在一个地图上有N个地窖(N<=20),每个地窖中埋有一定 ...
- zoj——1202 Divide and Count
Divide and Count Time Limit: 2 Seconds Memory Limit: 65536 KB Jack has several beautiful diamon ...
- SiteMesh2-简介
简介: SiteMesh类似与ASP.NET的模板页. SiteMesh是由一个基于Web页面布局.装饰以及与现存Web应用整合的框架.它能帮助我们在由大量页面构成的项目中创建一致的页面布局和外观,如 ...
- GNS3和Cisco IOU搭建路由交换实验-IOU篇
http://www.mamicode.com/info-detail-605879.html
- 演练:使用VS2010 C# 创作简单的多线程组件
BackgroundWorker 组件取代了 System.Threading 命名空间并添加了功能:但是,可以选择保留 System.Threading 命名空间以实现向后兼容并供将来使用.有关更多 ...
- Linux: 通过命令行上传文件到ftp服务器
url -T fie-name ftp://server-address --user user:password
- 【干货】Kafka 事务特性分析
特性背景 消息事务是指一系列的生产.消费操作可以要么都完成,要么都失败,类似数据库的事务.这个特性在0.10.2的版本是不支持的,从0.11版本开始才支持.华为云DMS率先提供Kafka 1.1.0的 ...