Hash-Table
LeetCode 763: Partition Labels
·1 分鐘
LeetCode
Medium
Hash-Table
Two-Pointers
String
Greedy
LeetCode 763 解題筆記:先記每個字母最後出現的位置,掃描時把這段的結尾延伸到最遠,走到結尾就切
LeetCode 2385: Amount of Time for Binary Tree to Be Infected
·1 分鐘
LeetCode
Medium
Hash-Table
Tree
Depth-First-Search
Breadth-First-Search
Binary-Tree
LeetCode 2385 解題筆記:把樹轉成無向圖,從 start 做 BFS,層數減一就是分鐘數
LeetCode 460: LFU Cache
·2 分鐘
LeetCode
Hard
Hash-Table
Linked-List
Design
Doubly-Linked-List
LeetCode 460 解題筆記:key → 節點、freq → 雙向鏈結串列,再記一個 min_freq;同頻率裡用 LRU 決定淘汰誰
LeetCode 146: LRU Cache
·2 分鐘
LeetCode
Medium
Hash-Table
Linked-List
Design
Doubly-Linked-List
LeetCode 146 解題筆記:雜湊表找節點、雙向鏈結串列維護使用順序;get 和更新既有的 key 都要移到最前面
LeetCode 889: Construct Binary Tree from Preorder and Postorder Traversal
·1 分鐘
LeetCode
Medium
Array
Hash-Table
Divide-and-Conquer
Tree
Binary-Tree
LeetCode 889 解題筆記:stack 存目前這條路徑,堆頂等於 postorder 的下一個就代表子樹完成要 pop;新節點先接左邊
LeetCode 863: All Nodes Distance K in Binary Tree
·1 分鐘
LeetCode
Medium
Hash-Table
Tree
Depth-First-Search
Breadth-First-Search
Binary-Tree
LeetCode 863 解題筆記:先把樹轉成無向圖(補上往父節點的邊),再從 target BFS 走 k 步
LeetCode 652: Find Duplicate Subtrees
·1 分鐘
LeetCode
Medium
Hash-Table
Tree
Depth-First-Search
Binary-Tree
LeetCode 652 解題筆記:後序把每棵子樹編碼,出現第二次時加入答案;字串編碼最差 O(n^2),改成 (值, 左 id, 右 id) 編號可到 O(n)
LeetCode 314: Binary Tree Vertical Order Traversal
·1 分鐘
LeetCode
Medium
Hash-Table
Tree
Depth-First-Search
Breadth-First-Search
Sorting
Binary-Tree
LeetCode 314 解題筆記:BFS 帶著欄位編號,用 BFS 才能保證同一欄由上到下、同一列由左到右
String 字串處理
·4 分鐘
演算法
String
Simulation
Hash-Table
用 list 組字串的模擬、在每個位置嘗試比對的解析、長度 k 的所有子字串、字元分類計數、固定起點的平衡子字串判斷,以及切成單字再處理
Hash Table 雜湊表
·4 分鐘
演算法
Hash-Table
Counting
Array
計數與「次數的次數」、把陣列本身當雜湊表(負號標記)、多個 key 同時計數(列、欄、對角線)、固定左端點增量計數,以及用正規化後的 key 分組
LeetCode 305: Number of Islands II
·2 分鐘
LeetCode
Hard
Union-Find
Hash-Table
Array
LeetCode 305 解題筆記:每加一格陸地島數加 1,跟四周已經是陸地的格子合併,每成功合併一次減 1;重複的位置要跳過
LeetCode 2584: Split the Array to Make Coprime Products
·2 分鐘
LeetCode
Hard
Math
Number-Theory
Sieve
Hash-Table
Array
LeetCode 2584 解題筆記:兩段乘積互質 ⇔ 沒有質因數同時出現在兩段;記下每個質因數最後出現的 index,從左往右擴張 reach,reach == i 時就能在 i 後面切