Java [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.
- 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]
解题思路:
跟前一个类似,不过每次选好当前元素后,就直接选下一个位置的元素了。并且在每次选元素之前,保证这次的元素与上次的元素不重合。
代码如下:
public class Solution {
public List<List<Integer>> combinationSum2(int[] candidates,
int target) {
Arrays.sort(candidates);
List<List<Integer>> result = new ArrayList<List<Integer>>();
getResult(result, new ArrayList<Integer>(), candidates, target, 0);
return result;
} public void getResult(List<List<Integer>> result,
List<Integer> current, int[] candiates, int target, int start) {
if (target > 0) {
for (int i = start; i < candiates.length && target >= candiates[i]; i++) {
if (i > start && candiates[i] == candiates[i - 1])
continue;
current.add(candiates[i]);
getResult(result, current, candiates, target - candiates[i],
i + 1);
current.remove(current.size() - 1);
}
} else if (target == 0) {
result.add(new ArrayList<Integer>(current));
}
}
}
Java [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 组合之和 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 (candidates) and a target number (target), find all unique c ...
- 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 (组合的和之二)
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 ...
- LeetCode 40 Combination Sum II(数组中求和等于target的所有组合)
题目链接:https://leetcode.com/problems/combination-sum-ii/?tab=Description 给定数组,数组中的元素均为正数,target也是正数. ...
- [LeetCode] 40. Combination Sum II ☆☆☆(数组相加等于指定的数)
https://leetcode.wang/leetCode-40-Combination-Sum-II.html 描述 Given a collection of candidate numbers ...
随机推荐
- [翻译]15个最常见的WCF面试问题
WCF和ASMX WebService的区别是什么? 最基本的区别在于,ASMX或者ASP.NET WebService是用来通过基于HTTP的SOAP来实现通讯.但WCF可以使用任意协议(HTTP, ...
- Fedora 命令
1. 更新包 yum clear all yum -y update 2.yum包查找 yum whatprovides xxxx.os.l 3 df 查看磁盘空间 xclip 复制到粘贴板 xcli ...
- proguardgui.bat来混淆已有的jar包
1.U:\android-sdk\tools\proguard\bin\找到 proguardgui.bat,双击就可以弹出一个混淆的界面 2.加入不要混淆的,比如我们用的系统的,还有别人的jar 3 ...
- 数据库 mysql 优化器原理
MySQL查询优化器有几个目标,但是其中最主要的目标是尽可能地使用索引,并且使用最严格的索引来消除尽可能多的数据行. 你的最终目标是提交SELECT语句查找数据行,而不是排除数据行.优化器试图排除数据 ...
- Google code: Why ‘Everything up-to-date’ when pushing (git)
原文链接:http://blog.rexzhao.com/2011/11/28/google-code-git-everything-up-to-date-when-push.html 第一次在Goo ...
- EXTJS store 某行某列数据更新等操作
1.可以使用add(Ext.data.Record[] records)或者add(Ext.data.Record record)向store末尾添加一个或多个record.如: var newRec ...
- 【扩展欧几里得】Bzoj 1477:青蛙的约会
Description 两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出发之前忘记了一件很重要的事 ...
- Java Code Examples for org.springframework.http.HttpStatus
http://www.programcreek.com/java-api-examples/index.php?api=org.springframework.http.HttpStatus
- NYOJ 题目15 括号匹配(二)(区间DP)
点我看题目 题意 : 中文题不详述. 思路 : 本来以为只是个小模拟,没想到是个区间DP,还是对DP不了解. DP[i][j]代表着从字符串 i 位置到 j 位置需要的最小括号匹配. 所以初始化的DP ...
- POJ 1797 Heavy Transportation(Dijkstra)
http://poj.org/problem?id=1797 题意 :给出N个城市M条边,每条边都有容量值,求一条运输路线使城市1到N的运输量最大. 思路 :用dijkstra对松弛条件进行变形.解释 ...