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. 成都大学CTF 网络攻防演练平台 WP

    web1 输入框那里鼠标右键,审查元素,删除maxlength web2 http://ctf.cdusec.org:8082/web2/?cdusec=tql web3 同上,用火狐hackbar或 ...

  2. 性能测试工具—Jmeter

    Jmeter视频教程: 在我要自学网搜索:关键字即可

  3. android TranslateAnimation动画执行时的坐标获取。

    android 的Tween动画并不会改变控件的属性值,比如以下测试片段: 定义一个从屏幕右边进入,滚动到屏幕左边消失的一个TranslateAnimation动画: <?xml version ...

  4. selenium webdriver——元素定位

    元素定位: >>WebDriver提供了八种元素定位方法,在Python语言中,所对应的方法如下: >>id属性定位:有唯一性 find_element_by_id(" ...

  5. 【转】UGUI VS NGUI

    原文:http://gad.qq.com/college/articledetail/7191053 注[1]:该比较是基于15年-16年期间使用NGUI(3.8.0版本)与UGUI(4.6.9版本) ...

  6. easyui在datagrid只想选择一条

    <table class="" id="jgrid" data-options="fitColumns:true,rownumbers: tru ...

  7. Callable、Future、FutureTask_笔记

    参考:http://blog.csdn.net/javazejian/article/details/50896505 1.Callable<V>接口 Runnable接口 public ...

  8. BZOJ 1010: [HNOI2008]玩具装箱toy(DP+斜率优化)

    [HNOI2008]玩具装箱toy Description P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再放到一种特殊 ...

  9. kubeadm安装k8s测试环境

    目标是搭建一个可测试的k8s环境,使用的工具 kubeadm, 最终一个master节点(非高可用),2个node节点. 环境以及版本 Centos7.3 kubeadm 1.11.1 kubelet ...

  10. springboot 2.0配置集成thymeleaf的坑

    Servlet.service() for servlet [dispatcherServlet] in context with path [] java.lang.NoClassDefFoundE ...