↓快轉到主要內容

Hash-Table

LeetCode 3071: Minimum Operations to Write the Letter Y on a Grid
·1 分鐘
LeetCode Medium Array Hash-Table Matrix Counting
LeetCode 3071 解題筆記:分別數 Y 上和 Y 外每個值的次數,枚舉 Y 的值 a 和背景的值 b(a != b),取最少要改的格子數
LeetCode 2099: Find Subsequence of Length K With the Largest Sum
·1 分鐘
LeetCode Easy Array Hash-Table Sorting Heap-Priority-Queue
LeetCode 2099 解題筆記:選出最大的 k 個數(連同 index),再按 index 排回原本的順序
LeetCode 2357: Make Array Zero by Subtracting Equal Amounts
·1 分鐘
LeetCode Easy Array Hash-Table Greedy Sorting
LeetCode 2357 解題筆記:每次操作會讓最小的正數那一群變成 0,所以次數就是不同的正數有幾種
LeetCode 264: Ugly Number II
·1 分鐘
LeetCode Medium Hash-Table Math Dynamic-Programming Heap-Priority-Queue
LeetCode 264 解題筆記:三個指針分別指向下一個要乘 2、3、5 的醜數,每次取最小的,相等的指針都要前進
LeetCode 187: Repeated DNA Sequences
·1 分鐘
LeetCode Medium Hash-Table String Bit-Manipulation Sliding-Window
LeetCode 187 解題筆記:把每個長度 10 的子字串丟進雜湊表計數,出現超過一次的就是答案
LeetCode 142: Linked List Cycle II
·1 分鐘
LeetCode Medium Hash-Table Linked-List Two-Pointers Floyds-Cycle-Finding-Algorithm
LeetCode 142 解題筆記:快慢指針相遇後,一個回到 head,兩個都一次一步,再相遇的地方就是環的入口
LeetCode 229: Majority Element II
·1 分鐘
LeetCode Medium Array Hash-Table Sorting Counting
LeetCode 229 解題筆記:超過 n / 3 的數最多兩個;Boyer-Moore 維護兩個候選人,最後再掃一次確認次數
LeetCode 106: Construct Binary Tree from Inorder and Postorder Traversal
·1 分鐘
LeetCode Medium Array Hash-Table Divide-and-Conquer Tree
LeetCode 106 解題筆記:後序的最後一個是根;從後序尾巴往前取根時,要先建右子樹再建左子樹
LeetCode 133: Clone Graph
·1 分鐘
LeetCode Medium Hash-Table Depth-First-Search Breadth-First-Search Graph
LeetCode 133 解題筆記:DFS 時用雜湊表記住舊節點對應的新節點,遇到已經複製過的就直接回傳,避免無限遞迴;空圖回傳 None
LeetCode 208: Implement Trie (Prefix Tree)
·1 分鐘
LeetCode Medium Hash-Table String Design Trie
LeetCode 208 解題筆記:每個節點是 children 字典加一個 is_end 標記;insert、search、startsWith 都是沿著字元往下走
LeetCode 138: Copy List with Random Pointer
·1 分鐘
LeetCode Medium Hash-Table Linked-List
LeetCode 138 解題筆記:第一次走過建立舊節點到新節點的字典,第二次接上 next 和 random;字典裡先放 None -> None
LeetCode 105: Construct Binary Tree from Preorder and Inorder Traversal
·2 分鐘
LeetCode Medium Array Hash-Table Divide-and-Conquer Tree
LeetCode 105 解題筆記:前序的第一個是根,在中序裡找到根的位置就知道左子樹有幾個節點;用字典查位置,O(n)