47:全排列 II
This commit is contained in:
parent
7e9979da76
commit
d96b90471b
@ -0,0 +1,81 @@
|
|||||||
|
//给定一个可包含重复数字的序列 nums ,按任意顺序 返回所有不重复的全排列。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 1:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:nums = [1,1,2]
|
||||||
|
//输出:
|
||||||
|
//[[1,1,2],
|
||||||
|
// [1,2,1],
|
||||||
|
// [2,1,1]]
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 2:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:nums = [1,2,3]
|
||||||
|
//输出:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 提示:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 1 <= nums.length <= 8
|
||||||
|
// -10 <= nums[i] <= 10
|
||||||
|
//
|
||||||
|
// Related Topics 回溯算法
|
||||||
|
// 👍 681 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
//47:全排列 II
|
||||||
|
public class PermutationsIi {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
Solution solution = new PermutationsIi().new Solution();
|
||||||
|
solution.permuteUnique(new int[]{1, 1, 2});
|
||||||
|
}
|
||||||
|
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
class Solution {
|
||||||
|
boolean[] vis;
|
||||||
|
|
||||||
|
public List<List<Integer>> permuteUnique(int[] nums) {
|
||||||
|
List<List<Integer>> ans = new ArrayList<List<Integer>>();
|
||||||
|
List<Integer> perm = new ArrayList<Integer>();
|
||||||
|
vis = new boolean[nums.length];
|
||||||
|
Arrays.sort(nums);
|
||||||
|
backtrack(nums, ans, 0, perm);
|
||||||
|
return ans;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void backtrack(int[] nums, List<List<Integer>> ans, int idx, List<Integer> perm) {
|
||||||
|
if (idx == nums.length) {
|
||||||
|
ans.add(new ArrayList<Integer>(perm));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (int i = 0; i < nums.length; ++i) {
|
||||||
|
if (vis[i] || (i > 0 && nums[i] == nums[i - 1] && !vis[i - 1])) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
perm.add(nums[i]);
|
||||||
|
vis[i] = true;
|
||||||
|
backtrack(nums, ans, idx + 1, perm);
|
||||||
|
vis[i] = false;
|
||||||
|
perm.remove(idx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
30
LeetCode/src/main/java/leetcode/editor/cn/PermutationsIi.md
Normal file
30
LeetCode/src/main/java/leetcode/editor/cn/PermutationsIi.md
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
<p>给定一个可包含重复数字的序列 <code>nums</code> ,<strong>按任意顺序</strong> 返回所有不重复的全排列。</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>示例 1:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>nums = [1,1,2]
|
||||||
|
<strong>输出:</strong>
|
||||||
|
[[1,1,2],
|
||||||
|
[1,2,1],
|
||||||
|
[2,1,1]]
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 2:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>nums = [1,2,3]
|
||||||
|
<strong>输出:</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>
|
||||||
|
<li><code>1 <= nums.length <= 8</code></li>
|
||||||
|
<li><code>-10 <= nums[i] <= 10</code></li>
|
||||||
|
</ul>
|
||||||
|
<div><div>Related Topics</div><div><li>回溯算法</li></div></div>\n<div><li>👍 681</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user