Combination Sum II
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的更多相关文章
- 【leetcode】Combination Sum II
Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...
- 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 ...
- [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 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III
39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...
- 【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 ...
- LeetCode: Combination Sum II 解题报告
Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all uni ...
- 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-combination sum and combination sum II
Combination sum: Given a set of candidate numbers (C) and a target number (T), find all unique combi ...
- Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II)
Leetcode之回溯法专题-40. 组合总和 II(Combination Sum II) 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使 ...
随机推荐
- HttpRequestUtil
package com.didichuxing.tempdirreader; import com.alibaba.fastjson.JSONObject; import java.io.Unsupp ...
- Chinese economic influence in North Korea
Where this new investment is being targeted is also interesting雄性禿 . "If you look at the econom ...
- 如何让UIView中的Button点击之后跳转到另一个ViewController上去,ViewController上也有一个按钮 可以返回
第一种方法:如果使用导航第一个按钮方法:[self.navigationController pushViewController:secondVC animated:YES];第二个按钮方法:[se ...
- google 账号登陆chrome内容是中文的问题
最近要用到google accout使用某项服务,奇怪的是之前是英文,登陆后就显示中文了,我把浏览器默认的语言和电脑的系统语言改了也无济于事,最好还是google 帮忙解决了,原来我的google a ...
- Session阻塞 读写锁引发的小问题
引子 我们来看两个ajax方法,先后的问题,会有什么样的结果? Javascript: $(function () { //第一个 $.ajax({ type: "POST&qu ...
- MySQL数据库7 - 汇总和分组数据
一 汇总和分组数据 查询语句 ---> 结果集(多条数据) ---> 聚合函数 ----> 单行记录 1.常用的聚合函数: sum() 数字 ...
- 数据结构《20》----Immutable stack
有趣的函数式数据结构<一>----不可变栈 什么是不可变?往栈中插入一个元素,原来的栈保持不变,返回一个新的栈(已插入新的元素). push, pop,getMax 等操作都要求在 常数时 ...
- ASP.NET MVC3的学习
ASP.NET MVC第一次课(2013-12-25晚学完) 1.ASP.NET MVC 的特点 分离任务 可扩展 强大的URL重写(路由)机制 ...
- C++深拷贝与浅拷贝
当用一个已初始化过了的自定义类类型对象去初始化另一个新构造的对象的时候,拷贝构造函数就会被自动调用.也就是说,当类的对象需要拷贝时,拷贝构造函数将会被调用.以下情况都会调用拷贝构造函数: (1)一个对 ...
- $.ajax请求返回数据中status为200,回调的却是error?
$.ajax({ type:'get',//使用get方法访问后台 dataType:'json',//访问json格式的数据 url:'http://job.hainan.net/api/recru ...