https://leetcode.com/problems/circular-array-loop/

题目蛮难的,有一些坑。

前后两个指针追赶找环的方法,基本可以归结为一种定式。可以多总结。

package com.company;

class Solution {
// 参考了这里的解法
// https://discuss.leetcode.com/topic/66894/java-slow-fast-pointer-solution
public boolean circularArrayLoop(int[] nums) {
for (int i=; i<nums.length; i++) {
if (nums[i] == ) {
continue;
}
int j = i, k = getIndex(i, nums);
// 检查同方向
while (nums[i] * nums[k] > && nums[i] * nums[getIndex(k, nums)] > ) {
if (j == k) {
// 碰到了
// 检查是否只有一个元素
if (j == getIndex(j, nums)) {
break;
}
return true;
}
// 一前一后两个指针
j = getIndex(j, nums);
k = getIndex(getIndex(k, nums), nums);
}
// 把已经走过的,置为0
j = i;
k = nums[j];
// 但要注意不同方向的不要置为0
while (nums[j]*k > ) {
int tmp = getIndex(j, nums);
nums[j] = ;
j = tmp;
}
}
return false;
} // 下面这个函数也是参考了上面Discuss里面的解法
private int getIndex(int i, int[] nums) {
int n = nums.length;
int j = (i + nums[i]) >= ? (i + nums[i]) % n : n + ((i + nums[i]) % n);
return j;
}
} public class Main { public static void main(String[] args) throws InterruptedException { System.out.println("Hello!");
Solution solution = new Solution(); // Your Codec object will be instantiated and called as such:
int[] nums = {, -, , -, -};
boolean ret = solution.circularArrayLoop(nums);
System.out.printf("ret:%b\n", ret); System.out.println(); } }

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. Leetcode0457--Circular Array Loop

    [转载请注明]https://www.cnblogs.com/igoslly/p/9339478.html class Solution { public: bool circularArrayLoo ...

  8. 132-pattern(蛮难的)

    https://leetcode.com/problems/132-pattern/ 下面是我的做法.后来又看了一个提示: https://discuss.leetcode.com/topic/678 ...

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

随机推荐

  1. python - web自动化测试 - 元素操作 - 等待

    # -*- coding:utf-8 -*- ''' @project: web学习 @author: Jimmy @file: wait.py @ide: PyCharm Community Edi ...

  2. Python-S9-Day101 Vue-cli

    01 昨天内容回顾 02 音乐播放器计算属性方法和组件创建 03 Vue-cli项目生成 04 模板中组件的使用 01 昨天内容回顾 1.1 {{xxx}}模板语法,插值,简单的运算: 1.2 指令系 ...

  3. [oldboy-django][2深入django]学生管理(Form)-- 编辑(设置input标签属性,设置input标签默认显示值,设置input的类型)

    1 django 后台实现设置input标签属性,设置input标签默认显示值,设置input输入框类型 # Form生成html标签 a. 通过Form生成Input输入框,Form标签,以及sub ...

  4. 直接插入排序(java实现)

    这几天看排序算法,网上大多数排序算法的解释都是“过于专业”,导致一时半会看不明白到底在说什么玩意.现在总结下几大排序算法(java实现) 1.直接插入排序 说的简单点,就是一组无序序列{A1,A2,. ...

  5. rule.xml属性概念

    # tableRule <tableRule name="rule1"> <rule> <columns>id</columns> ...

  6. EasyUi DataGrid 获取选中行的checkbox

    EasyUi DataGrid 获取选中行的checkbox var checked = $(":checkbox[name=ID]:checked"); console.log( ...

  7. Vue2.0 - 自定义指令 vue-directive

    Vue.directive('指令',function(el,binding,vnode){ el.style='color:'+binding.value;}); el : 指令所绑定的元素,可以用 ...

  8. iOS属性文字NSAttributedString

    它本身是一个Foundation框架的类, 但如果要使用它主要用到了UIKit框架中的NSAttributedString中的一些常量字符串 ----------------------------- ...

  9. windows api 程序

    #include "StdAfx.h" #include<windows.h> #include<mmsystem.h> LRESULT CALLBACK ...

  10. hdu 2243 考研路茫茫——单词情结 AC自动机 矩阵幂次求和

    题目链接 题意 给定\(N\)个词根,每个长度不超过\(5\). 问长度不超过\(L(L\lt 2^{31})\),只由小写字母组成的,至少包含一个词根的单词,一共可能有多少个? 思路 状态(AC自动 ...