惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Vulnerabilities – Threatpost
U
Unit 42
F
Fortinet All Blogs
aimingoo的专栏
aimingoo的专栏
P
Proofpoint News Feed
F
Full Disclosure
月光博客
月光博客
Engineering at Meta
Engineering at Meta
博客园_首页
The Register - Security
The Register - Security
G
Google Developers Blog
The Cloudflare Blog
博客园 - Franky
K
Kaspersky official blog
A
Arctic Wolf
Scott Helme
Scott Helme
C
Cisco Blogs
Hugging Face - Blog
Hugging Face - Blog
C
Check Point Blog
NISL@THU
NISL@THU
AI
AI
D
DataBreaches.Net
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Stack Overflow Blog
Stack Overflow Blog
Project Zero
Project Zero
The GitHub Blog
The GitHub Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
量子位
Vercel News
Vercel News
T
Tor Project blog
P
Privacy International News Feed
D
Docker
I
Intezer
L
LangChain Blog
P
Proofpoint News Feed
Security Latest
Security Latest
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Threatpost
博客园 - 聂微东
AWS News Blog
AWS News Blog
Martin Fowler
Martin Fowler
P
Privacy & Cybersecurity Law Blog
V
V2EX
Last Week in AI
Last Week in AI
C
Cybersecurity and Infrastructure Security Agency CISA
The Hacker News
The Hacker News
T
Tenable Blog
Blog — PlanetScale
Blog — PlanetScale
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Tailwind CSS Blog

博客园 - 司徒正美

leetcode 91. Decode Ways leetcode 1214 Two Sum BSTs leetcode 213 House Robber II leetcode 198 House Robber I leetcode 986. Interval List Intersections leetcode 869. Reordered Power of 2 leetcode 925. Long Pressed Name leetcode 1093. Statistics from a Large Sample leetcode 881. Boats to Save People leetcode 977. Squares of a Sorted Array leetcode 844. Backspace String Compare leetcode 1032. Stream of Characters leetcode 1023. Camelcase Matching leetcode 745 Prefix and Suffix Search leetcode 720. Longest Word in Dictionary leetcode 692. Top K Frequent Words leetcode 677. Map Sum Pairs leetcode 676. Implement Magic Dictionary leetcode 648. Replace Words
leetcode 457. Circular Array Loop
司徒正美 · 2020-01-06 · via 博客园 - 司徒正美

先回顾一下链表的类似问题

leetcode 141 判定链表是否有环

慢指针slowPtr每次后移1个结点。快指针fastPtr每次后移2个结点

   function isLinkedListContainsLoop( head){
        if(head==null){
            return false;
        }
        let slowPtr=head;
        let fastPtr=head;
        while(slowPtr.next!=null && fastPtr.next.next!=null){
            slowPtr=slowPtr.next;
            fastPtr=fastPtr.next.next;
            if(slowPtr==fastPtr){
                return true;
            }
        }
        return false;
    }

LeetCode 142 找出环的入口点(起点)

当fast按照每次2步,slow每次一步的方式走,发现fastPtr和slowPtr重合,确定了单向链表有环路。接下来,让slowPrt回到链表的头部,然后slowPtr和fastPtr各自从自己的位置(fastPtr从两个指针相遇的位置position出发)沿着链表出发,每次步长1,那么当fastPtr和slowPtr再次相遇的时候,就是环路的入口了。

function findLinkedListLoopBegin(head) {
            if (head == null) {
                return null;
            }
            let slowPtr = head;
            let fastPtr = head;
            let isLinkedListContainsLoop = false;
            while (slowPtr.next != null && fastPtr.next.next != null) {
                slowPtr = slowPtr.next;
                fastPtr = fastPtr.next.next;
                if (slowPtr == fastPtr) {
                    isLinkedListContainsLoop = true;
                    break;
                }
            }
            if (isLinkedListContainsLoop) {
                slowPtr = head;
                let count = 1
                while (slowPtr == fastPtr) {
                    slowPtr = slowPtr.next;
                    fastPtr = fastPtr.next;
                    count++
                }
                return slowPtr;
            }
            return null;
        }

设环长为n,非环形部分长度为m,当第一次相遇时显然slow指针行走了 m+An+k(A表示slow行走了A圈。附:An 是因为如果环够大,则他们的相遇需要经过好几环才相遇)。fast行走了 m+B*n+k。

上面我们说了slow每次行走一步,fast每次行走两步,则在同一时间,fast行走的路程是slow的两倍。假设slow行走的路程为S,则fast行走的路程为2S。

用fast减去slow可得:

S=(B-A)*n

很显然这意味着当slow和fast相遇时他们走过的路程都为圈长的倍数。

接下来,将slow移动到起点位置,如下图:

然后每次两个指针都只移动一步,当slow移动了m,即到达了环的起点位置,此时fast总共移动了 2S+m。 考虑到S为环长的倍数,可以理解为:fast先从链表起点出发,经过了m到达环的起点,然后绕着环移动了几圈,最终又到达环的起点,值为2S+m。所以fast最终必定处在环的起点位置。即两者相遇点即为环的起点位置。

衍生问题2,求环的大小(长度)

当fast按照每次2步,slow每次一步的方式走,发现fastPtr和slowPtr重合,确定了单向链表有环路。接下来,让slowPrt不动,fast 绕着环移动,每次移动一步,计数count加1,当两指针再次相遇时,count即是环的大小

回归原题,也是用快慢节点

function circularArrayLoop(nums) {
            let n = nums.length;
            if (n <= 1) {
                return false
            }
            function getNext(i) {
                return (i + nums[i] + n) % n
            }

            for (let i = 0; i < n; i++) {
                let slow = i, fast = getNext(i);
                //确保总是朝着一个方向前进
                while (nums[slow] * nums[i] > 0 && nums[fast] * nums[i] > 0) {
                    if (slow == fast) {
                        //判断是否只有一个元素
                        if (slow == getNext(slow)) {
                            break;
                        }
                        return true;
                    }
                    slow = getNext(slow);
                    fast = getNext(fast);//快指针每次走两步
                    if (nums[fast] * nums[i] < 0) {//如果方向反了
                        break;
                    }
                    fast = getNext(fast);
                }
            }
            return false;
        }

        console.log(circularArrayLoop([2, -1, 1, 2, 2]))
        console.log(circularArrayLoop([-1, 2]))
        console.log(circularArrayLoop([-2, 1, -1, -2, -2]))