排列
从n个不同元素中,任取m个不同的元素按照一定的顺序排成一列,叫做从n个不同元素中取出m个元素的一个排列. 从n个不同元素中取出m(m≤n)个元素的所有排列的个数,叫做从n个不同元素中取出m个元素的排列数,用符号A(n, m)
表示,此外规定0! = 1
组合
从n个不同元素中,任取m(m≤n)个元素并成一组,叫做从n个不同元素中取出m个元素的一个组合;从n个不同元素中取出m(m≤n)个元素的所有组合的个数,叫做从n个不同元素中取出m个元素的组合数。用符号C(n,m)
表示C(n,m)=C(n,n-m)
常见的题目
子集
题目来源
思路:
深度: 选择
depth0: [1], []
depth1: [2], []
depth2: [3], []
depth3: back
1 | const subsets = nums => { |
全排列
题目来源
思路:
[1, 2, 3] -> [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]
深度: 选择:
depth0: [[1], [2], [3]] intersection used[]
depth1: [[1], [2], [3]] intersection used[choice1]
depth2: [[1], [2], [3]] intersection used[choice1, choice2]
depth3: 结束
1 | const permute = nums => { |
括号生成
题目来源
思路:
n = 3 [“((()))”,”(()())”,”(())()”,”()(())”,”()()()”]
depth0:
=> 左 && (left < n) 或者 右 && (right < left)
depth1:
=> 左 && (left < n) 或者 右 && (right < left)
depth2:
=> 左 && (left < n) 或者 右 && (right < left)
.
.
.
depth2n: 结束
1 | const generateParenthesis = function (n) { |
组合总和
题目来源
思路:
candidates = [2,3,6,7], target = 7, result = [[7], [2,2,3]]
candidates = [2,3,5], target = 8, result = [[2,2,2,2],[2,3,3],[3,5]]
depth0: choice in candidates
depth1: choice in candidates
.
.
depth sum > target: back
1 | const combinationSum = (candidates, target) => { |
复原IP地址
(https://leetcode-cn.com/problems/restore-ip-addresses/)
图像渲染
(https://leetcode-cn.com/problems/flood-fill/)
单词搜索
(https://leetcode-cn.com/problems/word-search/)
复原IP地址
(https://leetcode-cn.com/problems/restore-ip-addresses/)
N皇后
(https://leetcode-cn.com/problems/n-queens/)