zoukankan      html  css  js  c++  java
  • lintcode:子集 & 带重复元素的子集

    地址:

    http://lintcode.com/zh-cn/problem/subsets/

    http://lintcode.com/zh-cn/problem/subsets-ii/

    子集

    其实就是一颗子集树

    class Solution {
    public:
        vector<vector<int>> res;
        vector<int> judge;
        int len;
        /*
         * @param nums: A set of numbers
         * @return: A list of lists
         */
        vector<vector<int>> subsets(vector<int> &nums) {
            // write your code here
            len = nums.size();
            for(int i=0;i<len;i++){
                judge.push_back(0);
            }
            backtrack(0,nums);
            return res;
        }
        
        void display(vector<int> &nums){
            vector<int> cur;
            for(int i=0;i<len;i++){
                if(judge[i]==1){
                    cur.push_back(nums[i]);
                }
            }
            res.push_back(cur);
        }
        
        void backtrack(int t,vector<int> &nums){
            if(t >= len){
                display(nums);
                return;
            }
            
            for(int i=0;i<=1;i++){
                judge[t] = i;
                backtrack(t+1,nums);
            }
        } 
    };

    带重复元素的子集

    筛选一下分支,排序数组,然后相同的元素只能出现1,...,0...或者全1、全0的形式,也就是不能有101这样的情况,

    class Solution {
    public:
        vector<vector<int>> res;
        vector<int> judge;
        int len;
        /*
         * @param nums: A set of numbers.
         * @return: A list of lists. All valid subsets.
         */
        vector<vector<int>> subsetsWithDup(vector<int> &nums) {
            // write your code here
            len = nums.size();
            sort(nums.begin(),nums.end());
            for(int i=0;i<len;i++){
                judge.push_back(0);
            }
            backtrack(0,nums);
            return res;
        }
    
       void display(vector<int> &nums){
            vector<int> cur;
            for(int i=0;i<len;i++){
                if(judge[i]==1){
                    cur.push_back(nums[i]);
                }
            }
            res.push_back(cur);
        }
        
        bool isOk(int t,vector<int> &nums){
            int count=1;
            for(int i=0;i<t;i++){
                if(nums[i]==nums[i+1]){
                    if(judge[i]==0){
                        count = 0;
                    } else {
                        count = 1;
                    }
                    
                    if(!count && judge[i+1] ){
                        return false;
                    }
                } else {
                    count=1;
                }
            }
            return true;
        }
        
        void backtrack(int t,vector<int> &nums){
            if(t >= len){
                display(nums);
                return;
            }
            
            for(int i=0;i<=1;i++){
                judge[t] = i;
                if(isOk(t,nums)){
                    backtrack(t+1,nums);
                }
            }
        }
        
    };
  • 相关阅读:
    JVM收藏的文章
    【转】Mysql相关子查询&&MySQL获取分组后的TOP N记录
    【转】JVM--内存区域划分
    【转】Nginx location写法
    【转】Dockerfile
    CORS web.xml 里配置
    分布式事务
    maven+dubbo+SpringMVC 项目搭建
    dubbo 报错
    多重背包问题:POJ2392
  • 原文地址:https://www.cnblogs.com/rimochiko/p/8440771.html
Copyright © 2011-2022 走看看