Back Tracking
题解
- 22 Generate Parentheses
- Patch result only when valid parentheses sequence.
- 37 Sudoku Solver
- Patch result only when valid sudoku.
51 N queues
|
|
39 Combination Sum
- Numbers w/o duplicate. DFS monoly on set.
|
|
40 Combination Sum II
- Numbers with duplicate. DFS monoly on sorted set, and skip duplicates before patching.
|
|
46 Permutations
- Numbers w/o duplicate. At each level try all numbers.
|
|
47 Permutations II
- Numbers with duplicate. DFS on sorted set, and skip duplicates.
|
|
- 60 Permutation Sequence
- Generate k-th permutation of 1…n.
- O(n^2) time, enumerate digits from the most siginificant position.
- O(nlog(n)) time if using BST or BIT to support log(n) looking up and updating for i-th digit.
77 Combinations
- Numbers w/o duplicate, generate (n, k) results. DFS monoly on sorted set.
|
|
78 Subsets
- Numbers w/o duplicate. DFS monoly on sorted set and add to result at each level.
|
|
- 89 Gray Code
- Gray code encodes 1 to n with 1 to n but each adjacent pair differs only in 1 bit.
- Note that the gray codes can be divided into parts with most significants 1 or 0.
90 Subsets II
- Numbers with duplicate, generate (n, k) results. Solutions must not duplicate. Skip duplicates before patching.
|
|
79 Word Search
- Given a letter grid, search for target word’s path. Brute-force DFS.
|
|
- 93 Restore IP Addresses
- Given a digit string, split it into valid IP.
- 131 Palindrome Partitioning
- Given a string, return all palindrome partitions. Test whether palindrome before DFS.
- 216 Combination Sum III
- Numbers in 1-9 and exactly k numbers, w/o duplicate. DFS monoly, and return when there is not enough numbers.
- 254 Factor Combinations
- Given a number, generate all factor decompositions of it. First generate all factors and DFS on them. Can use “Humble Number” alike method to accelerate it.
- 267 Palindrome Permutation II
- Given a string, generate all its permutaions that are palindrome. Simple letter count.
- 291 Word Pattern II
- Given a pattern string like “abab” and a target string like “xxyyxxyy”, find if there is a bijection between pattern string’s single characters and target string’s non-empty segments.
- Enumerate possible segments for each character and memo enumerated results.
- 306 Additive Number
- Given a string, find whether it can be parsed as a fibonacci sequence.
- Enumerate start state and validate following states.
- 320 Generalized Abbreviation
- Given a word, generate its generalized abbreviations, e.g., “word”: “w1rd”, “w2d”, “3w”, “4”, etc.
- DP or bit manipulation also work.
- 351 Android Unlock Patterns
- Calculate number of k-length Android unlock patterns.
- DFS, reuse symmetric cases.
参考资料
Linked Mentions
-
No backlinks found.