public class Solution {
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<List<Integer>> res=new ArrayList<List<Integer>>();
Arrays.sort(candidates);
work(target,0,candidates,res,new ArrayList<Integer>());
return res;
}
//胜读遍历,找出合格的数据
/*
* target:代表目标数据
* index:代表循环的其实位置,也就是查找合格数据的额起始位置
* candidates 候选值数组
* res:返回值
* arrayList:保存一组合格数据的变量
* */
public void work(int target, int index, int[] candidates, List<List<Integer>> res, ArrayList<Integer> arrayList){
//for循环,每次从index出发,因为数组已经排序,所以不会出现重复的数据
//终止条件为索引越界&&目标值要大于等于当前要检查的候选值
for(int i=index;i<candidates.length&&candidates[i]<=target;i++){
/*
* 如果target大于当前从candidate中提取的值时,则可以将其加入到arrayList中,在进入深度的遍历查找合格数据
* 注意的是,当无论是查找成功还是失败的时候,都要将arrayList的最后一个数据弹出,一遍进行下一次的深度遍历
* */
if(candidates[i]<target){
arrayList.add(candidates[i]);
work(target-candidates[i], i+1, candidates, res, arrayList);
arrayList.remove(arrayList.size()-1);
}
/*
* 如果target==当前提取的candidate中的值,则表明查找成功,将这一数组添加到res横中
* 并且弹出弹出arrayList中的最后一个数据进行下一次的遍历
* */
else if(candidates[i]==target){
arrayList.add(candidates[i]);
if(!check(arrayList,res))
res.add(new ArrayList<Integer>(arrayList));
arrayList.remove(arrayList.size()-1);
}
}
}
public boolean check(ArrayList<Integer> arrayList, List<List<Integer>> res){
ArrayList<Integer> temp;
int count=0;
for(int i=0;i<res.size();i++){
temp=(ArrayList<Integer>) res.get(i);
count=0;
if(temp.size()==arrayList.size()){
for(int j=0;j<temp.size();j++){
if(temp.get(j)==arrayList.get(j))count++;
}
if(count==arrayList.size())return true;
}
}
return false;
}
}

Combination Sum II的更多相关文章

  1. 【leetcode】Combination Sum II

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

  2. Combination Sum,Combination Sum II,Combination Sum III

    39. Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique co ...

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

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

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

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

  5. leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III

    39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...

  6. 【LeetCode】40. Combination Sum II (2 solutions)

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

  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解题报告—— Combination Sum & Combination Sum II & Multiply Strings

    1. Combination Sum Given a set of candidate numbers (C) (without duplicates) and a target number (T) ...

  9. leetcode-combination sum and combination sum II

    Combination sum: Given a set of candidate numbers (C) and a target number (T), find all unique combi ...

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

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

随机推荐

  1. JS 数字,金额 用逗号 隔开(数字格式化)

    <script> function fmoney(s,n) { n = n > 0 && n <= 20 ? n : 2; s = parseFloat((s ...

  2. 对于.NET Socket连接的细节记录

    如果客户端直接连接一个不存在的服务器端,客户端会抛出异常: 如果在连接过程中,客户端强制关闭了连接(没有调用Close直接关闭了程序),服务器端会抛出异常: 如果在连接过程中,客户端调用了Close, ...

  3. 作业七:团队项目——Alpha版本冲刺阶段003

    今日进展:我们的目标是做一款扫雷游戏,所以我们先去玩了几款游戏,找到了扫雷游戏的一些特点. 今日安排:先进行了一些必要的游戏过程,进行了基本的扫雷界面规划.

  4. 开发板远程操作SQL SERVER解决方案

    环境: 开发板:freescale 2.6 armv71,系统只读,唯一可以读写的路径是/tmp/sd(这是一个sd卡).程序放在/tmp/sd/transfer下(下文以运行路径代替),sql语句以 ...

  5. Djanog结合jquery实现ajax

    最近想在使用django的基础上通过jquery实现页面局部刷新的功能,研究了两天,终于是解决了这个问题,下面把方法步骤记录下来,以备以后重用. 在项目中通过两种形式实现了ajax: 第一种方法:we ...

  6. QImage::drawRect 和 fillRect在处理大面积区域时代价高昂

    项目需要生成一张掩码图, 出于操作pixel方便的考虑采用QImage(mono), 但在实现一个类似于 cvZero的操作时发现在图片面积较大时效率很低, 提醒一下 ps: 后来是改变策略, 用偏移 ...

  7. JS生成二维码,允许中文转码

    一.使用jquery-qrcode生成二维码 先简单说一下jquery-qrcode,这个开源的三方库(可以从https://github.com/jeromeetienne/jquery-qrcod ...

  8. 图片压缩工具optipng/jpegoptim安装

    [1]还未实践 #yum install optipng -y [2]已成功 #yum install -y libjpeg libjpeg-devel #wget http://freecode.c ...

  9. Linux Shell shortcut

    Ctrl+a跳到第一个字符前Ctrl+x同上但再按一次会从新回到原位置 Details see below: Linux shell shortcut

  10. 远程联机linux主机

    远程联机linux主机 推荐使用 ssh  如 ssh user@www.abc.com(ssh使用公钥+私钥非对称加密,数据传输安全,不要使用telnet) 传输文件:sftp 或者 scp 若想使 ...