Leetcode 40. 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.
- 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]
]
这道题和Leetcode 39差不多,区别是一个元素只能用一次,所以L27里面用了candidates[i+1:]。另外需要注意的是,[1a,2,5] 和 [1b,2,5]在结果中应被视为重复解而去掉一个。所以用了L13-L16。
class Solution(object):
def combinationSum2(self, candidates, target):
"""
:type candidates: List[int]
:type target: int
:rtype: List[List[int]]
"""
candidates.sort() res = []
self.helper(candidates, target, res, []) ans = []
for elem in res:
if elem not in ans:
ans.append(elem) return ans def helper(self, candidates, target, res, line):
if target == 0:
res.append([x for x in line]) for i,x in enumerate(candidates):
if x <= target:
line.append(x)
self.helper(candidates[i+1:], target - x, res, line)
line.pop()
Leetcode 40. Combination Sum II的更多相关文章
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- [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 ...
- [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 (组合的和之二)
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- leetcode 40 Combination Sum II --- java
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- [LeetCode] 40. Combination Sum II ☆☆☆(数组相加等于指定的数)
https://leetcode.wang/leetCode-40-Combination-Sum-II.html 描述 Given a collection of candidate numbers ...
- Java [Leetcode 40]Combination Sum II
题目描述: Given a collection of candidate numbers (C) and a target number (T), find all unique combinati ...
- LeetCode 40 Combination Sum II(数组中求和等于target的所有组合)
题目链接:https://leetcode.com/problems/combination-sum-ii/?tab=Description 给定数组,数组中的元素均为正数,target也是正数. ...
随机推荐
- Openstack api 学习文档 & restclient使用文档
Openstack api 学习文档 & restclient使用文档 转载请注明http://www.cnblogs.com/juandx/p/4943409.html 这篇文档总结一下我初 ...
- openstack-swift云存储部署(一)
最近因为工作的需要搭建了一套swift云存储架构 我们先来解读一下里面的技术知识点:swift服务是属于openstack中的一种组件服务,openstack中的组件服务还有keystone.Nova ...
- 使用 python 收集获取 Linux 系统主机信息
使用 python 代码收集主机的系统信息,主要:主机名称.IP.系统版本.服务器厂商.型号.序列号.CPU信息.内存等系统信息. #!/usr/bin/env python #encoding: u ...
- LeetCode#11. Container With Most Water
问题描述 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ...
- java设计模式之原型模式
原型模式概念 该模式的思想就是将一个对象作为原型,对其进行复制.克隆,产生一个和原对象类似的新对象.java中复制通过clone()实现的.clone中涉及深.浅复制.深.浅复制的概念如下: ⑴浅复制 ...
- UVA - 11584 Partitioning by Palindromes[序列DP]
UVA - 11584 Partitioning by Palindromes We say a sequence of char- acters is a palindrome if it is t ...
- 2.bootstrap练习笔记-轮播图
bootstrap练习笔记-轮播图 1.要使用轮播图,首先要将其放在一个主div里面 设置id为myCaroysel class为carousel slide 设置id是标识这个div是轮播图,等到l ...
- JAVA中如何使用SORT从大到小排
import java.util.Arrays;import java.util.Collections;public class Test { public static void main(Str ...
- 嵌入式Linux驱动学习之路(十九)触摸屏驱动、tslib测试
触摸屏使用流程: 1. 按下产生中断. 2.在中断处理程序中启动AD转换XY坐标. 3.AD转换结束并产生AD中断. 4. 在AD的中断处理函数中上报信息,启动定时器. 5. 定时器时间到后进入中断, ...
- 为WebService指定线程池
通过阅读WebService发布过程的源代码,可以配置自定义的线程池 package org.zln.ws.server;import com.sun.xml.internal.ws.api.Bind ...