29.Combination Sum(和为sum的组合)

Level:

??Medium

题目描述:

Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target.

The same repeated number may be chosen from candidates unlimited number of times.

Note:

  • All numbers (including target) will be positive integers.
  • The solution set must not contain duplicate combinations.

Example 1:

Input: candidates = [2,3,6,7], target = 7,
A solution set is:
[
  [7],
  [2,2,3]
]

Example 2:

Input: candidates = [2,3,5], target = 8,
A solution set is:
[
  [2,2,2,2],
  [2,3,3],
  [3,5]
]

思路分析:

? 先对数组进行排序,方便后面递归回溯的过程中进行剪枝。然后设置一个变量sum,记录当前序列的数字和,如果sum的值等于target那么当前序列就是结果的一种,我们利用回溯的思想,找出所有满足要求得解。

代码:

public class Solution{
    List<List<Integer>>res=new ArrayList<>();
    public List<List<Integer>>combinationSum(int []candidates,int target){
        if(candidates==null||candidates.length==0)
            return res;
        Arrays.sort(candidates);//排序,方便后面剪枝
        find(candidates,0,0,target,new ArrayList<>());
        return res;
    }
    public void find(int[]candidates,int start,int sum,int target,ArrayList<Integer>list){
        if(sum==target){
            res.add(new ArrayList<Integer>(list));
            return;
        }
        for(int i=start;i<candidates.length;i++){
            if(sum+candidates[i]<=target){
                list.add(candidates[i]);
               find(candidates,i,sum+candidates[i],target,list);
                list.remove(Integer.valueOf(candidates[i]));
            }else{
                break; //剪枝
            }
        }
    }
}

原文地址:https://www.cnblogs.com/yjxyy/p/10797665.html

时间: 2024-08-30 14:46:39

29.Combination Sum(和为sum的组合)的相关文章

UVALive8518 Sum of xor sum

题目链接:https://vjudge.net/problem/UVALive-8518 题目大意: 给定一个长度为 $N$ 的数字序列 $A$,进行 $Q$ 次询问,每次询问 $[L,R]$,需要回答这个区间内的子序列的所有子序列的异或和之和. $1 \le N,Q \le 100000$ $0 \le A[i] \le 1000000$ 知识点: 前缀和 解题思路: 将序列中的每一个数转成二进制(不超过 $20$ 位),逐位考虑. 根据序列中的数字用二进制表示时在该位上为 $1$ 或 $0$

hdu 3473 Minimum Sum(划分树-sum操作)

划分树.只是考虑求当前区间大于第k值的值得和,和小于第k值的和.显然可以在查询的时候直接搞出来.sum[d][i]表示第d层子区间l,r种l-i的和.写错了一个下标,检查了半辈子... #include<algorithm> #include<iostream> #include<cstring> #include<vector> #include<cstdio> #include<cmath> #include<queue&g

c++谭浩强教材教学练习例题1.2 求两数之和 为什么sum=a+b;sum的值为65538

第一章 #include <iostream>using namespace std; int main(){ int a,b,sum; sum=a+b; cin>>a>>b; cout<<"a+b="<<sum<<endl; return 0;} //原因sum=a+b;此语句位置不对,变量a,b在没有赋值时就被相加,超出int最大值范围.只能得到最大值65538 #include <iostream>

dfs --path sum 问题 本质上就是组合问题(有去重)

135. 数字组合 中文 English 给定一个候选数字的集合 candidates 和一个目标值 target. 找到 candidates 中所有的和为 target 的组合. 在同一个组合中, candidates 中的某个数字不限次数地出现. 样例 样例 1: 输入: candidates = [2, 3, 6, 7], target = 7 输出: [[7], [2, 2, 3]] 样例 2: 输入: candidates = [1], target = 3 输出: [[1, 1,

112. Path Sum 113. Path Sum II

回溯法的典型 1.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 t

two sum, three sum和four sum问题

1. two sum问题 给定一组序列:[-4 -6 5 1 2 3 -1 7],然后找出其中和为target的一对数 简单做法:两层循环遍历,时间复杂度为n^2 升级版:对给定的序列建立一个hash表,然后只需要外层一层循环就可以了,时间复杂度为n 2. three sum问题 给定一组序列:[-4 -6 5 1 2 3 -1 7],然后找出其中和为target的三个数 简单做法:三层for循环,时间复杂度为n^3 升级版: 如果看做是two sum升级,可以先从小到大排序,时间复杂度nlog

LeetCode 39. Combination Sum (组合的和)

Given a set of candidate numbers (C) (without duplicates) 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 (in

[Leetcode] Combination Sum II

Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note: All numbers (including target) will be posi

【leetcode】Combination Sum III(middle)

Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. Ensure that numbers within the set are sorted in ascending order. Example 1