zoukankan      html  css  js  c++  java
  • LeetCode_Combination Sum

    Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
    
    The same repeated number may be chosen from C unlimited number of times.
    
    Note:
    
    All numbers (including target) will be positive integers.
    Elements in a combination (a1, a2, � , ak) must be in non-descending order. (ie, a1 ? a2 ? � ? ak).
    The solution set must not contain duplicate combinations.
    For example, given candidate set 2,3,6,7 and target 7, 
    A solution set is: 
    [7] 
    [2, 2, 3] 
    

      

    class Solution {
    public:
        void  DFS(vector<int> &candidates, int target, int start, int sum, vector<int> &tp){
    		if(sum == target){
    			res.push_back(tp);
    			return;
    		}
    		for(int i = start; i< candidates.size(); ++i){
    			if(candidates[i] + sum <= target){
    				tp.push_back(candidates[i]);
    				DFS(candidates, target, i, sum+candidates[i], tp);
    				tp.pop_back();
    			}
    		}
    	}
        vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
            // Start typing your C/C++ solution below
            // DO NOT write int main() function
            res.clear();
    		int len = candidates.size();
    		if(len < 1 || target <1) return res;
    		sort(candidates.begin(), candidates.end());
    		vector<int> tp;
    		DFS(candidates, target, 0, 0, tp);
    		return res;
    		
        }
    private:
    	vector<vector<int>> res;
    };
    

      

  • 相关阅读:
    依赖注入方法
    依赖注入
    用spring来控制反转(ioc)
    ioc控制反转笔记
    写模块的流程例子
    淘淘商城笔记1
    二叉树的前序中序后序遍历
    专题2 二叉树(go)
    专题1:二分查找
    python自动化开发-[第三天]-编码,函数,文件操作
  • 原文地址:https://www.cnblogs.com/graph/p/3322746.html
Copyright © 2011-2022 走看看