Combination Sum II leetcode java
题目:
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]
题解:
这道题跟combination sum本质的差别就是当前已经遍历过的元素只能出现一次。
所以需要给每个candidate一个visited域,来标识是否已经visited了。
当回退的时候,记得要把visited一起也回退了。
代码如下:
1 public static ArrayList<ArrayList<Integer>> combinationSum(int[] candidates, int target) {
2 ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
3 ArrayList<Integer> item = new ArrayList<Integer>();
4 if(candidates == null || candidates.length==0)
5 return res;
6
7 Arrays.sort(candidates);
8 boolean [] visited = new boolean[candidates.length];
9 helper(candidates,target, 0, item ,res, visited);
return res;
}
private static void helper(int[] candidates, int target, int start, ArrayList<Integer> item,
ArrayList<ArrayList<Integer>> res, boolean[] visited){
if(target<0)
return;
if(target==0){
res.add(new ArrayList<Integer>(item));
return;
}
for(int i=start;i<candidates.length;i++){
if(!visited[i]){
if(i>0 && candidates[i] == candidates[i-1] && visited[i-1]==false)//deal with dupicate
continue;
item.add(candidates[i]);
visited[i]=true;
int newtarget = target - candidates[i];
helper(candidates,newtarget,i+1,item,res,visited);
visited[i]=false;
item.remove(item.size()-1);
}
}
}
Combination Sum II leetcode java的更多相关文章
- Combination Sum II [LeetCode]
Problem description: http://oj.leetcode.com/problems/combination-sum-ii/ Basic idea: use recursive a ...
- Combination Sum II —— LeetCode
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- Path Sum II leetcode java
题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the give ...
- 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][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 ...
- LeetCode: Combination Sum II 解题报告
Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...
- Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II)
Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II) 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使 ...
- 【leetcode】Combination Sum II
Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...
随机推荐
- [CEOI2017]Palindromic Partitions
[CEOI2017]Palindromic Partitions 题目大意: 给出一个长度为\(n(n\le10^6)\)的只包含小写字母字符串,要求你将它划分成尽可能多的小块,使得这些小块构成回文串 ...
- C/C++ 和 PHP 技术经典图书,学习视频资料总结
技术经典图书 1.<计算机科学导论> 作者:(美)佛罗赞,(美)莫沙拉夫著,刘艺等译(强推) 涵盖了大部分计算机课程的内容,但都是简介,是最基础的知识,非常适合计算机初学者看,强烈建议把课 ...
- ELASTIC制图等高级使用
基于上一个安装部署的文档后(ELASTIC 5.2部署并收集nginx日志) http://www.cnblogs.com/kerwinC/p/6387073.html 本次带来一些使用的分享. ki ...
- pop3_用Java发送图文并茂的HTML邮件
package com.syj; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.i ...
- vbs学习笔记1——判断文件和文件夹是否存在
首先分享一个“VBS脚本常用经典代码收集”,这里面关于vbs很丰富的内容. 所有vbs脚本都需要保存为.vbs形式才可以运行 FileSystemObject Object的所有方法参考:http:/ ...
- Linux虚拟主机管理系统---wdcp
关于WDCP这款虚拟主机管理系统,是疯子使用的第二款Linux虚拟主机管理系统,使用是挺简单的,以前好像是因为编码问题而放弃这款面板. WDCP功能比较完善,基本上需要的功能都能满足,例如:在线下载. ...
- 解决Python交叉编译后,键盘方向键乱码的问题
参考 http://www.alliedjeep.com/38071.htm https://www.zhihu.com/question/21518507 http://professor.blog ...
- AngularJS中实现显示或隐藏动画效果的3种方式
本篇体验在AngularJS中实现在"显示/隐藏"这2种状态切换间添加动画效果. 通过CSS方式实现显示/隐藏动画效果 思路: →npm install angular-anima ...
- lufylegend:图片的加载和显示
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8" /> <script ...
- Arcgis Pro为什么我已经安装了汉化包但是显示的还是英文?