zoukankan      html  css  js  c++  java
  • [leetcode]Combination Sum

    裸的dfs

    class Solution {
    public:
        void search(vector<vector<int> >& ans , vector<int>& tmp ,vector<int> &candidates , int target , int sum , int start) {
            if(sum > target) return;
            if(sum == target) {
                ans.push_back(tmp);
                return ;
            }
            
            for(int i = start ; i < candidates.size() ; i++) {
                tmp.push_back(candidates[i]);
                search(ans,tmp,candidates,target,sum+candidates[i] , i);
                tmp.pop_back();
            }
        }
        vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
            vector<vector<int> > ans;
            vector<int> tmp;
            sort(candidates.begin() , candidates.end());
            search(ans,tmp,candidates,target, 0 , 0);
            return ans;
        }
    };
  • 相关阅读:
    pku2351 Colored Sticks
    JSOI2010 满汉全席
    享元模式
    适配器模式
    合成模式
    原型模式
    创建型设计模式
    建造者模式
    装饰模式
    单例模式
  • 原文地址:https://www.cnblogs.com/x1957/p/3513389.html
Copyright © 2011-2022 走看看