带重复元素的排列

带重复元素的排列

带重复元素的排列

code

class Solution {
public:
/**
* @param nums: A list of integers.
* @return: A list of permutations.
*/
vector<vector<int> > permute(vector<int> nums) {
// write your code here
vector<vector<int> > result;
int size = nums.size();
if(size == 0) {
result.push_back(nums);
return result;
} permute(nums, 0, size, result); return result;
} void permute(vector<int> &nums, int begin, int end, vector<vector<int> > &result) {
if(begin == end) {
if(!isExist(nums, result)) {
result.push_back(nums);
}
}
else {
for(int i=begin; i<end; i++) {
int temp = nums[i];
nums[i] = nums[begin];
nums[begin] = temp; permute(nums, begin+1, end, result); temp = nums[i];
nums[i] = nums[begin];
nums[begin] = temp;
}
}
} bool isExist(vector<int> &nums, vector<vector<int> > &result) {
int size = result.size();
if(size == 0)
return false;
for(int i=0; i<size; i++) {
if(isSameNums(nums, result[i])) {
return true;
}
}
return false;
} bool isSameNums(vector<int> &nums1, vector<int> &nums2) {
int size = nums1.size();
for(int i=0; i<size; i++) {
if(nums1[i] != nums2[i]) {
return false;
}
}
return true;
}
};
05-27 02:34