【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, ...
随机推荐
- python 中面向对象的概念
原文 域和作用空间 本地域,函数域(nonlocal)和 全局域(global) def scope_test(): def do_local(): spam = "local spam&q ...
- .net core 支持apk下载
在 app.UseStaticFiles(); 后面加上 app.UseStaticFiles(new StaticFileOptions { //FileProvider = new Physica ...
- 180714、JRebel插件安装配置与破解激活(多方案)详细教程
JRebel 介绍 IDEA上原生是不支持热部署的,一般更新了 Java 文件后要手动重启 Tomcat 服务器,才能生效,浪费不少生命啊.目前对于idea热部署最好的解决方案就是安装JRebel插件 ...
- C#调用VB进行简繁转换
首先在C#项目中引用Microsoft.VisualBasic.dll,版本自己选择合适的 然后在项目中添加引用:using Microsoft.VisualBasic; 转换: 转为繁体: outp ...
- 利用System.Uri转URL为绝对地址
在使用ASPOSE.Word生成Word文档时可以通过InsertHtml(html)来将图文信息写入Word文档(图片内嵌),但要求html里图片的src是绝对全路径,所以需要对html进行转化. ...
- base标签浏览器兼容问题
<% String path = request.getContextPath(); String basePath = request.getScheme() + "://" ...
- css学习_css背景属性及其应用
css背景属性及其应用 1.背景 2.背景简写 3.背景透明(css3) 4.背景缩放(css3) 5.多背景图片(css3) 6.凹凸文字效果
- 524 (Div. 2) Masha and two friends
Codeforces Round #524 (Div. 2) C. Masha and two friends 题目链接 题意:较为简单,初始给定这个白黑相交的格子,第一遍把坐标范围内的全部涂白,第二 ...
- python 过滤掉字符串中的回车符与换行符(\t\n)
我们在文本数据预处理前,要将数据统一整理成需要的格式,其中有回车(\t)或者(\n)符号,会对我们的数据保存有影响,那么就需要将其过滤掉. 比较简单的方法,用replace()将这些符号替换为空,一定 ...
- CSS3 transform 属性
CSS3 transform 属性 语法: transform: none|transform-functions; 值 描述 none 定义不进行转换. matrix(n,n,n,n,n,n) 定义 ...