GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Update 003.cpp · hfl15/algorithm@31d5b0b

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • sword_offer/src
    • 003.cpp
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎sword_offer/src/003.cpp‎

Copy file name to clipboardExpand all lines: sword_offer/src/003.cpp
+5-6Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@
66
77
[解析]
88
9-
1. 一个快指针和一个慢指针,在环中,快指针肯定会反向追上慢指针。(假想两个人在操场上跑步,快的人的速度时慢的人的两倍,必将会与慢的人几次相遇)
9+
1. 一个快指针和一个慢指针,在环中,快指针肯定会反向追上慢指针。(假想两个人在操场上跑步,快的人的速度是慢的人的两倍,必将会与慢的人几次相遇)
1010
2. 两个指针重合的地方必定在环内,这个时候可以数一数环内有几个结点。只需要动其中一个指针遍历一遍又回到重合位置即可统计出环内结点数,假设为 k 。
11-
3. 这时快慢指针 pSlow 和 pFast 从链表头开始。pFast 先比 pSlow 走 k 步,
12-
如果给结点编号,从数值上看有,pSlow+k = pFast,然后两个指针同步没次向前移动一步,
13-
那么当 pSlow 到达环的入口节点时,pFast = pSlow+k,因为环只有 k 个节点,pFast
14-
相当于又回到了环的开始即入口节点,两指针重合。
11+
又设链表总共有 n 个节点,此时不在环内的节点有 n-k 个。
12+
3. 这时快慢指针 pSlow 和 pFast 从链表头开始。pFast 先比 pSlow 走 k 步,如果给结点编号,从数值上看有,pSlow+k = pFast。
13+
此后 pFlast 和 pSlow 同步向前逐步移动,两者相遇的点即为入口。此时 pSlow 走过了 n-k 步,指向入口结点;pFast 走过 k+(n-k)= n,刚好把所有节点都遍历一遍回到入口节点。
1514
*/
1615

1716
#include <iostream>
@@ -69,4 +68,4 @@ class Solution{
6968
int main()
7069
{
7170
return 0;
72-
}
71+
}

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.