【LeetCode每天一题】Combination Sum(组合和)
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]
] 思路
对于在数组中进行组合查找这种类似的问题,我们可以使用递归来进行解决。因为其中同一个数字可以重复利用多次,所以对于递归的写法应该注意。解决思路主要看代码的注释。
解决代码
class Solution(object):
def combinationSum(self, nums, target):
"""
:type candidates: List[int]
:type target: int
:rtype: List[List[int]]
"""
if len(nums) < 1: # nums中数目小于1时直接返回
return []
res = [] # 保存结果
nums.sort() # 排序之后可以减少递归次数
self.get_res(nums, res, 0, [],target) # 进行递归
return res def get_res(self, nums, res, index, path, target):
if target == 0: # 递归结束条件,当target等于0时,表示满足。将结果集进行添加。
return res.append(path)
if target < 0: # 递归结束条件,不满足直接进行返回。
return
for i in range(index, len(nums)): # 因为在数组中每一个数字可以多次重复利用,所以index表示从第几个元素开始进行执行。
if target < nums[index]: # 如果当前首元素大于target时,直接终止,避免不必要的递归
return
self.get_res(nums, res, i, path+[nums[i]], target-nums[i])
【LeetCode每天一题】Combination Sum(组合和)的更多相关文章
- leetcode第39题--Combination Sum II
题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...
- leetcode第38题--Combination Sum
题目: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C ...
- Leetcode 39 40 216 Combination Sum I II III
Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...
- LeetCode(40) Combination Sum II
题目 Given a collection of candidate numbers (C) and a target number (T), find all unique combinations ...
- LeetCode笔记:39. Combination Sum
题目描述 给定一个无重复的正整数数组 candidates 和一个正整数 target, 求所有和为 target 的 candidates 中数的组合中.其中相同数的不同顺序组合算做同一种组合,ca ...
- LeetCode(39) Combination Sum
题目 Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C w ...
- leetcode个人题解——#39 Combination Sum
思路:先对数据进行排序(看评论给的测试数据好像都是有序数组了,但题目里没有给出这个条件),然后回溯加剪枝即可. class Solution { public: ; vector<vector& ...
- LeetCode:40. Combination Sum II(Medium)
1. 原题链接 https://leetcode.com/problems/combination-sum-ii/description/ 2. 题目要求 给定一个整型数组candidates[ ]和 ...
- Leetcode 之 Combination Sum系列
39. Combination Sum 1.Problem Find all possible combinations of k numbers that add up to a number n, ...
随机推荐
- Spring学习笔记--代理
静态代理 1.静态代理的角色分析(UML画图推荐使用StarUML软件) 抽象角色---一般使用接口或者抽象类来实现. 真实角色---被代理的角色. 代理角色---代理真实角色—代理真是角色后一般会做 ...
- Linux CentOS中防火墙的关闭及开启端口
注:CentOS7之前用来管理防火墙的工具是iptable,7之后使用的是Firewall 样例:在CentOS7上安装tomcat后,在linux本机上可以访问tomcat主页,http://ip: ...
- vue 实现聊天框滚动到底
在需要出现滚动条的 DOM上添加 v-scroll 属性: <div class="chat-box" v-scroll="{auto: true}"&g ...
- 转载:Maplab开源VI-SLAM框架介绍
https://blog.csdn.net/ccfiricen/article/details/79089694
- window10装机 nvem简介,针对于 联想R720系列
1.nvem格式的硬盘比较新,传统的老毛桃,大白菜,并不能有效使用. 2.对于镜像类 goust类,仍然具备 硬盘兼容性差(对于gpt,MAR,uefi并不够智能)成功率极低,容易丢失一些关键文件,造 ...
- [No000013B]初识Ildasm.exe——IL反编译的实用工具
Ildasm.exe 概要: 一.前言: 微软的IL反编译实用程序——Ildasm.exe,可以对可执行文件(ex,经典的控制台Hello World 的 exe 可执行文件)抽取出 IL 代码,并且 ...
- 【模板】HDU 1541 树状数组
http://acm.hdu.edu.cn/showproblem.php?pid=1541 题意:给你一堆点,每个点右一个level,为其右下方所有点的数量之和,求各个level包含的点数. 题解: ...
- Linear transformations. 线性变换与矩阵的关系
0.2.2 Linear transformations. Let U be an n-dimensional vector space and let V be an m-dimensional v ...
- [daily][qemu][libvirt] 使用libvirt管理qemu
别人创建的虚拟机.用libvirt做的配置. 我一直是手写qemu脚本的,不会用virtsh,所以,学一下. ------------------ 先来个arch的文档: https://wiki.a ...
- 转:HashMap实现原理分析(面试问题:两个hashcode相同 的对象怎么存入hashmap的)
原文地址:https://www.cnblogs.com/faunjoe88/p/7992319.html 主要内容: 1)put 疑问:如果两个key通过hash%Entry[].length得 ...