Last active
February 22, 2018 01:53
-
-
Save sourabh2k15/14d40998a699cf81e64491b6caa1fc75 to your computer and use it in GitHub Desktop.
90-Subsets-2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
private: | |
vector<vector<int>> powerset; | |
vector<int> subset; | |
public: | |
vector<vector<int>> subsetsWithDup(vector<int>& nums) { | |
sort(nums.begin(), nums.end()); //change 1 | |
backtrack(nums, 0); | |
return powerset; | |
} | |
void backtrack(vector<int>& nums, int start){ | |
powerset.push_back(subset); | |
for(int i = start; i < nums.size(); i++){ | |
if(i > start && nums[i] == nums[i-1]) continue; //change 2, skip over duplicates | |
subset.push_back(nums[i]); | |
backtrack(nums, i+1); | |
subset.pop_back(); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment