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 ...
随机推荐
- linux安装project lemon测评机
(写下备用) 机子:xubuntu 16.04 LTS 1.下载lemon github地址:https://github.com/Sojiv/Project_lemon 这里download zip ...
- VMware 使用本机代理上网
灰机使用方法 VMware 安装方法 首先解决主机的配置 1.查询本机 IP 地址,使用 ipconfig /all 2.更改小灰机的设置 3.虚拟机设置 4.Ubuntu 设置
- Booting dircetly into Redlink FW from flash
Booting dircetly into Redlink FW from flash Hello, the usual way to use the Redlink FW is a two-step ...
- 反接保护电路 Reverse Voltage Protection
Reverse Voltage Protection I've long wanted to pull together some reverse polarity protection ideas ...
- HTTP协议GET和POST的区别
from http://blog.csdn.net/whuslei/article/details/6667095 权威点的说明请参考:http://www.cs.tut.fi/~jkorpela/f ...
- CRM上线之路 走上了CRM实施顾问-第12天上班 -第三周
今天是周五,<CRM初期需求说明>是经理们商讨的,总共2张纸,根据两次会议,我写了<CRM需求说明>总共18面. 这是这周的工作汇报,其实,报告是我一天内写出来的,中午饭都没吃 ...
- ubuntu的配置文件
ubuntu的配置文件 是 ~/.gconf 我是把终端弄挂了, 只能再桌面系统下找到 ~/.gconf 下的相应文件 修改后就恢复到原来状态.
- STM32F103 TIM3定时器初始化程序
//TIM3 分频 #define TIM3_DIV1 (1-1) #define TIM3_DIV18 (18-1) #define TIM3_DIV72 (72-1) //************ ...
- 电子书下载:Delphi XE 5 移动开发入门手册(完整版)
更多电子书请到: http://maxwoods.400gb.com 下载:Delphi XE5移动开发入门手册(完整版)
- 教你调用数据库读取短信 记事本 通讯录文件,让ios5的短信恢复到ios4
由于高版本的ios固件向下恢复到低版固件时无法通过itunes恢复备份,所以一些数据,比如SMS需要通过提取文件的方式单独进行备份恢复特别是ios5的短信,之前很是头痛,直接将文件恢复到指定目录修改权 ...