240
笔下虽有千言,胸中实无一策

30 Day Challenge Day 14 | Leetcode 90. Subsets II

题解

Medium | Backtracking

class Solution {
public:
    vector<vector<int>> subsetsWithDup(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        vector<vector<int>> sols;
        vector<int> sol;
        helper(nums, 0, sol, sols);
        return sols;
    }
    
    void helper(vector<int>& nums, int pos, vector<int>& sol, vector<vector<int>>& sols) {
        sols.push_back(sol);
        
        for(int i = pos; i < nums.size(); i++) {
            if(i > pos && nums[i] == nums[i-1]) continue;
            sol.push_back(nums[i]);
            helper(nums, i+1, sol, sols);
            sol.pop_back();
        }
    }
};
posted @ 2020-09-29 12:58  CasperWin  阅读(89)  评论(0编辑  收藏  举报