circular-array-loop(蛮难的)
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(蛮难的)的更多相关文章
- [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 ...
- Leetcode0457--Circular Array Loop
[转载请注明]https://www.cnblogs.com/igoslly/p/9339478.html class Solution { public: bool circularArrayLoo ...
- 132-pattern(蛮难的)
https://leetcode.com/problems/132-pattern/ 下面是我的做法.后来又看了一个提示: https://discuss.leetcode.com/topic/678 ...
- [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 ...
随机推荐
- linux简单授权
linux授权:r: readw: writex:executech:change b=byte1byte=8 bitsu=user ownerg=groupo=othera=all_ _ _ _ _ ...
- live 555 freebsd 或centos 7.4 实现代理视频直播服务
live 555 freebsd 或centos 7.4 实现代理视频直播服务 the live555 media server 在线直播服务器 关于此服务器 此服务是一个无安全的rtsp服 ...
- 实战小项目之RTMP流媒体演示系统
项目简介 windows下使用基于Qt对之前的RtmpApp进行封装与应用,单独功能使用线程执行,主要包括以下几个功能: 视频下载 推送文件 推送摄像头数据或者桌面 基于libvlc的播放器 视频下载 ...
- shell执行mysql的脚本(包括mysql执行shell脚本)
在Shell中执行mysql的脚本,这里介绍比较容易使用的一种方法 首先写好sql的脚本,后缀为.sql,比如 sql_file.sql:内容如下 #这是SQL的脚本create table if n ...
- Lyft Level 5 Challenge 2018-Final Round(Open Div.2) B. Taxi drivers and Lyft
http://codeforces.com/contest/1075/problem/B Palo Alto is an unusual city because it is an endless c ...
- BZOJ 2818: Gcd(欧拉函数)
GCDDescription 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. Input 一个整数N Output 如题 Sample Input 4 ...
- java课后作业-5
作业一:请写一个类,在任何时候都可以向它查询“你已经创建了多少个对象?“ 程序源代码; public class Lei { /** * @param args */ public static vo ...
- git删除远程仓库文件
1 首先将远程代码pull到本地,保持本地仓库跟远端仓库同步 git pull git@github.com:lanleilin/lanGallery.git//使用SSH的方式 2 然后使用git ...
- python(7)-- 文件I/O
1 打印到屏幕:print 语句.你可以给它传递零个或多个用逗号隔开的表达式.此函数把你传递的表达式转换成一个字符串表达式,并将结果写到标准输出,eg:print "Python 是一个非常 ...
- sql语句like的用法 有些正则表达式可以通过like实现
原文发布时间为:2010-10-28 -- 来源于本人的百度文章 [由搬家工具导入] 在SQL结构化查询语言中,LIKE语句有着至关重要的作用。LIKE语句的语法格式是:select * from 表 ...