【转载请注明】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的更多相关文章

  1. [LeetCode] Circular Array Loop 环形数组循环

    You are given an array of positive and negative integers. If a number n at an index is positive, the ...

  2. [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 ...

  3. LeetCode 457. Circular Array Loop

    原题链接在这里:https://leetcode.com/problems/circular-array-loop/ 题目: You are given a circular array nums o ...

  4. 【LeetCode】457. Circular Array Loop 环形数组是否存在循环 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题思路 快慢指针 代码 日期 题目地址:https://le ...

  5. Leetcode: Circular Array Loop

    You are given an array of positive and negative integers. If a number n at an index is positive, the ...

  6. [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 ...

  7. [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 ...

  8. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  9. 算法与数据结构基础 - 双指针(Two Pointers)

    双指针基础 双指针(Two Pointers)是面对数组.链表结构的一种处理技巧.这里“指针”是泛指,不但包括通常意义上的指针,还包括索引.迭代器等可用于遍历的游标. 同方向指针 设定两个指针.从头往 ...

随机推荐

  1. [luoguP1736] 创意吃鱼法(DP)

    传送门 f[i][j][0] 表示从右下角到左上角,以(i,j)为起点能延伸的最大值 f[i][j][1] 表示从左下角到右上角,以(i,j)为起点能延伸的最大值 up[i][j] 表示(i,j)上面 ...

  2. vue.js定义一个一级的路由 ----由浅入深

    #### 定义一个路由- 实例化一个路由并设置路由映射表 - 实例化里面第一个参数 routes 路由映射表 - routes 里面参数 - path 路由的路径 - component 路由对应的组 ...

  3. OSX: node中安装zeromq

    1. brew install pkg-config2. brew install zmq3. export PKG_CONFIG_PATH="/usr/local/lib/pkgconfi ...

  4. MVC中从控制器到视图的数据传递方法汇总

    1.ViewData对象概述ViewData是一种字典集合数据,是"视图基类"和"控制器基类"的属性常见用法是在控制器中写入数据,在视图中读取数据ViewDat ...

  5. uva 11212 - Editing a Book(迭代加深搜索 IDA*) 迭代加深搜索

    迭代加深搜索 自己看的时候第一遍更本就看不懂..是非常水,但智商捉急也是没有办法的事情. 好在有几个同学已经是做过了这道题而且对迭代加深搜索的思路有了一定的了解,所以在某些不理解的地方询问了一下他们的 ...

  6. C#根据规则生成6位随机码

    #region 获得6位优惠码 zhy public static string CreatePromoCode(string code) { if (code == "") { ...

  7. Intellij IDEA报错:Could not save application settings: java.io.IOException: java.lang.AssertionError: Unexpected content storage modificat

    Question: i have a message saying "Could not save application settings: java.io.IOException: ja ...

  8. Linux Framebuffer驱动剖析之中的一个—软件需求

    嵌入式企鹅圈将以本文作为2015年的终结篇,以回应第一篇<Linux字符设备驱动剖析>.嵌入式企鹅圈一直专注于嵌入式Linux和物联网IOT双方面的原创技术分享,稍后会公布嵌入式企鹅圈的2 ...

  9. 20170623_oracle基础知识_常见问题

    1 如何配置网络服务?两种连接 Oracle 工具? 1) 打开Net Manager 2) 选择服务命名,点击“+ ”号 3 ) 网络服务名:  remote协议:tcp/ip 主机名:ip地址 端 ...

  10. XAML实例教程系列 - XAML传递参数到值转换类实例 八

    Kevin Fan分享开发经验,记录开发点滴 XAML实例教程系列 - XAML传递参数到值转换类实例 2012-06-28 05:25 by jv9, 508 阅读, 0 评论, 收藏, 编辑 继上 ...