演算法與資料結構速查表

共 129 份速查表,依主題分組,並以「在 FAANG 軟體工程面試中實際出現的頻率」排序。建議先讀 從這裡開始 的閱讀順序,下方的完整目錄則供查閱用。

星等代表什麼

  • Priority 5 of 5 — Must know — expect it in almost every loop必備幾乎每一輪 FAANG 面試都會遇到 — 應該要能默寫出來。
  • Priority 4 of 5 — High value — a gap here costs you rounds高價值出現頻率高到「這裡有缺口就會掉關」的程度。
  • Priority 3 of 5 — Worth knowing — usually a variant of a must-know pattern值得會固定會出現,多半是五星模式的變形。
  • Priority 2 of 5 — Niche — read once, revisit only if a company is known to ask冷門面試少見 — 讀過一次即可,除非目標公司已知會問。

每份速查表內部的章節也標了同一套星等,所以即使面對四千行的文件,也能一眼看出哪些模板是非背不可的。

從這裡開始

12 份速查表,依閱讀順序排列。讀完這一串,就涵蓋了程式面試絕大多數會問到的內容。

  1. LeetCode 模式指南Priority 5 of 5 — Must know — expect it in almost every loop分診:拿到一題,它想要的是哪個模式?
  2. 複雜度速查表 — 經典演算法與資料結構Priority 5 of 5 — Must know — expect it in almost every loop每一輪面試都會被要求說出複雜度。
  3. 陣列Priority 5 of 5 — Must know — expect it in almost every loop原地改寫,以及「用索引當雜湊」的技巧。
  4. Hash Map Cheatsheet(雜湊表速查)Priority 5 of 5 — Must know — expect it in almost every loop面試中使用率最高的單一資料結構。
  5. 雙指標Priority 5 of 5 — Must know — expect it in almost every loop陣列/字串上的相向指標與尾隨指標。
  6. 滑動視窗Priority 5 of 5 — Must know — expect it in almost every loop依條件擴張與收縮 — 自成一個題型家族。
  7. Binary SearchPriority 5 of 5 — Must know — expect it in almost every loop邊界模板,外加對答案做二分搜尋。
  8. 二元樹Priority 5 of 5 — Must know — expect it in almost every loopDFS 的狀態如何往下傳、又如何往上收。
  9. BFS(廣度優先搜尋)Priority 5 of 5 — Must know — expect it in almost every loop層序走訪,以及它為什麼能給出最短路徑。
  10. DFS(深度優先搜尋)Priority 5 of 5 — Must know — expect it in almost every loop每一道圖/樹題目都會重複用到的遞迴模式。
  11. 堆積與優先佇列Priority 5 of 5 — Must know — expect it in almost every loopTop-k 與「反覆取出最好的那個」這類問題。
  12. 動態規劃(DP)Priority 5 of 5 — Must know — expect it in almost every loop最大也最難的五星區塊 — 時間要留最多給它。

完整目錄

陣列與字串21 份

面試中出現頻率最高的一塊。先把這裡的四大模式家族練熟,再談其他。

陣列

Priority 5 of 5 — Must know — expect it in almost every loop

陣列的基本功——原地改寫、旋轉、分割,以及「用索引當雜湊」的招式。它擁有這些操作本身;至於各個模式家族(視窗、指標、前綴和)各有自己的檔案。

滑動視窗

Priority 5 of 5 — Must know — expect it in almost every loop

依條件伸縮的視窗 — 固定大小、可變大小、至多 k 個,以及用相減湊出的恰好 k 個;擴張/收縮迴圈與六個標準視窗模板都歸這裡管。

字串演算法與操作

Priority 5 of 5 — Must know — expect it in almost every loop

日常字串目錄 — 字元層級的雙指標掃描、頻率與 anagram 簽章、run-length 分組、切詞、解析與原地改寫 — 至於題解倉庫、語言層級的字串 API、回文、子字串搜尋與雙序列 DP,則各自有自己的一張表。

雙指標

Priority 5 of 5 — Must know — expect it in almost every loop

陣列與字串上的雙指標家族:兩端向內收斂、快慢指標、從中心擴張、讀寫分離的原地重寫,每一種各給一個標準模板;依條件伸縮的視窗題不在這裡。

陣列 — 題目實作

Priority 4 of 5 — High value — a gap here costs you rounds

array.md 背後的題解倉庫:十三題真正在考「原地改寫陣列」或「拿索引當儲存空間」的題目,依各自用到的技巧分組。

Matrix — 實戰題解

Priority 4 of 5 — High value — a gap here costs you rounds

matrix.md 背後的題解庫:十七道題,依各自吃的幾何性質或技巧分組 —— 走訪順序、原地變換、階梯搜尋、格子搜尋、二維 DP,以及列對壓縮。

Matrix Data Structure

Priority 4 of 5 — High value — a gap here costs you rounds

把二維格子當成一個獨立主題來看 —— 走訪的幾何(螺旋、對角、旋轉、轉置)、原地標記,以及索引↔座標的換算。

N Sum

Priority 4 of 5 — High value — a gap here costs you rounds

專門講 k-sum 這個題型家族:從 2Sum 到 kSum、先排序再雙向收斂的遞迴寫法,以及重複值的處理。

Palindrome (回文)

Priority 4 of 5 — High value — a gap here costs you rounds

回文題型家族:中心擴散、雙指標驗證、回文 DP、Manacher,以及怎麼判斷一題該用哪一種。

Prefix Sum (前綴和)

Priority 4 of 5 — High value — a gap here costs you rounds

前綴和/累積和 — 子陣列和、二維前綴和、前綴和搭配雜湊表做計數。

前綴和 — 進階模板

Priority 4 of 5 — High value — a gap here costs you rounds

六個需要借用其他結構或其他恆等式的前綴和模板:補集技巧、應付含負數陣列的單調雙端佇列、二維的列對壓縮、前綴 XOR、用雜湊表實作的稀疏差分陣列,以及樹上前綴和的計數表。

Sliding Window — 進階視窗形態

Priority 4 of 5 — High value — a gap here costs you rounds

第一輪學習可以先跳過的滑動視窗技巧:用雙端佇列維護視窗極值、at-most-K-distinct 這一家、超出單一範例的 exactly-K 計數,以及那些「鍵不是字元」的視窗 —— 補集、以單字為單位的區塊、以索引界定的值桶,還有排序後的區間;六份必背模板留在主頁。

滑動視窗 — 實戰範例

Priority 4 of 5 — High value — a gap here costs you rounds

滑動視窗的 LeetCode 實作目錄,每題每語言只留一份標準解,並歸到它所對應的模板底下;模板本身、概念與選擇表都留在主檔滑動視窗那份。

字串演算法 — 實作範例

Priority 4 of 5 — High value — a gap here costs you rounds

字串類 LeetCode 的範例庫,每題每種語言只留一份標準解,並歸檔到它所實作的母文件模板底下;概念、模式目錄與模板本身都留在主字串文件。

雙指標 — 實例解析

Priority 4 of 5 — High value — a gap here costs you rounds

雙指標的 LeetCode 實作題庫,每題每種語言各一份標準解,依它所套用的模板分組;概念、指標種類與模板本身留在雙指標主文件裡。

把 xxx 加起來

Priority 3 of 5 — Worth knowing — usually a variant of a must-know pattern

逐位數相加,涵蓋面試官會丟給你的四種輸入形式:字串、整數、陣列、鏈結串列。

差分陣列

Priority 3 of 5 — Worth knowing — usually a variant of a must-know pattern

O(1) 區間更新、O(n) 重建 — 前綴和的反操作。

前綴和 — 範例詳解

Priority 3 of 5 — Worth knowing — usually a variant of a must-know pattern

prefix_sum.md 背後的解題存檔:八道模板無法從頭到尾解掉的題目,依照它們需要哪種前綴和形狀分組。

字串比對(KMP、Rolling Hash)

Priority 3 of 5 — Worth knowing — usually a variant of a must-know pattern

只談子字串搜尋 — KMP 的失敗函數、Rabin-Karp rolling hash,以及「內建 indexOf vs KMP vs hash」該怎麼選。

字串操作與語言 API

Priority 3 of 5 — Worth knowing — usually a variant of a must-know pattern

字串的語言層機制:Python 的切片與方法、Java 的 String 與 StringBuilder、字元分類與大小寫轉換、字元算術、split/join 的陷阱,以及建構字串的效能守則——不談使用它們的演算法。

進階字串演算法

Priority 2 of 5 — Niche — read once, revisit only if a company is known to ask

比較重的字串工具:後綴結構、Z-algorithm、Manacher,以及字串 DP — 這些對主字串文件來說太專門的部分。

雜湊、堆疊與佇列12 份

O(1) 查找加上 LIFO/FIFO 的紀律 — 把 O(n^2) 掃描壓成 O(n) 的那組結構。

Hash Map Cheatsheet(雜湊表速查)

Priority 5 of 5 — Must know — expect it in almost every loop

鍵→值的題型模式:查找、分組、索引表、前綴和表、重新映射。

堆疊(Stack)

Priority 5 of 5 — Must know — expect it in almost every loop

LIFO 的基本功,加上堆疊的經典模板:括號配對、min-stack、單調堆疊的精簡版、用顯式堆疊做走訪,以及作用域/上下文帳本。

雜湊表 — 題目詳解

Priority 4 of 5 — High value — a gap here costs you rounds

雜湊表家族的解法檔案庫:每題一個標準解、那些其實是單題深入剖析的專屬模板,以及有序 map(Java TreeMap / Python SortedDict)的參考資料。

Collection(容器選擇)

Priority 4 of 5 — High value — a gap here costs you rounds

跨容器的選型指南:什麼工作該用哪個 Java/Python 容器,以及各自的 API 陷阱。

單調堆疊資料結構

Priority 4 of 5 — High value — a gap here costs you rounds

next greater/previous smaller/span/直方圖這類題目 — 堆疊本身保持有序,所以每個元素只被推入與彈出一次。

Queue 佇列

Priority 4 of 5 — High value — a gap here costs you rounds

FIFO 的基本功:BFS 佇列、雙端佇列、環形緩衝區,以及以佇列為底的設計題。

Stack — 運算式解析

Priority 4 of 5 — High value — a gap here costs you rounds

用堆疊解運算式的題型家族:計算機三兄弟(LC 224 / 227 / 772)、decode-string 式的巢狀(LC 394),以及後綴/依序運算元求值(LC 150、682),還有讓運算子優先序自然浮現的 pre_op 延遲插入技巧。

Stack — 實戰題解

Priority 4 of 5 — High value — a gap here costs you rounds

stack.md 背後的題解庫:單調堆疊、貪婪移除、相鄰重複、括號家族、走訪與設計類題目,每題每語言各一份標準解,依各自演練的模板分組。

雜湊與計數

Priority 3 of 5 — Worth knowing — usually a variant of a must-know pattern

雜湊的內部原理與計數慣用手法 — 雜湊函式設計、碰撞、頻率表、滾動雜湊、自訂鍵。

單調佇列(雙端佇列)

Priority 3 of 5 — Worth knowing — usually a variant of a must-know pattern

用一個維持單調性的雙端佇列,以攤還 O(1) 的成本求滑動視窗的最大/最小值。

Set(集合)

Priority 3 of 5 — Worth knowing — usually a variant of a must-know pattern

成員判斷、去重與集合運算 — 只需要知道元素「在不在」、不需要對應值的題目。

Set — 實戰題解

Priority 3 of 5 — Worth knowing — usually a variant of a must-know pattern

set.md 背後的解法檔案庫:十四題,依「這個集合到底被拿來做什麼」分組 — 記住看過什麼、集合運算、用 O(1) 索引取代掃描,或是當成更大演算法裡的一個零件。

鏈結串列3 份

指標手術。概念不深,但對實作精準度的要求很高。

Linked List(鏈結串列)

Priority 5 of 5 — Must know — expect it in almost every loop

單向與雙向鏈結串列的指標手術:反轉、合併、重排、虛擬頭節點技巧,以及環的處理。

鏈結串列 — 範例題解

Priority 4 of 5 — High value — a gap here costs you rounds

linked_list.md 背後的題解存放處:反轉、合併、切分、複製、攤平與串列排序類題目,每題每語言各一份標準解,並依各自演練的技巧分組。

Two pointers - Linkedlist(雙指標:鏈結串列)

Priority 4 of 5 — High value — a gap here costs you rounds

走在鏈結串列節點上的雙指標:快慢指標找環、找中點、找倒數第 k 個、以及固定間距指標。不談陣列索引。

樹與堆積18 份

遞迴結構加上有序存取。所有標記為樹/堆積的面試題,大約四分之一出自這五份文件。

二元樹

Priority 5 of 5 — Must know — expect it in almost every loop

二元樹特有的思路:DFS 的狀態往哪個方向流(往下還是往上),以及建立在這個基礎上的 11 個結構化模板。

BST (Binary Search Tree)(二元搜尋樹)

Priority 5 of 5 — Must know — expect it in almost every loop

只談有序的樹 —— left < root < right 這條不變式換來了什麼(O(log n) 搜尋、中序即排序、依範圍剪枝、順序統計)。

堆積與優先佇列

Priority 5 of 5 — Must know — expect it in almost every loop

堆積(heap,這個資料結構)以及它所實作的優先佇列(priority queue,這個抽象資料型別),Python 與 Java 兩種語言。本檔案原本拆成 heap.md + priority_queue.md,兩邊把同樣的題目各解了一遍。

樹狀資料結構 — 概念與模式

Priority 5 of 5 — Must know — expect it in almost every loop

樹的概念、樹的種類,以及走訪順序的策略 — 講的是為什麼與選哪個,再加上那些不屬於單一模式模板的進階技巧(Morris 穿線、倍增法、換根)。模板本身放在 tree2.md。

樹的 LCA、距離與路徑問題

Priority 5 of 5 — Must know — expect it in almost every loop

最近共同祖先、節點間距離、父節點表(雙向)走訪,以及根到葉的路徑模板——所有答案是一條路徑或一個交會點、而不是某種形狀的樹問題。

進階堆積技巧

Priority 4 of 5 — High value — a gap here costs you rounds

第一輪複習可以先跳過的堆積模式:延遲刪除、掃描線的「存活」堆積、後悔貪婪、資源池配置器、格子圖上的最佳優先搜尋,以及二元堆積之外的結構;六個必背模板留在母文件裡。

BST — 進階模式與深入探討

Priority 4 of 5 — High value — a gap here costs you rounds

第一輪學習可以先跳過的 BST 內容:順序統計(rank)查詢、只用 O(h) 空間的惰性迭代器、用中序走訪偵測損壞 BST 的下降點、完整的建構變形目錄,以及超出標準刪除與驗證模板的拆離/邊界變形。

BST — LeetCode 實作範例

Priority 4 of 5 — High value — a gap here costs you rounds

bst.md 教的那些 BST 模板的解題檔案庫:每題每個語言一份標準解,外加雖然歸在 BST、卻完全用不到大小順序的「根到葉」與「節點到節點」路徑家族。

堆積(heap) — LC 題解範例

Priority 4 of 5 — High value — a gap here costs you rounds

堆積 / 優先佇列(priority queue)家族的題解存放處:每題每種語言一份標準解,把推理過程、追蹤與陷阱完整寫出來。

樹的建構

Priority 4 of 5 — High value — a gap here costs you rounds

從扁平的東西把二元樹建起來:兩組走訪陣列、單一陣列上的索引區間,或是帶括號/中序的字串 — 也就是所有「只讀取樹」的題目的反方向。

樹的模式模板 — 完整指南

Priority 4 of 5 — High value — a gap here costs you rounds

每個樹的模式配一份編號好、可直接複製貼上的模板,Python 與 Java 都有 — 樹模板的唯一集散地。模板優先,不談理論:某一題該用哪種走訪,是 tree.md 要回答的問題。

樹的序列化與字串編解碼

Priority 4 of 5 — High value — a gap here costs you rounds

把樹變成字串、再變回來 — 用子樹指紋做身分辨識與重複偵測,以及完整的編碼/解碼家族(括號式、逗號加 null 標記、深度前綴)。

字典樹(Trie)

Priority 4 of 5 — High value — a gap here costs you rounds

前綴樹 — insert/search/startsWith、節點該怎麼擺,以及那些「共用前綴」就是全部訣竅的題目(自動補完、單字搜尋、XOR trie)。

堆積的語言 API — heapq 與 PriorityQueue

Priority 3 of 5 — Worth knowing — usually a variant of a must-know pattern

堆積在語言層面的機械細節:heapq 和 PriorityQueue 你會用到的每個呼叫、怎麼假造出最大堆積、怎麼在不 pop 的情況下偷看堆頂,以及「只有部分有序」的容器會帶來哪些陷阱;用到這些 API 的演算法本身,放在其他堆積的表單裡。

樹 — LeetCode 實戰題解

Priority 3 of 5 — Worth knowing — usually a variant of a must-know pattern

其他檔案所教的樹模式,其題解檔案庫:每題每種語言各一份標準解法,依「題目在問什麼」分組,而不是依技巧分組。

樹的回溯模板(gpt)

Priority 3 of 5 — Worth knowing — usually a variant of a must-know pattern

根到葉的路徑問題:路徑本身就是狀態,回程時必須把它復原。

Trie — 實戰題解

Priority 3 of 5 — Worth knowing — usually a variant of a must-know pattern

trie.md 背後的題解倉庫:五道題,從把結構蓋出來,一路到在格子上搜尋——字典樹把「每個單字各走一次」壓成「只走一趟」。

Priority Queue (PQ)(優先佇列)

Priority 2 of 5 — Niche — read once, revisit only if a company is known to ask

僅作轉址。 本檔已併入 heap.md,留著只是為了把舊連結導向正確的章節。

轉址

圖論19 份

走訪、連通性與排序。BFS/DFS 是必備;帶權最短路徑三本柱多半是 L5 以上才會碰。

BFS(廣度優先搜尋)

Priority 5 of 5 — Must know — expect it in almost every loop

BFS 的主要參考文件:佇列模板、逐層展開、格子與多源 BFS,以及為什麼在無權圖上「第一次拜訪」就等於最短路徑;較重的變形與大量解題範例則各自放在自己的文件裡。

DFS(深度優先搜尋)

Priority 5 of 5 — Must know — expect it in almost every loop

DFS 的主文件:十個核心的深度優先模板 — 樹走訪、網格填色(flood fill)、路徑搜尋、回溯、樹結構修改、後序彙總、邊界消除、形狀簽名與帶權邊走訪 — 並附上用來挑選模板的辨識表。

圖論演算法

Priority 5 of 5 — Must know — expect it in almost every loop

圖的表示法、走訪、連通性、環偵測,以及一般圖題的完整目錄。

Dijkstra — 實作範例

Priority 4 of 5 — High value — a gap here costs you rounds

Dijkstra.md 背後的解題實作庫:十一題、兩種語言,依「搜尋狀態長什麼樣」分組,因為那正是決定你需要 dist[]、需要第二個狀態維度、還是一個單純 visited[] 的關鍵。

Dijkstra’s Algorithm

Priority 4 of 5 — High value — a gap here costs you rounds

用優先佇列解非負權重的單源最短路徑。

拓撲排序 - 完整指南

Priority 4 of 5 — High value — a gap here costs you rounds

為 DAG 排序 — Kahn 的 BFS 解法、DFS 後序、環的偵測,以及建構在它們之上的排程類題目。

拓撲排序 — 題目實作集

Priority 4 of 5 — High value — a gap here costs you rounds

topology_sorting.md 背後的解題檔案庫:八題、兩種語言,依照題目編碼出來的「相依關係形狀」分組,而不是依題號排。

Union Find(併查集)

Priority 4 of 5 — High value — a gap here costs you rounds

併查集(disjoint set union)——無向圖的連通性、連通元件計數、環偵測,搭配路徑壓縮與按秩合併。

進階圖論演算法

Priority 3 of 5 — Worth knowing — usually a variant of a must-know pattern

第一輪面試準備可以先跳過的圖論技巧:Tarjan 的 low-link 家族(強連通分量、橋、關節點)、Euler 路徑與迴路、最大流/最小割,以及二分圖的延伸主題 — 用併查集判定、最大匹配與貪婪 k-著色。

BFS — 進階變形

Priority 3 of 5 — Worth knowing — usually a variant of a must-know pattern

第一輪學習可以先跳過的 BFS 技巧:雙向 BFS、搭配 deque 的 0-1 BFS、超出標準模板的多源 BFS、在隱式狀態空間上的 BFS,以及 DAG 上所有最短路徑的列舉——必背的佇列模板留在主篇。

BFS —— LeetCode 解題實作

Priority 3 of 5 — Worth knowing — usually a variant of a must-know pattern

BFS 的解題實作庫:網格、狀態空間、樹的改寫與剝葉子這幾類題目,每題一個標準解,另外附上 LC 994 關於「時間該在哪裡加一」的完整討論 —— 這裡不教任何新模板。

DFS — 進階模式

Priority 3 of 5 — Worth knowing — usually a variant of a must-know pattern

第一輪複習可以先跳過的冷門硬 DFS 技巧:雙網格驗證、邊方向追蹤、跨連通分量的配對計數、尤拉路徑、Tarjan low-link 找橋、字典樹配萬用字元搜尋、以深度為索引的堆疊 DFS,以及在 N 元樹與 parent 陣列樹上的後序彙總。

DFS — 實戰題解

Priority 3 of 5 — Worth knowing — usually a variant of a must-know pattern

dfs.md 的題解檔案庫:核心模板涵蓋的每道 DFS 題目各一份標準解法,外加整個 DFS 題庫依模式與難度的索引。

圖論實作範例

Priority 3 of 5 — Worth knowing — usually a variant of a must-know pattern

graph.md 的解法歸檔:針對格子圖、複製圖、連通性、比值圖、隱式 DAG 與「依屬性做併查集」這幾類題目,每題各給一份標準解,本身不包含任何模板或理論內容。

Shortest Path Algorithms — 什麼時候用哪一個

Priority 3 of 5 — Worth knowing — usually a variant of a must-know pattern

只做選型判斷 — 哪種題型該用哪個最短路徑演算法,以及直覺選法在哪裡會錯。不放完整實作。

快速比較:拓撲排序 vs Quick Union(併查集)

Priority 3 of 5 — Worth knowing — usually a variant of a must-know pattern

只是一份決策文件 —— 拓撲排序 vs 併查集:各自回答什麼問題、直覺選錯的地方在哪,以及兩者都能解的題目。

併查集(Union Find)— 範例詳解

Priority 3 of 5 — Worth knowing — usually a variant of a must-know pattern

union_find.md 背後的解題檔案庫:十九道題,依「一個節點代表什麼」分組——頂點、格子、帶比值的變數,或樹節點——因為題目之間變的就只有這件事。

Bellman-Ford Algorithm

Priority 2 of 5 — Niche — read once, revisit only if a company is known to ask

容得下負權重、而且能偵測負環的單源最短路徑演算法,另外還有限制跳數(k hop)的變形。

Floyd-Warshall Algorithm

Priority 2 of 5 — Niche — read once, revisit only if a company is known to ask

用「中繼頂點」做 DP 求全點對最短路徑 — O(V³)、稠密圖、遞移閉包。

搜尋與排序6 份

把單調空間對半砍,以及排序究竟買到了什麼。「對答案做二分搜尋」是最被低估的五星技巧。

Binary Search

Priority 5 of 5 — Must know — expect it in almost every loop

Halving a monotonic search space — the loop-invariant reasoning behind l <= r vs l < r, the boundary (lower/upper bound) templates, rotated arrays, and floating-point and 2D search.

對答案做二分搜尋

Priority 5 of 5 — Must know — expect it in almost every loop

針對一個候選答案的範圍,搭配單調的可行性判定式做二分搜尋 — canFinish / isValid 的寫法、「最小化最大值 vs 最大化最小值」的取捨、[max(nums), sum(nums)] 邊界配方,以及在值域上做計數的判定式。

二分搜尋 — 範例題解

Priority 4 of 5 — High value — a gap here costs you rounds

binary_search.md 的題解存放處 — 索引空間類模板的每題一份標準解,附上主文放不下的追蹤過程與陷阱。

QuickSelect(以分割求第 K 個元素)

Priority 4 of 5 — High value — a gap here costs you rounds

以分割(partition)為基礎的選擇演算法:只往 QuickSort 分割後的其中一側遞迴,在平均 O(n) 時間內找出第 K 大、第 K 小或最接近的 K 個元素,包含 pivot 的挑選策略,以及最壞情況 O(n) 的 Median of Medians 大綱。

排序演算法與技巧

Priority 4 of 5 — High value — a gap here costs you rounds

排序(sorting)演算法與周邊技巧 — 比較式排序與其穩定性、計數/桶/基數排序、quickselect、自訂比較器,以及循環排序(cyclic sort)。

進階分治法

Priority 3 of 5 — Worth knowing — usually a variant of a must-know pattern

合併排序以外的分治法 —— 那些「遞迴 + 合併」的形狀(邊排序邊計數、最近點對、矩陣分治、運算式分治)以及它們的遞迴式。

動態規劃與遞迴17 份

最硬的五星區塊。從遞迴出發,先記憶化,再改成表格。

0/1 背包 DP(0/1 Knapsack 中文詳解)

Priority 5 of 5 — Must know — expect it in almost every loop

用中文把 0/1 背包從「物品重量價值」一路講到 LC 494/416 的解題流程,重點放在狀態怎麼定義、「拿或不拿」的轉移、以及為什麼內層迴圈一定要倒序;完全背包(Unbounded)與排列/組合的迴圈順序規則不在這裡展開。

回溯(Backtracking)

Priority 5 of 5 — Must know — expect it in almost every loop

帶「復原」動作的系統性搜尋:choose/explore/un-choose 骨架、start_idx 的控制、跳過重複、剪枝,以及每個必會題型剛好一份的標準模板 — 大量的實作解答與 hard 級的帶狀態模板放在兩個衛星檔裡。

動態規劃(DP)

Priority 5 of 5 — Must know — expect it in almost every loop

DP 的主文件 — 狀態設計、模式目錄,以及每個必會 DP 家族各一份標準模板;實作解法庫、冷門技巧,以及五個最重的子題目都各自獨立成篇,從這裡連過去。

背包 DP(0/1、完全背包、Coin Change)

Priority 5 of 5 — Must know — expect it in almost every loop

完整的背包問題家族:0/1 vs 完全背包(unbounded)vs 有界背包(bounded)vs 分組背包(group)、子集合和(subset-sum)的化簡、為什麼 0/1 的內層迴圈要倒序,以及區分組合與排列的迴圈順序規則。

進階回溯

Priority 4 of 5 — High value — a gap here costs you rounds

需要在遞迴中額外攜帶狀態的困難級回溯 — 一個 Trie 節點、前一個運算元、一份刪除額度 — 再加上約束傳播、記憶化搜尋,以及第一輪學習應該直接跳過的通用切分模板;必背的基本形狀不會在這裡重複。

DP 進階技巧

Priority 4 of 5 — High value — a gap here costs you rounds

DP 的附錄——第一輪該跳過的技巧:賽局理論/minimax DP、樹形與換根 DP、區間與字串 DP 的深入探討、機率與按步數計數的 DP、單調佇列與單調堆疊承載的 DP,以及從主表裁掉的長篇推導。

DP 模式總表

Priority 4 of 5 — High value — a gap here costs you rounds

模板索引:每個經典 DP 模式各佔一小節(Kadane、LIS、MCM、LCS、背包、狀態機、格子、位元遮罩、數位、樹上 DP、正規表達式、加權區間排程、切分、記憶化 DAG)。

遞迴

Priority 4 of 5 — High value — a gap here costs you rounds

把遞迴當成一種機制來談:base case、往下傳的狀態 vs 往上回傳的結果、呼叫堆疊的成本,以及怎麼改寫成迭代。使用遞迴的那些題型家族各自有專屬檔案。

從遞迴到動態規劃的轉換

Priority 4 of 5 — High value — a gap here costs you rounds

一套機械化的流程:遞迴 → 記憶化(memoization) → 表格化(tabulation) → 空間最佳化,一次套用在一題上。

字串 DP(雙序列網格模式)

Priority 4 of 5 — High value — a gap here costs you rounds

在一或兩個字串上做的 DP:dp[m+1][n+1] 的雙序列網格、以前綴為基礎(1-indexed)的表格設計,以及 LCS、編輯距離、交錯字串、萬用字元/括號比對的完整範例。

回溯 — LC 題目實作

Priority 3 of 5 — Worth knowing — usually a variant of a must-know pattern

回溯題解的長尾(LC 17、39、79、78、90、77、46、22、93、139、140、207),附遞迴軌跡與差一點就一樣的變形,每題每種語言只留一份正典解 — 它本身不教任何模板,每一節都指回它所實例化的那一個。

位元遮罩 DP(狀態壓縮)

Priority 3 of 5 — Worth knowing — usually a variant of a must-know pattern

狀態是「一個用整數編碼的子集合」的 DP:遮罩運算、子遮罩列舉、TSP 與指派問題的模板,以及 n <= 20 這條規模上限。

DP 實戰題解

Priority 3 of 5 — Worth knowing — usually a variant of a must-know pattern

dp.md 背後的題解檔案庫:每一題經典 DP 各一份標準 Java / Python 解法,加上依模式分類的題目索引,本身不放模板也不談理論。

Kadane’s Algorithm(卡登演算法)

Priority 3 of 5 — Worth knowing — usually a variant of a must-know pattern

深入最大子陣列這個題型家族:Kadane 本體與它的各種變形(乘積、環狀、可刪除一個元素、二維)。

單調堆疊 + DP

Priority 3 of 5 — Worth knowing — usually a variant of a must-know pattern

單調堆疊上還掛著一個 DP 值的題型:「這個元素能撐過幾輪」、直方圖最大矩形那類面積 DP,以及最大正方形/計算正方形數量的網格遞迴式。

數位 DP(在限制下數數字)

Priority 2 of 5 — Niche — read once, revisit only if a company is known to ask

數出某個區間內有多少數字滿足「逐位」的性質:tight/started/位置這組狀態、通用的 top-down 模板,以及 count(R) - count(L-1) 這個區間技巧。

貪婪與區間6 份

先排序再掃描,以及證明它正確的交換論證。

貪婪演算法

Priority 4 of 5 — High value — a gap here costs you rounds

每一步選當下最好的,以及證明這樣做安全的交換論證——區間排程、跳躍遊戲、任務分配——外加怎麼看出貪婪會失敗、必須改用 DP 的時機。

區間(Intervals)

Priority 4 of 5 — High value — a gap here costs you rounds

先排序再合併的區間題型 — 合併、插入、數重疊、最少刪除數。

貪婪 — 題目實作

Priority 3 of 5 — Worth knowing — usually a variant of a must-know pattern

greedy.md 背後的題解倉庫:十四題,依貪婪選擇的形狀分組 — 往前延伸可達範圍、累積後歸零、依頻率交錯、排序後取走,或邊掃邊定案。

掃描線(Line Sweep)演算法

Priority 3 of 5 — Worth knowing — usually a variant of a must-know pattern

掃描線:把每個區間轉成 +1 / -1 事件,依座標排序,掃過一次。

掃描線 — 範例詳解

Priority 3 of 5 — Worth knowing — usually a variant of a must-know pattern

scanning_line.md 背後的完整解題檔案:六道題目,依「掃描究竟在數什麼」分組 — 重疊深度、加權總和、存活工作的堆積,或兩個有序清單的交集。

MyCalendar 中重疊判定邏輯的解說

Priority 2 of 5 — Niche — read once, revisit only if a company is known to ask

只講一件事 — 區間重疊的判定式:閉區間 [a,b] vs 半開區間 [a,b),以及每一題 LC 各需要哪一種。

區間查詢與進階結構3 份

只有在題目同時需要「更新」和「區間查詢」時才動用這些 — 否則前綴和就夠了。

樹狀陣列(Binary Indexed Tree / Fenwick Tree)

Priority 3 of 5 — Worth knowing — usually a variant of a must-know pattern

專講 Fenwick 樹:i & -i 的索引運算、單點更新 + 前綴查詢,以及用它最俐落的那些題目。

Segment Tree & Binary Indexed Tree (Fenwick Tree)

Priority 3 of 5 — Worth knowing — usually a variant of a must-know pattern

支援區間查詢+區間更新的結構 —— 線段樹(segment tree)、懶惰標記(lazy propagation),以及 BIT vs 線段樹 vs 合併排序該怎麼選。

串流演算法

Priority 2 of 5 — Niche — read once, revisit only if a company is known to ask

被限制在單趟掃描與次線性記憶體之下的演算法 —— 蓄水池抽樣、Boyer-Moore 多數投票、加權隨機挑選,以及近似計數。

數學與位元運算4 份

小而自足的主題。學習成本低,偶爾一題定生死。

位元運算

Priority 3 of 5 — Worth knowing — usually a variant of a must-know pattern

整數在位元層次上是怎麼被表示的(二補數、固定位寬、位移),以及建立在其上的各種運算與技巧:遮罩、XOR 恆等式、最低位 1、子集列舉,還有 bitmask DP。

Bit Manipulation — Worked Examples

Priority 3 of 5 — Worth knowing — usually a variant of a must-know pattern

The worked-solution archive behind bit_manipulation.md: nineteen problems grouped by which property of the bit operators they lean on — XOR cancelling pairs, clearing the lowest set bit, carry-free arithmetic, an integer standing in for a subset, or a hand-built mask editing a bit field.

數學

Priority 3 of 5 — Worth knowing — usually a variant of a must-know pattern

面試中的數值操作 — 取位數、避免溢位的算術、進位轉換、開根號與次方。公式層級的計數則在隔壁那份。

組合數學與數學模式

Priority 2 of 5 — Niche — read once, revisit only if a company is known to ask

面試會考到的計數與數論:模運算、GCD/LCM、篩法、nCr、蓄水池抽樣,以及基本幾何。

設計與模擬7 份

「設計一個 X」與低階設計關卡 — 評分看的是操作複雜度與類別建模,不是花招。

Design — 完整範例

Priority 4 of 5 — High value — a gap here costs you rounds

design.md 背後的解法檔案庫:二十題 LC「設計一個 X」的完整解,依每題被迫採用的結構組合分組,而不是依題號。

系統設計題(Design)

Priority 4 of 5 — High value — a gap here costs you rounds

LC 的「設計一個 X」題型:從題目讀出必須支援的操作,再挑出能讓每個操作都做到 O(1) 或 O(log n) 的結構組合;實際寫出來的設計放在 examples 那一頁。

物件導向設計(OOD/Low-Level Design)

Priority 4 of 5 — High value — a gap here costs you rounds

低階設計那一關 — 類別建模、SOLID,以及面試官真的會問的設計模式(strategy、observer、factory、state),附完整的設計範例。

Iterator(迭代器)

Priority 3 of 5 — Worth knowing — usually a variant of a must-know pattern

迭代器契約:hasNext/next 搭配延遲狀態、攤平、預看與合併,單一觀念獨立成篇。

系統設計類的實作模式

Priority 3 of 5 — Worth knowing — usually a variant of a must-know pattern

那些會被當成「寫程式題」來考的分散式系統模式:一致性雜湊、token bucket 與 leaky bucket 限流、以及各種負載平衡演算法,附上可直接跑的實作,以及面試官一定會追問的取捨。

Concurrency Patterns (Java)

Priority 2 of 5 — Niche — read once, revisit only if a company is known to ask

The handful of Java concurrency problems that appear at L4+ — ordered printing, producer/consumer, read-write coordination and deadlock avoidance — plus the primitives they need.

進階模擬

Priority 2 of 5 — Niche — read once, revisit only if a company is known to ask

那種「答案就是忠實地一步步跑完」的題目:狀態機、機器人/格子上的行走、遊戲回合、迭代改寫,外加怎麼讓這種程式碼不要爆長。

語言工具箱7 份

語言機制,不是演算法。為了寫得快,也為了不再因為可避免的坑而掉 submission。

Java 容器與陣列速查表

Priority 4 of 5 — High value — a gap here costs you rounds

存放資料用的 Java 函式庫 API:陣列與二維陣列、list、map、佇列、堆積、堆疊與 pair — 每一種怎麼初始化、複製、走訪、索引,以及過程中的陷阱。

Java 字串與排序速查表

Priority 4 of 5 — High value — a gap here costs you rounds

Java 的 String 與 StringBuilder 操作 — 與 char[] 互轉、切片、解析、組字串與修改 — 加上所有跟 comparator 有關的東西:陣列、集合與 map 的排序,以及決定順序的回傳值規則。

Java 技巧與慣用寫法

Priority 4 of 5 — High value — a gap here costs you rounds

決定「正確的演算法會不會算出正確答案」的那些 Java 語言語意:字元其實是整數、傳值與傳參考的差別,以及整數運算。函式庫 API 則放在另外兩份姊妹速查表。

Python 插入、切片與索引運算

Priority 4 of 5 — High value — a gap here costs you rounds

Python 解法「邏輯明明對了卻答錯」的兩大元凶:插入到底落在哪一格,以及一段索引區間指的是「個數」還是「距離」。

面試用的 Python 標準函式庫

Priority 4 of 5 — High value — a gap here costs you rounds

逐個函式庫的參考手冊:heapq、sortedcontainers、bisect、collections、itertools、functools 與 datetime——每個呼叫、它們的複雜度,以及各自是為了取代哪一種手寫寫法而存在。

Python 技巧與慣用寫法

Priority 4 of 5 — High value — a gap here costs you rounds

解題過程中會用到的 Python 語言慣用寫法 — 內建操作的成本、複製、字串處理、排序 key、整數運算、生成式(comprehension)與作用域 — 依「你想做什麼」來分組。標準函式庫與索引運算另有專屬速查表。

Python 陷阱與並行處理

Priority 3 of 5 — Worth knowing — usually a variant of a must-know pattern

那些會悄悄害你 submission 掛掉的 Python 行為:可變預設參數、closure 的延遲綁定、整數快取、複製語意,再加上 GIL 與 Python 的並行處理全貌。

面試方法論6 份

怎麼分類一道題、怎麼說出它的複雜度。第一次模擬面試前先讀。

LeetCode 模式指南

Priority 5 of 5 — Must know — expect it in almost every loop

最上層的地圖:拿到一題,它想要的是哪個模式(也就是該翻哪份小抄)。這裡只做索引與分流,不放模板。

複雜度分析練習題

Priority 3 of 5 — Worth knowing — usually a variant of a must-know pattern

20 道自我測驗題 — 讀程式片段、說出複雜度、再對答案檢查自己。

程式面試流程

Priority 2 of 5 — Niche — read once, revisit only if a company is known to ask

轉址頁。原本的六步面試流程已經搬進 lc_pattern.md,那才是你開始解題時真正會打開的那一頁。

轉址

LC 題型分類索引(wisdompeak)

Priority 2 of 5 — Niche — read once, revisit only if a company is known to ask

指向 wisdompeak 的題目分類表,並把它的 29 個分類對應到本 repo 中負責教它的那份 cheatsheet。分類表本身不在這裡複製一份。

轉址

使用方式:先挑主題,讀它的「範圍」那行確認這份文件確實涵蓋你的問題,再直接跳到標星的章節。每份速查表只連向相鄰主題,不重複它們的內容。

原始檔:GitHub 上的 i18n/zh — 星等與分組定義在 data/cheatsheet_meta.json