• LeetCode_Subsets II


    Given a collection of integers that might contain duplicates, S, return all possible subsets.
    
    Note:
    
    Elements in a subset must be in non-descending order.
    The solution set must not contain duplicate subsets.
    For example,
    If S = [1,2,2], a solution is:
    

      

    [
      [2],
      [1],
      [1,2,2],
      [2,2],
      [1,2],
      []
    ]

    DFS :

    class Solution {
    public:
        void  DFS(vector<int> &S, vector<int> &temp,int n, int size,int start)
        {
        
            if(n == size)
            {
                result.push_back(temp);
                return ;
            }
            if(n > size)
                return ;
            
            for(int i = start; i< len ;i++)
            {
                if(i != start && S[i] == S[i-1]) 
                      continue ;
                if(flag[i] == false)
                {
                    flag[i] = true;
                    temp.push_back(S[i]);
                    DFS(S, temp, n+1, size,i+1);
                    temp.pop_back();
                    flag[i] = false;
                }
            }
        
        }
        vector<vector<int> > subsetsWithDup(vector<int> &S) {
            // Start typing your C/C++ solution below
            // DO NOT write int main() function
            result.clear();
            len = S.size();
            flag.resize(len,false);
            vector<int> temp;
            result.push_back(temp) ;
            sort(S.begin(), S.end());
            
            for(int i = 1; i <= len ; i++)
                 DFS(S, temp,0, i,0);
                 
            return result;
        }
    private:
        vector<vector<int> > result ;
        vector<bool> flag;
        int len;
    };

     解释下这句:”if(i != start && S[i] == S[i-1]) continue ; 这句话保证每个循环进入的元素不会有重复。start : 保证所有的元素进入的顺序为从左往右~

    重写后的代码:

    class Solution {
    public:
    void DFS(vector<int> &S, vector<int> &ans, int size, int currentPos)
        {
        
            if(ans.size() == size ){
                res.push_back(ans);
                return;
            }
            
            for(int i = currentPos; i< S.size(); ++i)
            {
                if(i != currentPos && S[i] == S[i-1]) continue;    
                ans.push_back(S[i]);
                DFS(S, ans, size, i+1);
                ans.pop_back();
            }
        }
        vector<vector<int> > subsetsWithDup(vector<int> &S) {
            // Start typing your C/C++ solution below
            // DO NOT write int main() function
            res.clear();
            sort(S.begin(), S.end());
            vector<int> emp;
            res.push_back(emp);
            
            for(int i = 1; i <= S.size(); ++i)
            {
                vector<int> ans;
                DFS(S, ans, i, 0);
            }
            
            return res;
        }
    private:
        vector<vector<int>> res;
    };
  • 相关阅读:
    IntelliJ IDEA 常用设置讲解
    Maven
    FileStram文件正由另一进程使用,该进程无法访问该文件,解决方法
    IIS 调用Microsoft.Office.Interop.Word.Documents.Open 返回为null
    .NET 中的 async/await 异步编程
    PHP表单验证内容是否为空
    PHP中的魔术变量
    PHP中的function函数详解
    PHP中的循环while、do...while、for、foreach四种循环。
    利用switch语句进行多选一判断。
  • 原文地址:https://www.cnblogs.com/graph/p/3216589.html
Copyright © 2020-2023  润新知