add: 220512 [cpp]

This commit is contained in:
Eat-Swap 2022-05-12 20:32:01 +08:00
parent 9f5356b1db
commit 326523b170
Signed by: Eatswap
GPG Key ID: BE661106A1F3FA0B
2 changed files with 33 additions and 1 deletions

32
cpp/2205/220512.cpp Normal file
View File

@ -0,0 +1,32 @@
#include <vector>
#include <algorithm>
#include <cstdio>
/**
* 47. Permutations II
* Given a collection of numbers, nums, that might contain duplicates, return all possible unique permutations in any order.
*/
class Solution {
public:
static std::vector<std::vector<int>> permuteUnique(std::vector<int>& nums) {
std::sort(nums.begin(), nums.end());
std::vector<std::vector<int>> ret {nums};
while (std::next_permutation(nums.begin(), nums.end())) {
ret.push_back(nums);
}
return ret;
}
};
int main() {
std::vector<int> arg {1, 1, 2};
auto r = Solution::permuteUnique(arg);
for (const auto& i : r) {
for (const auto& j : i) {
std::printf("%d ", j);
}
std::putchar('\n');
}
return 0;
}

View File

@ -3,4 +3,4 @@ PROJECT(2205)
SET(CMAKE_CXX_STANDARD 23) SET(CMAKE_CXX_STANDARD 23)
ADD_EXECUTABLE(2205 220512-CN.cpp) ADD_EXECUTABLE(2205 220512.cpp)