题目

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. opesntack 底层共享存储 迁移配置

    底层共享存储在迁移配置: 每台compute 节点都需要配置一下 让nova用户可以登陆 usermod -s /bin/bash nova 设置nova 用户密码 echo "nova&q ...

  2. android 设置为系统应用

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 将一个应用apk放到手机的 /系统/应用  这个目录下, 就会是 系统应用.

  3. NOIP练习赛题目6

    长途旅行 难度级别:A: 运行时间限制:3000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 JY 是一个爱旅游的探险家,也是一名强迫症患者.现在JY 想要在C ...

  4. Codeforces Round #371 (Div. 2) A. Meeting of Old Friends 水题

    A. Meeting of Old Friends 题目连接: http://codeforces.com/contest/714/problem/A Description Today an out ...

  5. oracle切割以,隔开的数字字符串

    提前声明strsplit_typeCREATE OR REPLACE TYPE strsplit_type as table of varchar2(4000); 如果不,会报错:PLS-00201: ...

  6. 使用Puppeteer进行数据抓取(四)——快速调试

    在我们使用chrome作为爬虫获取网页数据时,往往需如下几步. 打开chrome 导航至目标页面 等待目标页面加载完成 解析目标页面数据 保存目标页面数据 关闭chrome 我们实际的编码往往集中在第 ...

  7. HappyJTAG2 - JTAG AND SPI AVR8 interface EMBEDDED JTAG ! EMBEDDED SPI !

    New version released ! V2.45 (Check version list for details) This construction is based on HappyJTA ...

  8. C# 对WinForm应用程序的App.config的使用及加密

    原文地址:http://blog.163.com/zhou_zzq/blog/static/1019622120137621739874/   我们在写C#应用程序时,在工程文件中放置一个app.co ...

  9. [置顶] 从零开始学C++之STL(二):实现简单容器模板类Vec(vector capacity 增长问题、allocator 内存分配器)

    首先,vector 在VC 2008 中的实现比较复杂,虽然vector 的声明跟VC6.0 是一致的,如下:  C++ Code  1 2   template <  class _Ty,  ...

  10. 该对象尚未初始化。请确保在所有其他初始化代码后面的应用程序启动代码中调用 HttpConfiguration.EnsureInitialized()。

    WebAPI使用属性路由,配置config.MapHttpAttributeRoutes();后出现错误: System.InvalidOperationException: 该对象尚未初始化.请确保 ...