leetcode — combination-sum-ii
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
*Source : https://oj.leetcode.com/problems/combination-sum-ii/
*
* Created by lverpeng on 2017/7/14.
*
* 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 CombinationSum2 {
private List<List<Integer>> result = new ArrayList<List<Integer>>();
/**
* 先考虑一个数字,比如第一位,其他位以此类推,第一位为例
*
* @param num
* @param start
* @param end
* @param target
* @param list
*/
public void combinationSum(int[] num, int start, int end, int target, List<Integer> list) {
if (target == 0 && list.size() > 0) {
Integer[] newList = new Integer[list.size()];
System.arraycopy(list.toArray(new Integer[list.size()]), 0, newList, 0, list.size());
result.add(Arrays.asList(newList));
return;
}
int index = start;
int multiple = 0;
while (index <= end && num[index] <= target) {
if (index > start && num[index] == num[index-1]) {
index ++;
continue;
}
int newTarget = target - num[index];
list.add(num[index]);
combinationSum(num, index + 1, end, newTarget, list);
list.remove(list.size() - 1);
index++;
}
}
public List<List<Integer>> combination(int[] num, int target) {
Arrays.sort(num);
List<Integer> combinationList = new ArrayList<Integer>();
combinationSum(num, 0, num.length - 1, target, combinationList);
return result;
}
private static void printMatrix(List<List<Integer>> list) {
StringBuilder stringBuilder = new StringBuilder();
for (List<Integer> intList : list) {
for (Integer num : intList) {
stringBuilder.append(num);
stringBuilder.append(", ");
}
stringBuilder.delete(stringBuilder.length() - 2, stringBuilder.length() - 1);
stringBuilder.append("\n");
}
System.out.println(stringBuilder);
}
public static void main(String[] args) {
CombinationSum2 combinationSum = new CombinationSum2();
int[] num = new int[]{10,1,2,7,6,1,5};
printMatrix(combinationSum.combination(num, 8));
}
}
leetcode — combination-sum-ii的更多相关文章
- LeetCode: Combination Sum II 解题报告
Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...
- [LeetCode] Combination Sum II 组合之和之二
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- Leetcode Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- [LeetCode] Combination Sum II (递归)
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- LeetCode——Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- LeetCode Combination Sum II (DFS)
题意: 在集合candidates中选出任意多个元素,使得他们的和为target,返回所有的组合,以升序排列. 思路: 难点在于如何去重,比如集合{1,1,2},target=3,那么只有一个组合就是 ...
- leetcode Combination Sum II python
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- [Leetcode] combination sum ii 组合之和
Given a collection of candidate numbers ( C ) and a target number ( T), find all unique combinations ...
- [Leetcode][Python]40: Combination Sum II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 40: Combination Sum IIhttps://oj.leetco ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
随机推荐
- jquery学习总结1-12
一.jquery入口函数的几种写法 1.$(document).ready(function() { } ) ; 2.jquery(document).ready(function() { } ) ...
- JAVA 8 主要新特性 ----------------(七)新时间日期 API -----LocalDate
一.改版原因 1.老板的Date和Calander存在问题,日期操作名称混乱,有的在text下,有的在util下,包名混乱 2.Simple包混乱,致命错误线程不安全. ...
- 爬虫学习笔记-urllib库
urllib库是python中一个最基本的网络请求库.可以模拟浏览器的行为,向指定的服务器发送一个请求,并可以保存服务器返回的数据. urlopen函数:在python3的urllib库中,所有和网络 ...
- 缓存为王-varnish
2.varnish的软件清单 [root@centos69 ~]# rpm -ql varnish /etc/logrotate.d/varnish /etc/rc.d/init.d/varnish ...
- ora 01795 in 1000 limit
https://docs.oracle.com/cd/B19306_01/server.102/b14200/conditions013.htm https://docs.oracle.com/cd/ ...
- 【洛谷P3960】列队题解
[洛谷P3960]列队题解 题目链接 题意: Sylvia 是一个热爱学习的女孩子. 前段时间,Sylvia 参加了学校的军训.众所周知,军训的时候需要站方阵. Sylvia 所在的方阵中有 n×m ...
- 前端基础之 html
---恢复内容开始--- web服务本质 import socket sock=socket.socket() sock.bind(()) sock.listen() where True: conn ...
- C语言常识
struct tree{ struct tree *left; struct tree *right; }; typedef struct tree newtree; int main(int arg ...
- 安装Pangolin
Pangolin是一个用于OpenGL显示/交互以及视频输出的一个轻量级 快速开发库 一:安装必要的库 1.Glew sudo apt-get install libglew-dev 2.Cmake ...
- 网络编程——UDP编程
一个简单的聊天代码:运行结果: 在这个程序之中,由于recvfrom函数拥塞函数,没有数据时会一直阻塞,所以客户端和服务器端只能通过一回一答的方式进行信息传递.严格的讲UDP没有明确的客户端和服务端, ...