给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。

candidates 中的数字可以无限制重复被选取。

说明:

  • 所有数字(包括 target)都是正整数。
  • 解集不能包含重复的组合。

示例 1:

输入: candidates = [2,3,6,7], target = 7,
所求解集为:
[
[7],
[2,2,3]
]

示例 2:

输入: candidates = [2,3,5], target = 8,
所求解集为:
[
  [2,2,2,2],
  [2,3,3],
  [3,5]
] 解题思路:
先排序,然后递归求解
代码如下:
class Solution:
def Solver(self, res, path, candidates, target, idx):
for i in range(idx, len(candidates)):
new_target = target - candidates[i]
if new_target < 0:
return
else:
if new_target == 0:
res.append(path + [candidates[i]])
else:
self.Solver(res, path + [candidates[i]], candidates,
new_target, i) def combinationSum(self, candidates, target):
"""
:type candidates: List[int]
:type target: int
:rtype: List[List[int]]
"""
path = []
res = []
candidates = sorted(candidates)
self.Solver(res, path, candidates, target, 0)
return res

leetcode第39题:组合综合的更多相关文章

  1. 【JavaScript】Leetcode每日一题-组合总和4

    [JavaScript]Leetcode每日一题-组合总和4 [题目描述] 给你一个由 不同 整数组成的数组 nums ,和一个目标整数 target .请你从 nums 中找出并返回总和为 targ ...

  2. leetcode第39题--Combination Sum II

    题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...

  3. 【LeetCode】39. 组合总和

    39. 组合总和 知识点:递归:回溯:组合:剪枝 题目描述 给定一个无重复元素的正整数数组 candidates 和一个正整数 target ,找出 candidates 中所有可以使数字和为目标数  ...

  4. LeetCode第[18]题(Java):4Sum 标签:Array

    题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...

  5. LeetCode第[1]题(Java):Two Sum 标签:Array

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  6. LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2

    题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...

  7. LeetCode第[1]题(Java):Two Sum (俩数和为目标数的下标)——EASY

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  8. leetcode 第188题,我的解法,Best Time to Buy and Sell Stock IV

    <span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255) ...

  9. leetcode第37题--Count and Say

    题目:(据说是facebook的面试题哦) The count-and-say sequence is the sequence of integers beginning as follows:1, ...

随机推荐

  1. docker 基本操作

    # 常用命令   docker run 镜像   docker images 查看所有镜像   docke ps  查看运行中的容器 docker ps -a 列出所有容器     docker st ...

  2. chmod 没有x权限怎么办

    解决方法1:    # /lib64/ld-linux-x86-64.so.2 /bin/chmod 755 /bin/chmod   //linux动态命令库   解决方法2:方法2提到的两种方法形 ...

  3. python记录_day22 序列化

    序列化是指把内存里的数据类型转换成字符串,以使其能存储到硬盘或通过网络传输到远程,因为硬盘和网络传输时只能接受bytes 一.pickle 把python对象写入到文件中的一种解决方案,但是写入到文件 ...

  4. linux导出sql数据

    1. 导出数据库的数据 在linux命令行下输入 mysqldump -u userName -p  dabaseName  > fileName.sql 在linux命令行下输入 2. 导出表 ...

  5. hadoopMR自定义输入格式

    输入格式 1.输入分片与记录  2.文件输入  3.文本输入  4.二进制输入  5.多文件输入  6.数据库格式输入 详细的介绍:https://blog.csdn.net/py_123456/ar ...

  6. 安装sql 2008步骤以及所遇到的问题

    下载网址:http://www.xiazaiba.com/html/4610.html 安装步骤: 1.  在Windows7操作系统系,启动Microsoft SQL 2008安装程序后,系统兼容性 ...

  7. Leetcode 144

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...

  8. 使用JQuery 合并两个 json 对象

    一,保存object1和2合并后产生新对象,若2中有与1相同的key,默认2将会覆盖1的值 var object = $.extend({}, object1, object2); 二,将2的值合并到 ...

  9. dubbo 框架小结

    1. dubbo:protocol Dubbo缺省协议采用单一长连接和NIO异步通讯,适合于小数据量大并发的服务调用,以及服务消费者机器数远大于服务提供者机器数的情况. <dubbo:proto ...

  10. JBoss/WildFly 初步安装配置教程

    1.下载 Redhat的JBoss与Oracle的Weblogic.IBM的WebSphere并称三大JAVA EE中间件. JBoss AS是JBoss的开源版本,JBoss EAP是JBoss的企 ...