[Leetcode][Python]40: Combination Sum II
# -*- coding: utf8 -*-
'''
__author__ = 'dabay.wang@gmail.com' 40: Combination Sum II
https://oj.leetcode.com/problems/combination-sum-ii/ Given a collection of candidate numbers (C) and a target number (T),
find all unique combinations in C where the candidate numbers sums to T.
Each number in C may only be used once in the combination. 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 10,1,2,7,6,1,5 and target 8,
A solution set is:
[1, 7]
[1, 2, 5]
[2, 6]
[1, 1, 6] ===Comments by Dabay===
递归。
先把candidates排序。
遍历candidates,用index来记录目前的指针。
注意去掉结果集中可能的重复。
''' class Solution:
# @param candidates, a list of integers
# @param target, integer
# @return a list of lists of integers
def combinationSum2(self, candidates, target):
def combinationSum(candidates, target, index, res, res_list):
if target == 0:
if res not in res_list:
res_list.append(list(res))
return
while index < len(candidates) and target >= candidates[index]:
res.append(candidates[index])
combinationSum(candidates, target-candidates[index], index+1, res, res_list)
res.pop()
index += 1 res_list = []
candidates.sort()
combinationSum(candidates, target, 0, [], res_list)
return res_list def main():
sol = Solution()
candidates = [10,1,2,7,6,1,5]
target = 8
print sol.combinationSum2(candidates, target) if __name__ == "__main__":
import time
start = time.clock()
main()
print "%s sec" % (time.clock() - start)
[Leetcode][Python]40: Combination Sum II的更多相关文章
- 【一天一道LeetCode】#40. Combination Sum II
一天一道LeetCode系列 (一)题目 Given a collection of candidate numbers (C) and a target number (T), find all u ...
- 【LeetCode】40. Combination Sum II (2 solutions)
Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...
- 【LeetCode】40. Combination Sum II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:回溯法 日期 题目地址:ht ...
- LeetCode:40. Combination Sum II(Medium)
1. 原题链接 https://leetcode.com/problems/combination-sum-ii/description/ 2. 题目要求 给定一个整型数组candidates[ ]和 ...
- LeetCode OJ 40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III
39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...
- [LeetCode] 40. Combination Sum II 组合之和之二
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- [LeetCode] 40. Combination Sum II 组合之和 II
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
随机推荐
- [Linux]shell编程基础/linux基础入门
声明执行程序 #!/bin/bash 用来告诉系统使用/bin/bash 程序来执行该脚本.譬如python 脚本,可以这样写: #!/usr/bin/python 赋值和引用 赋值公式: 变量名 ...
- C#基础:委托 【转】
委托是C#中最为常见的内容.与类.枚举.结构.接口一样,委托也是一种类型.类是对象的抽象,而委托则可以看成是函数的抽象.一个委托代表了具有相同参数列表和返回值的所有函数.比如: delegate in ...
- poj 2516 (费用流)
题意:有N个供应商,M个店主,K种物品.每个供应商对每种物品的的供应量已知,每个店主对每种物品的需求量的已知,从不同的供应商运送不同的货物到不同的店主手上需要不同的花费,又已知从供应商m送第k种货物的 ...
- swift Dictionary 字典
// // main.swift // 字典 // // Created by zhangbiao on 14-6-15. // Copyright (c) 2014年 理想. All rig ...
- Swift自定义Class实现Hashable
假如有个Bit类,其中含有CGPoint类型的point属性,Class定义如下 class Bit { var point : CGPoint init(point : CGPoint) { sel ...
- mysql插入大量数据
创建实验表: CREATE TABLE `a` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` char(50) NOT NULL, `type` ch ...
- VIM 中 查看{}是否闭合,按%跳转到下个闭合
VIM 中 查看{}是否闭合,按%跳转到下个闭合
- T-SQL存储过程
存储过程(procedure)就是一个函数,完成一段sql代码的封装,实现代码的重用. 优点: 1.比使用DotNet直接写Sql脚本执行少了一块解析编译的过程.效率更快一点点. ...
- js判断手机的类型
var ua = navigator.userAgent;if(ua.match(/iPhone|iPod/i) != null){console.log("iphone代码"); ...
- MD5校验
好久没有写随笔了,正好这两天可以休整一下,借此机会总结下最近使用python的小体会. 个人体会文件校验在下载文件时使用较多,在linux下最简单的实现方式就是: 1 $ md5sum filenam ...