# 47. Permutations II

Given a collection of numbers, `nums`, that might contain duplicates, return all possible unique permutations in any order.

Example 1:

``````Input: nums = [1,1,2]
Output:
[[1,1,2],
[1,2,1],
[2,1,1]]
``````

Example 2:

``````Input: nums = [1,2,3]
Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
``````

Constraints:

• `1 <= nums.length <= 8`
• `-10 <= nums[i] <= 10`

``````class Solution {
public:
vector<vector<int>> permuteUnique(vector<int>& nums) {
vector<vector<int>> res;
vector<int> cur, visited(nums.size(), 0);
sort(nums.begin(), nums.end());
dfs(nums, 0, visited, cur, res);
return res;
}
void dfs(vector<int>& nums, int level, vector<int>& visited, vector<int>& cur, vector<vector<int>>& res) {
if (level >= nums.size()) {
res.push_back(cur);
return;
}
for (int i = 0; i < nums.size(); ++i) {
if (visited[i] == 1) continue;
if (i > 0 && nums[i] == nums[i - 1] && visited[i - 1] == 0) continue;
visited[i] = 1;
cur.push_back(nums[i]);
dfs(nums, level + 1, visited, cur, res);
cur.pop_back();
visited[i] = 0;
}
}
};
``````

``````level = 0, i = 0 => cur: {}
level = 1, i = 0 => cur: {1 } skipped 1
level = 1, i = 1 => cur: {1 }
level = 2, i = 0 => cur: {1 2 } skipped 1
level = 2, i = 1 => cur: {1 2 } skipped 1
level = 2, i = 2 => cur: {1 2 }
level = 3 => saved  {1 2 2}
level = 3, i = 0 => cur: {1 2 2 } skipped 1
level = 3, i = 1 => cur: {1 2 2 } skipped 1
level = 3, i = 2 => cur: {1 2 2 } skipped 1
level = 2, i = 2 => cur: {1 2 2 } -> {1 2 } recovered
level = 1, i = 1 => cur: {1 2 } -> {1 } recovered
level = 1, i = 2 => cur: {1 } skipped 2
level = 0, i = 0 => cur: {1 } -> {} recovered
level = 0, i = 1 => cur: {}
level = 1, i = 0 => cur: {2 }
level = 2, i = 0 => cur: {2 1 } skipped 1
level = 2, i = 1 => cur: {2 1 } skipped 1
level = 2, i = 2 => cur: {2 1 }
level = 3 => saved  {2 1 2}
level = 3, i = 0 => cur: {2 1 2 } skipped 1
level = 3, i = 1 => cur: {2 1 2 } skipped 1
level = 3, i = 2 => cur: {2 1 2 } skipped 1
level = 2, i = 2 => cur: {2 1 2 } -> {2 1 } recovered
level = 1, i = 0 => cur: {2 1 } -> {2 } recovered
level = 1, i = 1 => cur: {2 } skipped 1
level = 1, i = 2 => cur: {2 }
level = 2, i = 0 => cur: {2 2 }
level = 3 => saved  {2 2 1}
level = 3, i = 0 => cur: {2 2 1 } skipped 1
level = 3, i = 1 => cur: {2 2 1 } skipped 1
level = 3, i = 2 => cur: {2 2 1 } skipped 1
level = 2, i = 0 => cur: {2 2 1 } -> {2 2 } recovered
level = 2, i = 1 => cur: {2 2 } skipped 1
level = 2, i = 2 => cur: {2 2 } skipped 1
level = 1, i = 2 => cur: {2 2 } -> {2 } recovered
level = 0, i = 1 => cur: {2 } -> {} recovered
level = 0, i = 2 => cur: {} skipped 2
``````

``````class Solution {
public:
vector<vector<int>> permuteUnique(vector<int>& nums) {
set<vector<int>> res;
permute(nums, 0, res);
return vector<vector<int>> (res.begin(), res.end());
}
void permute(vector<int>& nums, int start, set<vector<int>>& res) {
if (start >= nums.size()) res.insert(nums);
for (int i = start; i < nums.size(); ++i) {
if (i != start && nums[i] == nums[start]) continue;
swap(nums[i], nums[start]);
permute(nums, start + 1, res);
swap(nums[i], nums[start]);
}
}
};
``````

[[1,2,2], [2,1,2], [2,2,1], [2,2,1], [2,1,2]]

``````start = 0, i = 0 => {1 2 2}
start = 1, i = 1 => {1 2 2}
start = 2, i = 2 => {1 2 2}
start = 3 => saved  {1 2 2}
start = 1, i = 2 => {1 2 2} skipped
start = 0, i = 1 => {1 2 2} -> {2 1 2}
start = 1, i = 1 => {2 1 2}
start = 2, i = 2 => {2 1 2}
start = 3 => saved  {2 1 2}
start = 1, i = 2 => {2 1 2} -> {2 2 1}
start = 2, i = 2 => {2 2 1}
start = 3 => saved  {2 2 1}
start = 1, i = 2 => {2 2 1} -> {2 1 2} recovered
start = 0, i = 1 => {2 1 2} -> {1 2 2} recovered
start = 0, i = 2 => {1 2 2} -> {2 2 1}
start = 1, i = 1 => {2 2 1}
start = 2, i = 2 => {2 2 1}
start = 3 => saved  {2 2 1}
start = 1, i = 2 => {2 2 1} -> {2 1 2}
start = 2, i = 2 => {2 1 2}
start = 3 => saved  {2 1 2}
start = 1, i = 2 => {2 1 2} -> {2 2 1} recovered
start = 0, i = 2 => {2 2 1} -> {1 2 2} recovered
``````

``````class Solution {
public:
vector<vector<int>> permuteUnique(vector<int>& nums) {
vector<vector<int>> res;
sort(nums.begin(), nums.end());
permute(nums, 0, res);
return res;
}
void permute(vector<int> nums, int start, vector<vector<int>>& res) {
if (start >= nums.size()) res.push_back(nums);
for (int i = start; i < nums.size(); ++i) {
if (i != start && nums[i] == nums[start]) continue;
swap(nums[i], nums[start]);
permute(nums, start + 1, res);
}
}
};
``````

``````start = 0, i = 0 => {1 2 2}
start = 1, i = 1 => {1 2 2}
start = 2, i = 2 => {1 2 2}
start = 3 => saved  {1 2 2}
start = 1, i = 2 => {1 2 2} skipped
start = 0, i = 1 => {1 2 2} -> {2 1 2}
start = 1, i = 1 => {2 1 2}
start = 2, i = 2 => {2 1 2}
start = 3 => saved  {2 1 2}
start = 1, i = 2 => {2 1 2} -> {2 2 1}
start = 2, i = 2 => {2 2 1}
start = 3 => saved  {2 2 1}
start = 1, i = 2 => {2 2 1} recovered
start = 0, i = 1 => {2 1 2} recovered
start = 0, i = 2 => {2 1 2} skipped
``````

``````class Solution {
public:
vector<vector<int>> permuteUnique(vector<int>& nums) {
vector<vector<int>> res;
sort(nums.begin(), nums.end());
permute(nums, 0, res);
return res;
}
void permute(vector<int>& nums, int start, vector<vector<int>>& res) {
if (start >= nums.size()) res.push_back(nums);
for (int i = start; i < nums.size(); ++i) {
int j = i - 1;
while (j >= start && nums[j] != nums[i]) --j;
if (j != start - 1) continue;
swap(nums[i], nums[start]);
permute(nums, start + 1, res);
swap(nums[i], nums[start]);
}
}
};
``````

``````start = 0, i = 0 => {1 2 2} , j = -1
start = 1, i = 1 => {1 2 2} , j = 0
start = 2, i = 2 => {1 2 2} , j = 1
start = 3 => saved  {1 2 2}
start = 1, i = 2 => {1 2 2} skipped, j = 1
start = 0, i = 1 => {1 2 2} -> {2 1 2}, j = -1
start = 1, i = 1 => {2 1 2} , j = 0
start = 2, i = 2 => {2 1 2} , j = 1
start = 3 => saved  {2 1 2}
start = 1, i = 2 => {2 1 2} -> {2 2 1}, j = 0
start = 2, i = 2 => {2 2 1} , j = 1
start = 3 => saved  {2 2 1}
start = 1, i = 2 => {2 2 1} -> {2 1 2} recovered
start = 0, i = 1 => {2 1 2} -> {1 2 2} recovered
start = 0, i = 2 => {1 2 2} skipped, j = 1
``````

``````class Solution {
public:
vector<vector<int>> permuteUnique(vector<int>& nums) {
vector<vector<int>> res;
sort(nums.begin(), nums.end());
permute(nums, 0, res);
return res;
}
void permute(vector<int>& nums, int start, vector<vector<int>>& res) {
if (start >= nums.size()) res.push_back(nums);
unordered_set<int> st;
for (int i = start; i < nums.size(); ++i) {
if (st.count(nums[i])) continue;
st.insert(nums[i]);
swap(nums[i], nums[start]);
permute(nums, start + 1, res);
swap(nums[i], nums[start]);
}
}
};
``````

``````start = 0, i = 0 => {1 2 2}
start = 1, i = 1 => {1 2 2}
start = 2, i = 2 => {1 2 2}
start = 3 => saved  {1 2 2}
start = 1, i = 2 => {1 2 2} skipped
start = 0, i = 1 => {1 2 2} -> {2 1 2}
start = 1, i = 1 => {2 1 2}
start = 2, i = 2 => {2 1 2}
start = 3 => saved  {2 1 2}
start = 1, i = 2 => {2 1 2} -> {2 2 1}
start = 2, i = 2 => {2 2 1}
start = 3 => saved  {2 2 1}
start = 1, i = 2 => {2 2 1} -> {2 1 2} recovered
start = 0, i = 1 => {2 1 2} -> {1 2 2} recovered
start = 0, i = 2 => {1 2 2} skipped
``````

``````class Solution {
public:
vector<vector<int>> permuteUnique(vector<int>& nums) {
if (nums.empty()) return vector<vector<int>>(1, vector<int>());
set<vector<int>> res;
int first = nums[0];
nums.erase(nums.begin());
vector<vector<int>> words = permuteUnique(nums);
for (auto &a : words) {
for (int i = 0; i <= a.size(); ++i) {
a.insert(a.begin() + i, first);
res.insert(a);
a.erase(a.begin() + i);
}
}
return vector<vector<int>> (res.begin(), res.end());
}
};
``````

``````class Solution {
public:
vector<vector<int>> permuteUnique(vector<int>& nums) {
vector<vector<int>> res{{}};
for (int num : nums) {
for (int k = res.size(); k > 0; --k) {
vector<int> t = res.front();
res.erase(res.begin());
for (int i = 0; i <= t.size(); ++i) {
vector<int> one = t;
one.insert(one.begin() + i, num);
res.push_back(one);
if (i < t.size() && num == t[i]) break;
}
}
}
return res;
}
};
``````

``````class Solution {
public:
vector<vector<int>> permuteUnique(vector<int>& nums) {
vector<vector<int>> res;
sort(nums.begin(), nums.end());
res.push_back(nums);
while (next_permutation(nums.begin(), nums.end())) {
res.push_back(nums);
}
return res;
}
};
``````

Github 同步地址：

https://github.com/grandyang/leetcode/issues/47

Permutations

Next Permutation

Palindrome Permutation II

Number of Squareful Arrays

https://leetcode.com/problems/permutations-ii/

https://leetcode.com/problems/permutations-ii/discuss/18601/Short-iterative-Java-solution

https://leetcode.com/problems/permutations-ii/discuss/18596/A-simple-C%2B%2B-solution-in-only-20-lines

https://leetcode.com/problems/permutations-ii/discuss/18594/Really-easy-Java-solution-much-easier-than-the-solutions-with-very-high-vote

LeetCode All in One 题目讲解汇总(持续更新中…)

（欢迎加入博主的知识星球，博主将及时答疑解惑，并分享刷题经验与总结，快快加入吧～）

|

Venmo 打赏

—|—

 微信打赏 Venmo 打赏
（欢迎加入博主的知识星球，博主将及时答疑解惑，并分享刷题经验与总结，试运营期间前五十位可享受半价优惠～）

×

Help us with donation