{
  "type": "code_options",
  "author": "csdn.net",
  "source": "solution.md",
  "exercise_id": "02c66ed919a64658b723d5e7e8928b0e",
  "keywords": "数组,回溯",
  "title": "全排列 II",
  "desc": [
    {
      "content": "\n<p>给定一个可包含重复数字的序列 <code>nums</code> ，<strong>按任意顺序</strong> 返回所有不重复的全排列。</p><p> </p><p><strong>示例 1：</strong></p><pre><strong>输入：</strong>nums = [1,1,2]<strong><br />输出：</strong>[[1,1,2], [1,2,1], [2,1,1]]</pre><p><strong>示例 2：</strong></p><pre><strong>输入：</strong>nums = [1,2,3]<strong><br />输出：</strong>[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]</pre><p> </p><p><strong>提示：</strong></p><ul>\t<li><code>1 <= nums.length <= 8</code></li>\t<li><code>-10 <= nums[i] <= 10</code></li></ul>",
      "language": "markdown"
    }
  ],
  "answer": [
    {
      "content": "",
      "language": "java"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "java"
      }
    ],
    [
      {
        "content": "",
        "language": "java"
      }
    ],
    [
      {
        "content": "",
        "language": "java"
      }
    ]
  ],
  "template": {
    "content": "class Solution {\n\tList<List<Integer>> ans = new ArrayList<>();\n\n\tpublic List<List<Integer>> permuteUnique(int[] nums) {\n\t\tdfs(nums, 0);\n\t\treturn ans;\n\t}\n\n\tprivate void dfs(int[] nums, int cur) {\n\t\tif (cur == nums.length) {\n\t\t\tList<Integer> line = new ArrayList<>();\n\t\t\tfor (int i : nums) {\n\t\t\t\tline.add(i);\n\t\t\t}\n\t\t\tans.add(line);\n\t\t} else {\n\t\t\tfor (int i = cur; i < nums.length; i++) {\n\t\t\t\tif (canSwap(nums, cur, i)) {\n\t\t\t\t\tswap(nums, cur, i);\n\t\t\t\t\tdfs(nums, cur + 1);\n\t\t\t\t\tswap(nums, cur, i);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate boolean canSwap(int nums[], int begin, int end) {\n\t\tfor (int i = begin; i < end; i++) {\n\t\t\tif (nums[i] == nums[end]) {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t}\n\n\t\treturn true;\n\t}\n\n\tprivate void swap(int nums[], int i, int j) {\n\t\tint temp = nums[i];\n\t\tnums[i] = nums[j];\n\t\tnums[j] = temp;\n\t}\n}",
    "language": "java"
  },
  "node_id": "dailycode-423001ac26c44d20a3572064866b8a29",
  "license": "csdn.net",
  "created_at": 1637894159,
  "topic_link": "https://bbs.csdn.net/topics/600470224"
}