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(组合和)的更多相关文章

  1. leetcode第39题--Combination Sum II

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

  2. leetcode第38题--Combination Sum

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

  3. 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 ...

  4. LeetCode(40) Combination Sum II

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

  5. LeetCode笔记:39. Combination Sum

    题目描述 给定一个无重复的正整数数组 candidates 和一个正整数 target, 求所有和为 target 的 candidates 中数的组合中.其中相同数的不同顺序组合算做同一种组合,ca ...

  6. LeetCode(39) Combination Sum

    题目 Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C w ...

  7. leetcode个人题解——#39 Combination Sum

    思路:先对数据进行排序(看评论给的测试数据好像都是有序数组了,但题目里没有给出这个条件),然后回溯加剪枝即可. class Solution { public: ; vector<vector& ...

  8. LeetCode:40. Combination Sum II(Medium)

    1. 原题链接 https://leetcode.com/problems/combination-sum-ii/description/ 2. 题目要求 给定一个整型数组candidates[ ]和 ...

  9. Leetcode 之 Combination Sum系列

    39. Combination Sum 1.Problem Find all possible combinations of k numbers that add up to a number n, ...

随机推荐

  1. 7.5爬取猫眼Top100电影名单

    2018-7-5 20:22:57 还有有一丢丢成就感!以后可以爬取简单网站了!比如妹子图片,只是现在不知道咋下载! 正则还是刚看,要多去用正则!正则很强大的东西! #!/usr/bin/env py ...

  2. 客户端TortoiseSVN的安装及使用方法 (申明:来源于网络)

    客户端TortoiseSVN的安装及使用方法 (申明:来源于网络) 地址:http://blog.chinaunix.net/uid-27004869-id-4112057.html

  3. poj3080 Blue Jeans【KMP】【暴力】

    Blue Jeans Time Limit: 1000MS   Memory Limit: 65536K Total Submissions:21746   Accepted: 9653 Descri ...

  4. 不存在具有键“test”的“IEnumerable<SelectListItem>”类型的 ViewData 项。

    x 很简单的一个问题,有时候提示语已经写得很清楚了, 但是自己那时候就是"鬼迷心窍"了吧··· 解决方案 public PartialViewResult Edit() { vie ...

  5. JavaScript 引入方式 语言规范 语言基础 数据类型 常用方法 数组 if_else 比较运算符 for while 函数 函数的全局变量和局部变量 {Javascript学习}

    Javascript学习 JavaScript概述 ECMAScript和JavaScript的关系 1996年11月,JavaScript的创造者--Netscape公司,决定将JavaScript ...

  6. python中的一个现象,db.commit和db.commit()

    假设有一个表,有自增字段,在开发环境中(sublime/Liclipse等)执行insert语句时,如果调用db.commit,那么数据库中不会有这条记录,但也不报错,再次插入成功时,自增自段加1. ...

  7. HTML5:基本语句

    HTML5:超文本标记语言(HTML)第五次重大修改 我用的sulime编辑器,用Package contorl后安装emmet插件来编辑HTML代码,以下为sublime操作小技巧 ctrl+s 保 ...

  8. Linux之sed、awk

    Linux 之AWK 命令 简介 awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在对数据分析并生成报告时,显得尤为强大. 简单来说awk就是把文件逐行的读入,以空格默认分隔 ...

  9. bc https://en.wikipedia.org/wiki/Gossip_protocol

    分布式容错性:分布式网络极其鲁棒,能够容忍部分节点的异常状态: 不可篡改性:一致提交后的数据会一直存在,不可被销毁或修改: 隐私保护性:密码学保证了数据隐私,即便数据泄露,也无法解析. 随之带来的业务 ...

  10. Xib给特定view添加手势

    步骤1.拖拽手势注意:拖拽到First Responder下方,成功后会出现一个分类Objects(如图 拖拽成功后会多出一个分类Objects ) 步骤2.给需要的view绑定手势控件 拖拽gest ...