当前位置:编程学习 > C/C++ >>

leetcode Subsets II

Take advantage of the order to eliminate the duplicate sets. The following is the code: 
 
 
class Solution {  
 public:  
  vector<vector<int>> res;  
  void subsets(vector<int>& S, int start, vector<int>& numset) {  
    if (start >= S.size())   
      return;    
    numset.push_back(S[start]);  
    res.push_back(numset);  
    subsets(S, start + 1, numset);  
    numset.pop_back();  
      
    while (start + 1 < S.size() && S[start] == S[start + 1])  
      ++start;  
    if (start + 1 < S.size())  
      subsets(S, start + 1, numset);  
  }  
  vector<vector<int> > subsetsWithDup(vector<int> &S) {  
    // Note: The Solution object is instantiated only once and is reused by each test case.  
    res.clear();  
    vector<int> numset;  
    res.push_back(numset);  
    sort(S.begin(), S.end());  
    subsets(S, 0, numset);  
    return res;  
  }  
};  

 

 
 
补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,