【Combination Sum 】cpp

题目:

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:
    vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
            vector<vector<int> > ret;
            vector<int> tmp;
            int sum = 0;
            std::sort(candidates.begin(), candidates.end());
            Solution::dfs(ret, tmp, sum, candidates, 0, candidates.size()-1, target);
            return ret;
    }
    static void dfs(
            vector<vector<int> >& ret,
            vector<int>& tmp,
            int &sum,
            vector<int>& candidates,
            int begin,
            int end,
            int target )
    {
            if ( sum>target ) return;
            if ( sum==target )
            {
                ret.push_back(tmp);
                return;
            }
            for ( int i=begin; i<=end; ++i )
            {
                if ( sum+candidates[i]<=target )
                {
                    sum += candidates[i];
                    tmp.push_back(candidates[i]);
                    Solution::dfs(ret, tmp, sum, candidates, i, end, target);
                    sum -= candidates[i];
                    tmp.pop_back();
                }
            }
    }
};

tips:

采用深搜模板:

1. 终止条件sum>target

2. 加入解集条件sum==target

3. 遍历当前层所有分支(如果满足sum+candidates[i]<target,则还可以再往上加candidates[i];注意,这里传入下一层的begin下标为i,因为要求元素可以无限多重复)

4. 由于传入下一层始终满足begin<=end,因此不要在终止条件中加入(begin>end)

时间: 2024-10-01 05:01:54

【Combination Sum 】cpp的相关文章

【Path Sum】cpp

题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example:Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ 7 2 1 return true,

【Two Sum】cpp

题目: Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note t

【Minimum Path Sum】cpp

题目: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time. 代码: class Solution { public:

【Binary Tree Maximum Path Sum】cpp

题目: Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example:Given the below binary tree, 1 / 2 3 Return 6. 代码: /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode

【Maximum Subarray 】cpp

题目: Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [−2,1,−3,4,−1,2,1,−5,4],the contiguous subarray [4,−1,2,1] has the largest sum = 6. 代码: class Solution { public:

【Add binary】cpp

题目: Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 代码: class Solution { public: string addBinary(string a, string b) { std::string result; std::string::reverse_iter

【Word Break】cpp

题目: Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. For example, givens = "leetcode",dict = ["leet", "code"]. Return true becau

【Sudoku Solver】cpp

题目: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character '.'. You may assume that there will be only one unique solution. A sudoku puzzle... ...and its solution numbers marked in red. 代码: cla

【Subsets II】cpp

题目: Given a collection of integers that might contain duplicates, nums, 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 nums = [1,2,2], a sol