Floyd Cycle Detection
Floyd判圈算法能在O(n)时间复杂度内判断迭代函数或链表中是否有环,并求出环的长度与起点

判断环存在
通常采用快慢指针的方式来判断环是否存在
从绿色起点G开始,快指针每次走2步,慢指针每次走1步,当链表未遍历完且快慢指针相遇且时说明链表中存在环
很容易证明:假定链表存在环,那么快慢指针一定会在环内打转,由于存在速度差,则快慢指针一定会相遇
环的长度
若快指针和慢指针在环上的红点R第一次相遇, 则让快指针不动,慢指针继续走并同时从0开始记录步数,则再次相遇时,步数即为环的长度
环的起点
存在环的情况下,假定环的长度为C
同时假定两指针同时从绿点G出发,蓝点B为环的起点,distance(G,B) = x,
,distance(B,R) = d,很容易证明此时慢指针刚好走了C-d步
(不加周期nC,因为慢指针如果在环上走过超过一圈,那么快指针走过超过两圈,则在此之前两指针一定会有一次相遇,这次就不是第一次相遇,详细可自行推倒)
令慢指针走的长度为L,则
L = x + C - d,
2L = nC + x + C - d(n>0, 否则L = 0)
联立可得 nC = x + C - d = L
移项得 x = nC - C + d
如果此时有一个指针S2从绿点出发,和慢指针S1速度相同,则S2与S1相遇时,相遇点即为环的起点
证明:由上式可知,当S2走了x步到达蓝点时,S1正好先走d步到达蓝点,然后再进行非负数个循环,此时S1与S2都在蓝点,得证
例题
141. Linked List Cycle(判断链表是否存在环)
142. Linked List Cycle II(判断链表是否存在环并找出环的起点)
202. Happy Number
// 142题
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
ListNode* slow, *quick, *t;
t = slow = quick = head;
while(quick != NULL && quick->next != NULL){
slow = slow->next;
quick = quick->next->next;
if(slow == quick){
slow = head;
while(slow != quick){
quick = quick->next;
slow = slow->next;
}
return slow;
}
}
return NULL;
}
};
// 202题,除了数学方法和基本记忆查询外,还可以采用迭代的方式,这是在迭代函数上进行的
class Solution {
public:
int digitSquareSum(int n){
int r = 0, b;
while(n != 0) {
b = n % 10;
n = n / 10;
r += b * b;
}
return r;
}
bool isHappy(int n) {
int slow, fast;
slow = n;
fast = n;
do {
slow = digitSquareSum(slow);
fast = digitSquareSum(fast);
fast = digitSquareSum(fast);
} while(slow != fast);
if(slow == 1) return true;
return false;
}
};
Floyd Cycle Detection的更多相关文章
- Floyd判圈算法 Floyd Cycle Detection Algorithm
2018-01-13 20:55:56 Floyd判圈算法(Floyd Cycle Detection Algorithm),又称龟兔赛跑算法(Tortoise and Hare Algorithm) ...
- Floyd's Cycle Detection Algorithm
Floyd's Cycle Detection Algorithm http://www.siafoo.net/algorithm/10 改进版: http://www.siafoo.net/algo ...
- SGU 455 Sequence analysis(Cycle detection,floyd判圈算法)
题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=455 Due to the slow 'mod' and 'div' operati ...
- Floyd判圈算法
Floyd判圈算法 leetcode 上 编号为202 的happy number 问题,有点意思.happy number 的定义为: A happy number is a number defi ...
- FLOYD判圈
转载一篇博客:http://blog.csdn.net/javasus/article/details/50015687 Floyd判圈算法(Floyd Cycle Detection Algorit ...
- Floyd 循环检测算法(快慢指针法/龟兔指针法)
Floyd Cycle Detection Algorithm Floyd Cycle Detection Algorithm,即 Floyd 循环检测算法,又称快慢指针法.龟兔指针法.该算法用于 ...
- Pollard's rho algorithm和涉及到的两个循环检测算法
0. 简单介绍 Pollard的\(\rho\)算法是John Pollard在1975年发明的,用于分解质因数[1].假定被分解的数为N,N的最小的质因数为\(p(p\ne N)\),那么该算法可以 ...
- LEETCODE —— Linked List Cycle [Floyd's cycle-finding algorithm]
Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it ...
- Floyd判断环算法总结
Floyd判断环算法 全名Floyd’s cycle detection Algorithm, 又叫龟兔赛跑算法(Floyd's Tortoise and Hare),常用于链表.数组转化成链表的题目 ...
随机推荐
- Jcaptcha组件和kaptcha组件实现验证码
- leetcode-mid-math - 69. Sqrt(x)-NO
mycode memory error class Solution(object): def mySqrt(self, x): """ :type x: int : ...
- 使用collection:分段查询结果集
1.在人员接口书写方法 public List<Employee> getEmpsByDeptId(Integer deptId); 2在人员映射文件中进行配置 <!-- publi ...
- 二、启动一款app演示
一.下载aapt包 1. aapt即Android Asset Packaging Tool,在SDK的build-tools目录下.该工具可以查看apk包名和launcherActivity 2.打 ...
- Bader分析
一.背景 理查德·贝德(Richard Bader)开发了一种将分子分解为原子的直观方法.他对原子的定义纯粹基于电子电荷密度.Bader使用所谓的零磁通表面来划分原子.零通量表面是2D表面,其上电荷密 ...
- 【C++进阶:atoi()与itoa()】
两种函数: atoi 把字符串转为整形: itoa 整形转为字符串: https://www.cnblogs.com/bluestorm/p/3168719.html
- 【漏洞复现】局域网 ARP 中间人攻击 获取他人账号密码
日期:2019-07-18 14:24:42 更新: 作者:Bay0net 介绍:如何在局域网内,窃取其他用户的账号密码? 0x01. 漏洞环境 攻击工具 arpspoof 基本用法: arpspoo ...
- 关于Laravel Gate 和 Policies 的理解
他们的关系是什么? Gate 派生出 Policies 原因见:Providers/AuthServiceProvider.php 文件 其中有注册policies方法: public functio ...
- Linux下安装Elasticsearch6.5
1.安装JDK8(Elastic 需要 Java 8 环境) 1)下载jdk8文件:http://www.oracle.com/technetwork/java/javase/downloads/jd ...
- 记一次Python pip安装失败的总结
pip 安装失败时,可能换此方法可解决1.升级pip版本,这个一般会主动提示python3 -m pip install --upgrade pip2.修改pip源,默认的pip源速度实在无法忍受,或 ...