# ＃16 Permutations II

Given a list of numbers with duplicate number in it. Find all uniquepermutations.

Example

For numbers [1,2,2] the unique permutations are:

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

Mycode（AC ＝ 290ms）：

class Solution {
public:
/**
* @param nums: A list of integers.
* @return: A list of unique permutations.
*/
vector<vector<int> > permuteUnique(vector<int> &nums) {
// write your code here
vector<vector<int>> ans;
if (nums.size() == 0) return ans;

vector<int> sofar;
unordered_set<int> visited;
permute(ans, sofar, nums, visited, answered);
return ans;
}

void permute(vector<vector<int>>& ans,
vector<int>& sofar,
vector<int>& nums,
unordered_set<int>& visited,
{
if (sofar.size() == nums.size()) {
ans.push_back(sofar);
}
return;
}

for (int i = 0; i < nums.size(); i++) {
if (visited.find(i) == visited.end()) {
sofar.push_back(nums[i]);
visited.insert(i);

permute(ans, sofar, nums, visited, answered);

sofar.pop_back();
visited.erase(i);
}
}
}
};

• 本文已收录于以下专栏：

举报原因： 您举报文章：＃16 Permutations II 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)