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. Buffer、Channel示例

      a.txt 孔雀向西飞,今朝更好看.孔雀向西飞,今朝更好看.孔雀向西飞,今朝更好看.孔雀向西飞,今朝更好看. 示例一. package com.test; import java.io.FileI ...

  2. java发送http的get、post请求[转]

    原文链接:http://www.cnblogs.com/zhuawang/archive/2012/12/08/2809380.html package wzh.Http; import java.i ...

  3. [小菜随笔]python tkinter实现简单的ping命令

    本文主要是介绍python图形界面上的按键与实际功能的对接,其实编程掌握了基础之后的学习应该都是靠自己去挖掘其他的 在网上发现多半教程都是2的,故本文使用的是python3.5,其实也没什么区别,就有 ...

  4. 003-Tuple、Array、Map与文件操作入门实战

    003-Tuple.Array.Map与文件操作入门实战 Tuple 各个元素可以类型不同 注意索引的方式 下标从1开始 灵活 Array 注意for循环的until用法 数组的索引方式 上面的for ...

  5. Linux/Unix 线程同步技术之互斥量(1)

    众所周知,互斥量(mutex)是同步线程对共享资源访问的技术,用来防止下面这种情况:线程A试图访问某个共享资源时,线程B正在对其进行修改,从而造成资源状态不一致.与之相关的一个术语临界区(critic ...

  6. python selenium与自动化

    大学是学习过java,但是工作中没用,忘完了,而且哪怕以后有了机会,就是很不愿意去学这个语言,开始喜欢上了c#,但是随着学的升入,感觉.net太庞大了,要学习那么多,总感觉我学这个要做什么,感觉要做的 ...

  7. Json 讲解

    JSON详解 阅读目录 JSON的两种结构 认识JSON字符串 在JS中如何使用JSON 在.NET中如何使用JSON 总结 JSON的全称是”JavaScript Object Notation”, ...

  8. #include <vector>用法之我见

    vector是一种顺序容器,事实上和数组差不多,但它比数组更优越.一般来说数组不能动态拓展,(何为动态拓展,即是说如果你知道你要存的数据的个数,你定义的存储数据的数组大小也就决定了,但是若你事先不知道 ...

  9. HTML5的form表单属性

    form:HTML4中,表单内的从属元素必须书写在<form></form>之内,但是在HTML5中,表单的从属元素可以处于页面的任何位置,然后为其添加form属性,属性值为f ...

  10. Java输入一行数据并转存到数组中

    直接看下面的代码吧!主要是split和foreach的使用 import java.io.*; import java.util.*; public class Main{ public static ...