39. Combination Sum

Combination,组合问题用DFS罗列全部的答案。

class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> res = new ArrayList<>();
if(candidates == null || candidates.length == 0) return res;
dfs(candidates, target, 0, res, new ArrayList<Integer>());
return res;
} //1.定义
private void dfs(int[] candidates, int target, int index, List<List<Integer>> res, List<Integer> subset){
//3.出口
if(target == 0){
res.add(new ArrayList<Integer>(subset));
return;
}
if(target < 0){
return;
}
//2.拆解
for(int i = index; i < candidates.length; i++){
subset.add(candidates[i]);
dfs(candidates, target - candidates[i], i, res, subset);
subset.remove(subset.size() - 1);
} }
}

40. Combination Sum II

1.  为了防止candidates[ ]中的数字被重复使用,DFS中的index使用 i + 1。

2.防止答案中出现重复的数字使用情况,

 if(i > index && candidates[i] == candidates[i - 1]) continue;
即当前层的数字只能被重复使用一次。
class Solution {
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<List<Integer>> res = new ArrayList<>();
if(candidates == null || candidates.length == 0) return res;
Arrays.sort(candidates);
dfs(candidates, target, 0, res, new ArrayList<>());
return res;
} private void dfs(int[] candidates, int target, int index, List<List<Integer>> res, List<Integer> subset){
//出口
if(target == 0){
res.add(new ArrayList<Integer>(subset));
return;
}
if(target < 0){
return;
}
//拆解
for(int i = index; i < candidates.length; i++){
if(i > index && candidates[i] == candidates[i - 1]) continue;
subset.add(candidates[i]);
dfs(candidates, target - candidates[i], i + 1, res, subset);
subset.remove(subset.size() - 1);
}
}
}

<BackTracking> dfs: 39 40的更多相关文章

  1. leetcode 39 dfs leetcode 40 dfs

    leetcode 39 先排序,然后dfs 注意先整全局变量可以减少空间利用 class Solution { vector<vector<int>>ret; vector&l ...

  2. leetcode四道组合总和问题总结(39+40+216+377)

    39题目: 链接:https://leetcode-cn.com/problems/combination-sum/ 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 ...

  3. Leetcode 39 40 216 Combination Sum I II III

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

  4. Leetcode216/39/40/77之回溯解决经典组合问题

    Leetcode216-组合总和三 找出所有相加之和为 n 的 k 个数的组合,且满足下列条件: 只使用数字1到9 每个数字 最多使用一次 返回 所有可能的有效组合的列表 .该列表不能包含相同的组合两 ...

  5. ZOJ - 2477 dfs [kuangbin带你飞]专题二

    注意输入的处理,旋转操作打表.递增枚举可能步数,作为限制方便找到最短路. AC代码:90ms #include<cstdio> #include<cstring> char m ...

  6. dfs | Security Badges

    Description You are in charge of the security for a large building, with n rooms and m doors between ...

  7. 蓝桥杯 算法提高 8皇后·改 -- DFS 回溯

      算法提高 8皇后·改   时间限制:1.0s   内存限制:256.0MB      问题描述 规则同8皇后问题,但是棋盘上每格都有一个数字,要求八皇后所在格子数字之和最大. 输入格式 一个8*8 ...

  8. poj1564-Sum It Up(经典DFS)

    给出一个n,k,再给出的n个数中,输出所有的可能使几个数的和等于k Sample Input 4 6 4 3 2 2 1 15 3 2 1 1400 12 50 50 50 50 50 50 25 2 ...

  9. [hdu5416 CRB and Tree]树上路径异或和,dfs

    题意:给一棵树,每条边有一个权值,求满足u到v的路径上的异或和为s的(u,v)点对数 思路:计a到b的异或和为f(a,b),则f(a,b)=f(a,root)^f(b,root).考虑dfs,一边计算 ...

随机推荐

  1. C# 原子操作理解

    C#内置提供的原子操作 Interlocked.Increment:以原子操作的形式递增指定变量的值并存储结果. Interlocked.Decrement:以原子操作的形式递减指定变量的值并存储结果 ...

  2. Azure EA (3) 使用Postman访问海外Azure Billing API

    <Windows Azure Platform 系列文章目录> 本文介绍的是海外版的Azure Global服务,因为跨境内境外网络,访问速度会比较慢 在开始使用Azure Billing ...

  3. 大话设计模式Python实现-工厂方法模式

    工厂方法模式(Factory Method Pattern):定义一个用于创建对象的接口,让子类决定实例化哪一个类,工厂方法使一个类的实例化延时到其子类. #!/usr/bin/env python ...

  4. 前端实现的canvas支持多图压缩并打包下载的工具

    # 技术栈 canvas jszip.js(网页端压缩解压缩插件JSZIP库) FileSaver.js(文件保存到本地库) 在线预览:http://htmlpreview.github.io/?ht ...

  5. 将Excel表格数据转换成Datatable

    /// <summary> /// 将Excel表格数据转换成Datatable /// </summary> /// <param name="fileUrl ...

  6. Spring-AOP源码分析随手记(二)

    这次来分析下切面的执行过程. 1.怎么看? 怎么开始看源码呢?就直接从被增强的方法调用那里打断点,看看怎么执行的: 然后就来到了这: 2.初步分析 里面有段: if (this.advised.exp ...

  7. Java多线程——ThreadLocal类的原理和使用

    Java多线程——ThreadLocal类的原理和使用 摘要:本文主要学习了ThreadLocal类的原理和使用. 概述 是什么 ThreadLocal可以用来维护一个变量,提供了一个ThreadLo ...

  8. Java生鲜电商平台-商品无限极目录的设计与架构

    Java生鲜电商平台-商品无限极目录的设计与架构 说明:任何一个商品都应该是先属于某一个目录,然后在目录中添加商品,目录理论上最多支持三级,因为级别太多,不容易管理.但是设计中需要设计无限制的级别. ...

  9. 表单验证如何让select设置为必选

    <select class="custom-select mr-sm-2" name="province" id="province" ...

  10. weblogic 安全漏洞问题解决

    1 weblogic控制台地址暴露 ²  整改方法: 禁用weblogic控制台.在weblogic域(sguap-domain和base-domain)的config下的config.xml中 &l ...