↓Skip to main content

Recursion

LeetCode 25: Reverse Nodes in k-Group
·2 mins
LeetCode Hard Linked-List Recursion
LeetCode 25 note: count the length to know how many full groups to reverse, k nodes each; a dummy head avoids special-casing the first group, and the old group head becomes its tail
Backtracking
·6 mins
Algorithm Backtracking Recursion Dfs Pruning
The choose, recurse, undo template for subsets, permutations, constrained placement, grid search, and pruning
LeetCode 60: Permutation Sequence
·2 mins
LeetCode Hard Math Combinatorics Recursion
LeetCode 60 note: make k 0-indexed; once the first digit is fixed there are (n-1)! arrangements, so k // (n-1)! picks the first digit among those remaining, then repeat for the next position
LeetCode 394: Decode String
·2 mins
LeetCode Medium Stack String Recursion
LeetCode 394 note: on [ push the current string and count, on ] pop and combine; counts can have several digits
LeetCode 22: Generate Parentheses
·2 mins
LeetCode Daily Medium String Backtracking Recursion Combinatorics Parentheses
Generating all valid parentheses combinations using backtracking approach
LeetCode 1008: Construct Binary Search Tree from Preorder Traversal
·3 mins
LeetCode Medium Tree Bst Binary-Tree Recursion
LeetCode 1008 note: splitting the array is O(n^2); recursing with an upper bound is O(n)
LeetCode 2487: Remove Nodes From Linked List
·1 min
LeetCode Medium Linked-List Stack Recursion Monotonic-Stack
LeetCode 2487 note: the kept nodes are non-increasing left to right; keep a monotonic stack, pop anything smaller than the new node, then link what remains
LeetCode 50: Pow(x, n)
·2 mins
LeetCode Medium Math Recursion
LeetCode 50 note: fast exponentiation, x^n = (x²)^(n/2) with an extra x for odd n; for negative n compute 1 / x^(-n)
LeetCode 143: Reorder List
·2 mins
LeetCode Medium Linked-List Two-Pointers Stack Recursion
LeetCode 143 note: find the middle, reverse the second half, and merge the two alternately, cutting the first half’s tail
LeetCode 24: Swap Nodes in Pairs
·2 mins
LeetCode Medium Linked-List Recursion
LeetCode 24 note: a dummy plus pairs, rewiring prev.next, a.next and b.next together; swap nodes, not values
LeetCode 509: Fibonacci Number
·1 min
LeetCode Easy Math Dynamic-Programming Recursion Memoization
LeetCode 509 note: the starting point of 1D DP; only the previous two terms are needed, so roll two variables
LeetCode 342: Power of Four
·1 min
LeetCode Easy Math Bit-Manipulation Recursion
LeetCode 342 note: confirm a power of two, then that the single 1 sits at an even position: n & 0x55555555 != 0, or n % 3 == 1