Re: [LeetCode] 刷到面試 Grind169 C++

看板Marginalman作者 (彗星雷達)時間2年前 (2023/03/19 16:19), 編輯推噓1(101)
留言2則, 2人參與, 2年前最新討論串13/14 (看更多)
141. Linked List Cycle easy題 兩種解法 哈希表跟雙指針 有趣 哈希 class Solution { public: bool hasCycle(ListNode *head) { unordered_set<ListNode*> visited; while(head){ if(visited.count(head)) return true; visited.insert(head); head=head->next; } return false; } }; 雙指針 class Solution { public: bool hasCycle(ListNode *head) { auto fast=head; auto slow=head; while(fast){ if(!fast->next) return false; fast=fast->next->next; slow=slow->next; if(slow==fast) return true; } return false; } }; -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 223.137.157.124 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1679213956.A.20A.html

03/19 16:19, 2年前 , 1F
大師 瘋狂刷題
03/19 16:19, 1F

03/19 16:23, 2年前 , 2F
不然我要失業了
03/19 16:23, 2F
文章代碼(AID): #1a5iM48A (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1a5iM48A (Marginalman)