Given a collection of numbers that might contain duplicates, return all possible unique permutations.
給予一個集合,裡面的元素是數字,可能包含重複的數字。回傳所有不重複的組合。
Input: [1,1,2]
Output:
[
[1,1,2],
[1,2,1],
[2,1,1]
]
class Solution {
public:
vector<vector<int>> permuteUnique(vector<int>& nums) {
int size = nums.size();
vector<vector<int>> ans;
sort(nums.begin(),nums.end());
do
{
vector<int> temp;
for(int i=0;i<size;i++)
temp.push_back(nums[i]);
ans.push_back(temp);
}
while(next_permutation(nums.begin(),nums.end()));
return ans;
}
};
LeetCode
C++
1. Two Sum
Nov 15, 2023You are given an array of positive integers arr. Perform some operations (possibly none) on arr so that it satisfies these conditions:
Nov 15, 2023Given a string s, return the number of homogenous substrings of s. Since the answer may be too large, return it modulo 109 + 7.
Nov 9, 2023There are n computers numbered from 0 to n - 1 connected by ethernet cables connections forming a network where connections[i] = [ai, bi] represents a connection between computers ai and bi. Any computer can reach any other computer directly or indirectly through the network.
Nov 9, 2023or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up