Matrix
LeetCode 3643: Flip Square Submatrix Vertically
·1 分鐘
LeetCode
Easy
Two-Pointers
Matrix
LeetCode 3643 解題筆記:只翻轉子矩陣的上下列,第 x + i 列和第 x + k - 1 - i 列的那一段互換
LeetCode 1222: Queens That Can Attack the King
·1 分鐘
LeetCode
Medium
Array
Matrix
Simulation
LeetCode 1222 解題筆記:從國王往八個方向一步一步走,每個方向第一個遇到的皇后就能攻擊;皇后放進集合方便查詢
LeetCode 3619: Count Islands With Total Value Divisible by K
·2 分鐘
LeetCode
Medium
Dfs
Bfs
Matrix
Graph
LeetCode 3619 解題筆記:網格 flood fill 加總每座島的值;格子到 10^5,用迭代避免遞迴太深
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 3070: Count Submatrices with Top-Left Element and Sum Less Than k
·1 分鐘
LeetCode
Medium
Array
Matrix
Prefix-Sum
LeetCode 3070 解題筆記:包含左上角的子矩陣就是二維前綴和 P[i][j],數有幾個 <= k
LeetCode 240: Search a 2D Matrix II
·1 分鐘
LeetCode
Medium
Array
Binary-Search
Divide-and-Conquer
Matrix
LeetCode 240 解題筆記:從右上角開始,比目標大就往左、比目標小就往下,每一步排除一整列或一整欄
LeetCode 48: Rotate Image
·1 分鐘
LeetCode
Medium
Array
Math
Matrix
LeetCode 48 解題筆記:順時針轉 90 度 = 先轉置再把每一列左右反轉,兩步都可以原地做
LeetCode 417: Pacific Atlantic Water Flow
·1 分鐘
LeetCode
Medium
Array
Depth-First-Search
Breadth-First-Search
Matrix
LeetCode 417 解題筆記:反過來從海邊往內走,只能走到不比自己低的格子;兩個海都走得到的格子就是答案
LeetCode 74: Search a 2D Matrix
·1 分鐘
LeetCode
Medium
Array
Binary-Search
Matrix
LeetCode 74 解題筆記:每列接上一列是完整排序的,把矩陣當成長度 m × n 的一維陣列二分,mid 換成 (mid // n, mid % n)
LeetCode 73: Set Matrix Zeroes
·1 分鐘
LeetCode
Medium
Array
Hash-Table
Matrix
LeetCode 73 解題筆記:先記下哪些列、哪些欄有 0,掃完之後再一起改;進階用第一列、第一欄當標記
LeetCode 59: Spiral Matrix II
·1 分鐘
LeetCode
Medium
Array
Matrix
Simulation
LeetCode 59 解題筆記:跟 LC 54 一樣維護四個邊界,只是把讀取換成依序填入 1 到 n²
LeetCode 54: Spiral Matrix
·1 分鐘
LeetCode
Medium
Array
Matrix
Simulation
LeetCode 54 解題筆記:維護上下左右四個邊界,依序走右、下、左、上,每走完一邊就把那個邊界往內縮