使用 BFS 解决一系列排列、组合问题。基本上对 queue 进行迭代就可以了(需要 popleft 时选择 deque),但有时需要用到递归。
A huge number of coding interview problems involve dealing with Permutations and Combinations of a given set of elements. This pattern describes an efficient breadth-first search approach to handle all these problems.
Snippets
1 | """Iteration""" |
1 | """Recursion""" |
LeetCode
78. Subsets
90. Subsets II
46. Permutations
784. Letter Case Permutation
22. Generate Parentheses
320. Generalized Abbreviation
241. Different Ways to Add Parentheses
95. Unique Binary Search Trees II
96. Unique Binary Search Trees