题目

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, a1a2 ≤ … ≤ 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的更多相关文章

  1. Combination Sum II [LeetCode]

    Problem description: http://oj.leetcode.com/problems/combination-sum-ii/ Basic idea: use recursive a ...

  2. Combination Sum II —— LeetCode

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  3. 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 ...

  4. 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) ...

  5. [Leetcode][Python]40: Combination Sum II

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 40: Combination Sum IIhttps://oj.leetco ...

  6. [array] leetcode - 40. Combination Sum II - Medium

    leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...

  7. LeetCode: Combination Sum II 解题报告

    Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...

  8. Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II)

    Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II) 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使 ...

  9. 【leetcode】Combination Sum II

    Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...

随机推荐

  1. 1722 最优乘车 1997年NOI全国竞赛

    题目描述 Description H城是一个旅游胜地,每年都有成千上万的人前来观光.为方便游客,巴士公司在各个旅游景点及宾馆,饭店等地都设置了巴士站并开通了一些单程巴上线路.每条单程巴士线路从某个巴士 ...

  2. ZJOI2019day1退役记

    ZJOI2019day1退役记 每天都在划水,考场上心态炸了,也没什么好说的. 有人催我更退役记,等成绩出来了再更更吧,成绩出来也没心情更了,落差好大,还打不过文化课选手 虽然被卡常数卡到心态爆炸,但 ...

  3. JZYZOJ 2042 多项式逆元 NTT 多项式

    http://172.20.6.3/Problem_Show.asp?id=2042 题意:求一个次数界为n的多项式在模P并模x^m的意义下的逆元.P=7*17*2^23+1. 多项式逆元的含义以及求 ...

  4. HDU.5730.Shell Necklace(分治FFT)

    题目链接 \(Description\) 有\(n\)个长度分别为\(1,2,\ldots,n\)的珠子串,每个有\(a_i\)种,每种个数不限.求有多少种方法组成长度为\(n\)的串.答案对\(31 ...

  5. java基础学习总结——super关键字

    一.super关键字

  6. vue项目开发之v-for列表渲染的坑

    不知道大家在用vue开发的过程中有没有遇到过在使用v-for的时候会出现大片的黄色警告,比如下图: 其实这是因为没有写key的原因 :key是为vue的响应式渲染提供方法,在列表中单条数据改变的情况下 ...

  7. Android的GridView控件点击图片变暗效果

    @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setC ...

  8. LPC43xx SGPIO I2C Implementation

    I²C SGPIO Configuration SGPIO is a hardware feature of LPC4300 series. There are 16 SGPIO pins calle ...

  9. Git 忽略某个目录中的文件,同时保留这个目录

    类似的一个问题是项目根目录下可能有 logs 一类的目录, 我们希望他人把仓库 clone 下来的时候能够已经携带了这个目录, 但又不希望让这个目录中的日志文件进版本库. 之前看到一些项目用了一种比较 ...

  10. C#远程调用技术WebService葵花宝典

    一.课程介绍 直接开门见山吧,在学习之前阿笨想问大家一句,关于WebService远程过程调用技术(RPC) 你真的会了吗?不要跟老夫扯什么WebService技术已经过时,如果你的内心有在偷偷告诉你 ...