zoukankan      html  css  js  c++  java
  • 40. 组合总和 II

    dfs就好了

    先去重一下

    class Solution {
    public:
        vector<vector<int> > ret;
        int cnt[55], n;
        void dfs(vector<int> candidates, int target, int k, vector<int> v)
        {
            if (target < 0) return;
            if(k == n)
            {
                if(target == 0)
                    ret.push_back(v);
                return;
            }
    
            for(int i = 0; i <= cnt[candidates[k]]; i++)
            {
                if(i == 0) 
                    dfs(candidates, target, k + 1, v);
                else
                {
                    for(int j = 1; j <= i; j++)
                        v.push_back(candidates[k]);
                    dfs(candidates, target - candidates[k] * i, k + 1, v);
                    for(int j = 1; j <= i; j++)
                        v.erase(v.end() - 1);
                }        
            }
        }
    
    
        vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
    
            memset(cnt, 0, sizeof(cnt));
            vector<int> v;
            int length = candidates.size();
            for(int i = 0; i < length; i++)
                cnt[candidates[i]]++;
            sort(candidates.begin(), candidates.end());
            candidates.erase(unique(candidates.begin(), candidates.end()), candidates.end());   
            n = candidates.size();
            dfs(candidates, target, 0, v);
    
    
            return ret;
    
    
    
        }
    };
    自己选择的路,跪着也要走完。朋友们,虽然这个世界日益浮躁起来,只要能够为了当时纯粹的梦想和感动坚持努力下去,不管其它人怎么样,我们也能够保持自己的本色走下去。
  • 相关阅读:
    [Oracle]Oracle的闪回归档
    【zabbix】snmp监控linux主机
    XFS文件系统
    PostgreSQL的使用向导
    PostgreSQL 12 YUM安装
    011.MySQL双主多从+Keepalived配置
    010.MySQL-Keepalived搭配脚本04
    009.MySQL-Keepalived搭配脚本03
    008.MySQL-Keepalived搭配脚本02
    007.MySQL-Keepalived搭配脚本01
  • 原文地址:https://www.cnblogs.com/WTSRUVF/p/15364349.html
Copyright © 2011-2022 走看看