题目链接:
https://leetcode-cn.com/problems/permutations/
解法分析:
解决一个回溯问题,实际上就是一个决策树的遍历过程。只需要思考 3 个问题:
- 路径:也就是已经做出的选择
 - 选择列表:也就是你当前可以做的选择
 - 结束条件:也就是到达决策树底层,无法再做选择的条件
 
比如:

回溯的通用解法:
1 2 3 4 5 6 7 8 9 10
   | result = [] def backtrack(路径, 选择列表):     if 满足结束条件:         result.add(路径)         return
      for 选择 in 选择列表:         做选择         backtrack(路径, 选择列表)         撤销选择
   | 
核心可以用一句话总结:在递归之前做出选择,在递归之后撤销刚才的选择。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
   | 
 
 
  var permute = function(nums) {     const res = [];     const set = new Set();      backtrack([]);     return res;
      function backtrack(path) {                  if (path.length === nums.length) {             res.push(path.concat());             return;         }
          for (let i = 0; i < nums.length; i++) {             if (!set.has(i)) {                 path.push(nums[i]);                  set.add(i); 
                  backtrack(path);
                  path.pop();                  set.delete(i);              }         }             } };
 
  | 
参考题解:
- https://labuladong.gitee.io/algo/1/5/
 - https://gitee.com/labuladong/fucking-algorithm/blob/master/%E7%AE%97%E6%B3%95%E6%80%9D%E7%BB%B4%E7%B3%BB%E5%88%97/%E5%9B%9E%E6%BA%AF%E7%AE%97%E6%B3%95%E8%AF%A6%E8%A7%A3%E4%BF%AE%E8%AE%A2%E7%89%88.md#javascript