[LeetCode]题解(python):040-Combination Sum II
题目来源
https://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.
题意分析
Input: a list as candidates, a value named target
Output:the list number that sumed to target
Conditions:在list里面找若干个数,使得和为target,注意每个数可以取一次
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]
题目思路
与上题类似,先对list进行排序,然后穷举即可,注意可能会出现重复情况,所以需要去重(加一个判断语句),另外注意传递时的参数的范围
AC代码(Python)
1 _author_ = "YE"
2 # -*- coding:utf-8 -*-
3
4 class Solution(object):
5 def find(self,candidates, target, start, valueList):
6 if target == 0:
7 if valueList not in Solution.ans:
8 Solution.ans.append(valueList)
9 length = len(candidates)
10 for i in range(start, length):
11 if candidates[i] > target:
12 return
13 self.find(candidates, target - candidates[i], i + 1, valueList + [candidates[i]])
14
15 def combinationSum2(self, candidates, target):
16 """
17 :type candidates: List[int]
18 :type target: int
19 :rtype: List[List[int]]
20 """
21 candidates.sort()
22 Solution.ans = []
23 self.find(candidates, target, 0, [])
24 return Solution.ans
25
26 candidates = [10,1,2,7,6,1,5]
27 target = 8
28 s = Solution()
29 print(s.combinationSum2(candidates,target))
[LeetCode]题解(python):040-Combination Sum II的更多相关文章
- [Leetcode][Python]40: Combination Sum II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 40: Combination Sum IIhttps://oj.leetco ...
- LeetCode 040 Combination Sum II
题目要求:Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find al ...
- LeetCode(40) Combination Sum II
题目 Given a collection of candidate numbers (C) and a target number (T), find all unique combinations ...
- 【LeetCode】040. Combination Sum II
题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...
- Java for LeetCode 040 Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- leetcode第39题--Combination Sum II
题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...
- 040 Combination Sum II 组合总和 II
给定候选号码数组 (C) 和目标总和数 (T),找出 C 中候选号码总和为 T 的所有唯一组合.C 中的每个数字只能在组合中使用一次.注意: 所有数字(包括目标)都是正整数. 解决方案集不 ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- LeetCode解题报告—— Combination Sum & Combination Sum II & Multiply Strings
1. Combination Sum Given a set of candidate numbers (C) (without duplicates) and a target number (T) ...
- Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II)
Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II) 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使 ...
随机推荐
- linux eclipse cdt make error 127
不知道为啥,copy原来的eclipse环境到新的地方后,编译总是出错: make:*** error 127 解决方案是:属性Properties---C++编译 c++build---build ...
- CentOS6.4 配置LVS(DR模式)
DR模式中LVS主机与实际服务器都有一块网卡连在同一物理网段上. IP分配 VIP:10.10.3.170 RIP1:10.10.3.140 RIP2:10.10.3.141 1.安装所需的依赖包 y ...
- 转:深入理解JavaScript闭包概念
闭包向来给包括JavaScript程序员在内的程序员以神秘,高深的感觉,事实上,闭包的概念在函数式编程语言中算不上是难以理解的知识.如果对作用域,函数为独立的对象这样的基本概念理解较好的话,理解闭包的 ...
- Cobar_基于MySQL的分布式数据库服务中间件
Cobar是阿里巴巴研发的关系型数据的分布式处理系统,是提供关系型数据库(MySQL)分布式服务的中间件,该产品成功替代了原先基于Oracle的数据存储方案,它可以让传统的数据库得到良好的线性扩展,并 ...
- CentOS 安装Gitolite 服务器
[jackluo@localhost .ssh]$ sudo groupadd git #创建 用户组 [jackluo@localhost .ssh]$ sudo adduser --system ...
- Shell 函数 function [转]
本文也即<Learning the bash Shell>3rd Edition的第四章Basic Shell Programming之读书笔记,但我们将不限于此. 运行shell脚本程序 ...
- MatLab 组件大全
MATLAB 矩阵实验室 7.0.1 Simulink ...
- backtrack5渗透 笔记
目录 1.信息收集 2.扫描工具 3.漏洞发现 4.社会工程学工具 5.运用层攻击msf 6.局域网攻击 ...
- discuz怎么根据连接知道调用的是什么模板页面
其实不怎么难,基本都可以看出discuz是怎么样调用模板页面的 这个是论坛的帖子的列表页,看到url就可以看出是forum目录下的forumdisplay这个模板,forumdisplay.html这 ...
- python实现查找指定文件
若不包含子目录的遍历: import glob for filename in glob.glob("/data/testdata/*.jpg"): print filename ...