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)是面对数组.链表结构的一种处理技巧.这里“指针”是泛指,不但包括通常意义上的指针,还包括索引.迭代器等可用于遍历的游标. 同方向指针 设定两个指针.从头往 ...
随机推荐
- STL源码剖析 学习笔记 MiniSTL
https://github.com/joeyleeeeeee97 目录: 第二章 空间适配器 第三章 迭代器 第四章 序列式容器(vector,list,deque,stack,heap,prior ...
- jq仿ps颜色拾取功能-js颜色拾取
1.效果展示 2.html代码:index.html <!DOCTYPE html> <html lang="en"> <head> <m ...
- 【Nginx】进程模型
转自:网易博客 服务器的并发模型设计是网络编程中很关键的一个部分,服务器的并发量取决于两个因素,一个是提供服务的进程数量,另外一个是每个进程可同时处理的并发连接数量.相应的,服务器的并发模型也由两个部 ...
- [Vue-rx] Share RxJS Streams to Avoid Multiple Requests in Vue.js
Splitting a stream into multiple streams causes new subscriptions. You can think of new subscription ...
- Object-C--->Swift之(十一)属性观察者
属性观察者机制能让程序在属性被赋值时获得运行代码的机会,用来监视属性的除初始化之外的属性值变化,当属性值发生改变时能够对此作出响应 详细包含两个特殊的回调方法: willSet(newValue):被 ...
- AOP设计场景
AOP就是切面编程的一个思想,当然完毕一项编码任务,发现有些东西是反复工作,这时就能够考虑使用AOP编程.把一些共性的东西交给它来完毕,我们仅仅关心业务逻辑的东西,最精彩用的场景有两种: 一,控制数据 ...
- ARP病毒查找与防范
笔者是一名大学生,学的是网络project. 自然接触的网络就多了.当然免不了做些坏事,嘿嘿. 比如我经经常使用在网上学习的知识去測验一下我自己的电脑或者同学啥的,都是一些小打小闹. 无伤大雅,知道有 ...
- jqury+css实现可弹出伸缩层
1.使用可弹出伸缩窗调整了之前的页面布局,使用这样的布局使整个界面看起来更加清爽也更简洁 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L ...
- mac 浏览器解决跨域问题
Chrome:命令行执行如下命令open -a Google\ Chrome --args --disable-web-security出现如下提示,说明已经开启: Safari: open -a ' ...
- oninput 中文输入
使用 input 监听 input 中文输入,会被每次输入的字母打断,下面就解决这个问题 $('input') .off() .on('input',function(){ if($(this).pr ...