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. geth 基本使用

    概要 geth 是以太坊的官方 golang 客户端. 通过 geth 的使用可以直观的了解以太坊, 乃至区块链的运作. 下面, 通过 geth 来构造一次搭建私链, 创建账户, 挖矿, 交易的流程. ...

  2. Unreal Engine 4 系列教程 Part 10:制作简单FPS游戏

    .katex { display: block; text-align: center; white-space: nowrap; } .katex-display > .katex > ...

  3. Python连载26-shelve模块

    一.持久化 --shelve 持久化工具 (1)作用:类似字典,用kv对保存数据,存取方式类似于字典 (2)例子:通过一下案例创建了一个数据库,第二个程序我们读取了数据库 #使用shelve创建文件并 ...

  4. 使用OpenCL提升OpenCV图像处理性能 | speed up opencv image processing with OpenCL

    本文首发于个人博客https://kezunlin.me/post/59afd8b3/,欢迎阅读最新内容! speed up opencv image processing with OpenCL G ...

  5. 在 .NET Core 中使用异步的 ADO.NET 的简单示例

    直接贴代码: Program.cs using Microsoft.Extensions.Configuration; using System; using System.Data; using S ...

  6. Knative 初体验:CICD 极速入门

    Knative 社区很早就在讨论用 Tekton 替换 Build 模块的相关事宜.Knative Build 官方已经正式说明不再建议使用 Knative Build 了. 如果你知道 Knativ ...

  7. MySQL5.7安装脚本

    目录结构: install_mysql.sh:安装脚本 my.cnf: MySQL配置文件 mysql--linux-glibc2.-x86_64.tar.gz:MySQL二进制包 以下为目录中的文件 ...

  8. C# NPOI Excel

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  9. aspx.designer.cs没有自动生成代码(没有自动注册)

    遇到这个问题的最大可能是:aspx页面存在bug. 比如说我的主页是从项目里的别的页面复制过来的,但是少复制了一些引用,页面就存在bug,导致aspx.designer.cs没有自动生成代码. 解决方 ...

  10. OWIN,Katana,identity整体概述

    在用asp.net identity的时候,发现很多概念不是很懂,特地去查资料了解了一些相关信息,现在做下笔记. 1.OWIN,OWIN是Open Web Server Interface for . ...