给定一个不含重复数字的数组 nums ,返回其 所有可能的全排列 。你可以 按任意顺序 返回答案。

思路

  1. 动态规划持续迭代
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
var permute = function (nums) {
let curItems = [[]]
for (const val of nums) {
const nextItems = []
for (const items of curItems) {
for (let j = 0; j <= items.length; ++j) {
const curVals = [...items]
curVals.splice(j, 0, val)
nextItems.push(curVals)
}
}
curItems = nextItems
}
return curItems
}