[leetcode]40. Combination Sum II组合之和之二
Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target.
Each number in candidates 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.
Example 1:
Input: candidates = [10,1,2,7,6,1,5], target = 8,
A solution set is:
[
[1, 7],
[1, 2, 5],
[2, 6],
[1, 1, 6]
]
Example 2:
Input: candidates = [2,5,2,1,2], target = 5,
A solution set is:
[
[1,2,2],
[5]
]
题意:
给定一个集合以及一个值target,找出所有加起来等于target的组合。(每个元素只能用一次)
Solution1: Backtracking
在[leetcode]39. Combination Sum组合之和的基础上, 加一行查重的动作。
code
class Solution {
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
Arrays.sort(candidates);
List<List<Integer>> result = new ArrayList<>();
List<Integer> path = new ArrayList<>();
helper(candidates, 0, target, path, result );
return result;
}
private void helper(int[] nums, int index, int remain, List<Integer> path, List<List<Integer>> result){
if (remain == 0){
result.add(new ArrayList<>(path));
return;
}
for(int i = index; i < nums.length; i++){
if (remain < nums[i]) return;
if(i > index && nums[i] == nums[i-1]) continue; /** skip duplicates */
path.add(nums[i]);
helper(nums, i + 1, remain - nums[i], path, result);
path.remove(path.size() - 1);
}
}
}
[leetcode]40. Combination Sum II组合之和之二的更多相关文章
- [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] Combination Sum II 组合之和之二
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- [LeetCode] 377. Combination Sum IV 组合之和 IV
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [LeetCode] 216. Combination Sum III 组合之和 III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- [LeetCode] 377. Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- LeetCode OJ:Combination Sum II (组合之和 II)
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- LeetCode 40. Combination Sum II (组合的和之二)
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
随机推荐
- PythonStudy——字符串常用操作 String common operations
# 1.字符串的索引取值: 字符串[index]# 正向取值从0编号,反向取值从-1编号 s1 = '123abc呵呵' t_s = ' # 取出c print(s1[5], s1[-3]) # 2. ...
- php 字符串固定长度,不够补充其他字符串
<?php $input = '456'; $var= str_pad($input,5,10,STR_PAD_LEFT); w3school手冊:http://www.w3school.com ...
- Http原理与实践
Http原理 一.使用Http协议最简单的例子 1.输入URL打开网页 2.AJAX获取数据 3.img标签加载图片 二.Cache-Control 1.public.private 2.must-r ...
- 关于SpringMVC
SpringMVC 原理:1.用户发送请求给服务器.url:user.do2.服务器收到请求.发现DispatchServlet可以处理.于是调用DispatchServlet.3.DispatchS ...
- 【剑指offer】反转链表
输入一个链表,反转链表后,输出新链表的表头. *与之前的问题不同,这里需要修改链表的指向(之前的问题,不需要修改结点的指针,只需使用栈保存每个结点的值) *注意非空处理以及最后一个结点指针的修改 /* ...
- CSS之metra&title&base&target
<!DOCTYPE html><html lang="en"><head> <style type="text/css" ...
- ado.net调用带参数的sql语句
- html/css/js-个人容易忘的一些属性
1.当div里面的文字超过给定div给定的宽度,div里面的文字自动换行 word-break:break-all:会截断该行最后的单词 word-wrap:break-word:不会截断,该行长度最 ...
- python网页爬虫开发之六-Selenium使用
chromedriver禁用图片,禁用js,切换UA selenium 模拟chrome浏览器,此时就是一个真实的浏览器,一个浏览器该加载的该渲染的它都加载都渲染,所以爬取网页的速度很慢.如果可以不加 ...
- is,as,类库
is和as运算符: 所有类型的基类 object类型 - 基类:所有类型的基类,就类似是整个生物圈的生物类,是个大的概念 object o1 = new Random(); //object可以承载R ...