๋ฌธ์
์ฝ๋
class Solution {
public:
map<vector<int>,int>visited;
vector<vector<int>>ans;
void dfs(int num, vector<int>&nums,vector<int>current){
sort(current.begin(),current.end());
if(!visited[current]){
ans.push_back(current);
visited[current]=1;
}
if(num>nums.size()) return;
for(int i=num; i<nums.size(); i++){
vector<int>temp=current;
temp.push_back(nums[i]);
dfs(i+1,nums,temp);
}
}
vector<vector<int>> subsetsWithDup(vector<int>& nums) {
for(int i=0; i<nums.size(); i++){
dfs(i,nums,{});
}
sort(ans.begin(),ans.end());
return ans;
}
};
ํ์ด
backtraking ๋ฌธ์ ์ ๋๋ค.
inp: [4,4,4,1,4]
out: [[],[1],[1,4],[1,4,4],[1,4,4,4],[1,4,4,4,4],[4],[4,4],[4,4,4],[4,4,4,4]]
์ ์ฒ๋ผ ์ ๋ ฌ ๋ ์ฑ๋ก ์ถ๋ ฅํด์ผ ํ๊ธฐ ๋๋ฌธ์ ์ ๋ ฌ์ ํด์ฃผ๋๊ฒ ๊ด๊ฑด์ด๋ฉฐ, ์ ๋ map<vector<int>,int>visited ์ ํตํด ๋ฐฉ๋ฌธ ์ฒ๋ฆฌ๋ฅผ ํ์ฌ ์ค๋ณต์ฒ๋ฆฌ๋ฅผ ์ ๊ฑฐํ์ต๋๋ค.
'Algorithm ๐ง๐ปโ๐ป > Leetcode' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Leetcode,c++] Valid Anagram (0) | 2021.11.20 |
---|---|
[Leetcode,c++] Single Number (0) | 2021.11.19 |
[Leetcode,c++] Combination Sum II (0) | 2021.11.16 |
[Leetcode,c++] Rotate Image (0) | 2021.11.15 |
[Leetcode,c++] Permutations II (0) | 2021.11.15 |
[Leetcode,c++] Roman to Integer (0) | 2021.11.15 |
๋๊ธ