【LeetCode题意分析&解答】39. Combination Sum
Given a set of candidate numbers (C) 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 (including target) will be positive integers.
- Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
- The solution set must not contain duplicate combinations.
For example, given candidate set 2,3,6,7
and target 7
,
A solution set is: [7]
[2, 2, 3]
题意分析:
本题是给定一个数字集合(可能有重复)和一个目标值,让你找出所有可能的组合,使之和为目标值。所有的数字可以使用无限次,要求得到的组合不能有重复,并且组合里面的数字必须是升序。
解答:
思路是循环拿每个数字去迭代,找出所有的组合,满足条件返回,不满足退回上一步继续下一个数字的尝试。看特征,仍然是用回溯法的思路去做。
AC代码:
class Solution(object):
def combinationSum(self, candidates, target):
ret_list = []
def backtracing(target, candidates, index, temp_list):
if target == 0:
ret_list.append(temp_list)
elif target > 0:
for i in xrange(index, len(candidates)):
backtracing(target - candidates[i], candidates, i, temp_list + [candidates[i]]) backtracing(target, sorted(list(set(candidates))), 0, [])
return ret_list
【LeetCode题意分析&解答】39. Combination Sum的更多相关文章
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【LeetCode题意分析&解答】35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- 【LeetCode题意分析&解答】38. Count and Say
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...
- 【LeetCode题意分析&解答】43. Multiply Strings
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- 【LeetCode题意分析&解答】42. Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- 【LeetCode题意分析&解答】41. First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- 【LeetCode题意分析&解答】36. Valid Sudoku
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...
- 【LeetCode题意分析&解答】34. Search for a Range
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
随机推荐
- c++中自增(++)和自减(--)操作符
自增(++)和自减(--)操作符为对象加1 或减1 操作提供了方便简短的实现方式.它们有前置和后置两种使用形式.到目前为止,我们已经使用过前自增操作,该操作使其操作数加1,操作结果是修改后的值.同理, ...
- How to close existing connections to a DB
use master ALTER DATABASE YourDatabase SET SINGLE_USER WITH ROLLBACK IMMEDIATE --do you stuff here A ...
- 在什么情况下使用exist和in
http://www.itpub.net/thread-406784-4-1.htmlYou Asked (Jump to Tom's latest followup) Tom: can you gi ...
- 引用jquery框架后出错
问题描述:当引用了jquery框架后,页面的js不能正常工作. 后面我的解决办法:是因为在引用 jquery的框架时的代码为 <script type="text/javascript ...
- Programming C#.Interfaces
类实现一个接口时,它必须实现该接口的所有部分(方法和属性等),效果相当于类要表明:“我同意履行这个接口所定义的协定.” 从抽象类继承实现了“is-a(是一种)”关系,实现接口时一种“implement ...
- ecshop开发日志之虚拟商品发送邮件通知
购买虚拟商品,系统会在支付后自动发送邮件到用户填写的邮件地址中,追踪过程如下首先在订单列表中可以获得到处理订单的php文件为flow.php,之后在最后一步url地址显示为http://localho ...
- js 中 setTimeout()的用法
setTimeout()在js类中的使用方法 setTimeout (表达式,延时时间)setTimeout(表达式,交互时间)延时时间/交互时间是以豪秒为单位的(1000ms=1s) setTi ...
- 项目中引用ThinkPHP框架
ThinkPHP是一个宽度.兼容且简单的国产的轻量级框架,具有优良的性能,并且非常注重易用性. 那么,我们该如何将ThinkPHP引入自己的项目中,使得自己的项目可以使用这款优良的框架呢? 首先介绍下 ...
- 低功耗之战!ANT VS Bluetooth LE
利用近距离无线通信技术将手机及可穿戴式传感器终端等与智能电话连接起来,实现新的功能.最近,以此为目标的行动正在展开.其中备受关注的近距离无线方式是“ANT”和“Bluetooth LE”.为了在各种便 ...
- JS获取年月日时分秒
var d = new Date(); ) + "-" + d.getDate() + " " + d.getHours() + ":" + ...