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. [NewLife.XCode]分表分库(百亿级大数据存储)

    NewLife.XCode是一个有15年历史的开源数据中间件,支持netcore/net45/net40,由新生命团队(2002~2019)开发完成并维护至今,以下简称XCode. 整个系列教程会大量 ...

  2. (三十四)golang--接口

    golang的多态特性主要体现在接口上: 主要优势:高内聚低耦合: package main import ( "fmt" ) type usb interface { start ...

  3. JVM内存溢出分析java.lang.OutOfMemoryError: Java heap space

    JVM内存溢出查询java.lang.OutOfMemoryError: Java heap space查出具体原因分为几个预备步骤 1.在运行java程序是必须设置jvm -XX:+HeapDump ...

  4. ubuntu18.04.2下编译openjdk9源码

    最近在看<深入理解Java虚拟机 第二版>这本书,上面有关于自己编译OpenJDK源码的内容.自己根据书里的指示去操作,花了三天的时间,重装了好几次Ubuntu(还不知道快照这个功能,好傻 ...

  5. Python platform 模块

    Python platform 模块 platform 模块用于查看当前操作系统的信息,来采集系统版本位数计算机类型名称内核等一系列信息. 使用方法: import platform # 获取操作系统 ...

  6. redis笔记3

    redis持久化机制 redis提供了两种持久化策略 RDB RDB的持久化策略: 按照规则定时将内存的数据同步到磁盘 snapshot redis在指定的情况下会触发快照 自己配置的快照规则 sav ...

  7. php字符串查找函数 php查找字符串中出现的次数函数substr_count,判断字符串中是否包含另一个字符串函数strpos

    php字符串查找函数 php查找字符串中出现的次数函数substr_count,判断字符串中是否包含另一个字符串函数strpossubstr_count($haystack, $needle [,$o ...

  8. Docker制作dotnet core控制台程序镜像

    (1)首先我们到某个目录下,然后在此目录下打开visual studio code. 2.编辑docker file文件如下: 3.使用dotnet new console创建控制台程序; 4.使用d ...

  9. Struts2 Action的3种创建方式

    Action是Strut2的核心内容,相当于Servlet,用于处理业务. Action是一个Java类,直接新建Java类即可. Action有3种实现方式. 1.使用POJO,设置成员变量,写对应 ...

  10. Django 使用 mysql 数据库连接

    启用 mysql 数据库连接 修改 app01 下的 __init__.py import pymysql pymysql.install_as_MySQLdb() 修改 settings.py DA ...