216. Combination Sum III

保证subset.size( ) == k && target == 0的时候结束DFS

subset.size( ) > k || target < 0返回。

class Solution {
public List<List<Integer>> combinationSum3(int k, int n) {
List<List<Integer>> res = new ArrayList<>();
if(k <= 0 || n < 1) return res;
dfs(res, new ArrayList<Integer>(), k, n, 1);
return res;
} private void dfs(List<List<Integer>> res, List<Integer> subset, int k, int target, int index){
if(subset.size() > k && target < 0)
return;
if(subset.size() == k && target == 0){
res.add(new ArrayList<Integer>(subset));
return;
}
for(int i = index; i <= 9; i++){
subset.add(i);
dfs(res, subset, k, target - i, i + 1);
subset.remove(subset.size() - 1);
}
}
}

377. Combination Sum IV

DFS: 此方法超时

class Solution {
int count;
public int combinationSum4(int[] nums, int target) {
if(nums == null || nums.length == 0) return 0;
dfs(nums, target, new ArrayList<Integer>());
return count;
} private void dfs(int[] nums, int target, List<Integer> subset){
if(target == 0){
count++;
return;
}
if(target < 0){
return;
}
for(int i = 0; i < nums.length; i++){
subset.add(nums[i]);
dfs(nums, target - nums[i], subset);
subset.remove(subset.size() - 1);
}
}
}

DP: i - nums[ j ]表 当前nums[ j ]与之前comb[ ] 中的组合数字继续组合得到target。

class Solution {
public int combinationSum4(int[] nums, int target) {
int[] comb = new int[target + 1];
comb[0] = 1;
for(int i = 1; i < comb.length; i++){
for(int j = 0; j < nums.length; j++){
if(i - nums[j] >= 0){
comb[i] += comb[i - nums[j]];
}
}
}
return comb[target];
}
}

<BackTracking> Combination, DFS :216 DP: 377的更多相关文章

  1. UvaLive6661 Equal Sum Sets dfs或dp

    UvaLive6661 PDF题目 题意:让你用1~n中k个不同的数组成s,求有多少种组法. 题解: DFS或者DP或打表. 1.DFS 由于数据范围很小,直接dfs每种组法统计个数即可. //#pr ...

  2. P1021 邮票面值设计(dfs+背包dp)

    P1021 邮票面值设计 题目传送门 题意: 给定一个信封,最多只允许粘贴N张邮票,计算在给定K(N+K≤15N+K≤15)种邮票的情况下 (假定所有的邮票数量都足够),如何设计邮票的面值,能得到最大 ...

  3. dfs与dp算法之关系与经典入门例题

    目录 声明 dfs与dp的关系 经典例题-数字三角形 - POJ 1163 题目 dfs思路 解题思路 具体代码 dp思路 解题思路 具体代码 声明 本文不介绍dfs.dp算法的基础思路,有想了解的可 ...

  4. DFS与DP算法

    名词解释: DFS(Dynamic Plan):动态规划 DFS(Depth First Search):深度优先搜索 DFS与DP的关系 很多情况下,dfs和dp两种解题方法的思路都是很相似的,这两 ...

  5. B. Kay and Snowflake 解析(思維、DFS、DP、重心)

    Codeforce 685 B. Kay and Snowflake 解析(思維.DFS.DP.重心) 今天我們來看看CF685B 題目連結 題目 給你一棵樹,要求你求出每棵子樹的重心. 前言 完全不 ...

  6. 377. Combination Sum IV——DP本质:针对结果的迭代,dp[ans] <= dp[ans-i] & dp[i] 找三者关系 思考问题的维度+1,除了数据集迭代还有考虑结果

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  7. UVALive 6257 Chemist's vows --一道题的三种解法(模拟,DFS,DP)

    题意:给一个元素周期表的元素符号(114种),再给一个串,问这个串能否有这些元素符号组成(全为小写). 解法1:动态规划 定义:dp[i]表示到 i 这个字符为止,能否有元素周期表里的符号构成. 则有 ...

  8. Codeforces Good bye 2015 B. New Year and Old Property dfs 数位DP

    B. New Year and Old Property 题目连接: http://www.codeforces.com/contest/611/problem/B Description The y ...

  9. 杭电OJ——1011 Starship Troopers(dfs + 树形dp)

    Starship Troopers Problem Description You, the leader of Starship Troopers, are sent to destroy a ba ...

随机推荐

  1. Tomca原理分析之责任链

    责任链使用位置:Container处理请求 Container处理请求是使用Pipeline-Valve管道来处理的!(Valve是阀门之意) Pipeline-Valve是责任链模式,责任链模式是指 ...

  2. spring boot 开启https

    1.生成证书 keytool -genkey -alias tomcat -keyalg RSA -keystore E:/https.keystore 将生成好的证书放在项目根目录即可 2 修改配置 ...

  3. docker 更新内存限制步骤

    停止容器: docker stop id 更新配额: docker update -m 80G id 内存参数和大小 容器ID重启容器:docker start id

  4. 读取指定页面中的超链接-Python 3.7

    #!/usr/bin/env python#coding: utf-8from bs4 import BeautifulSoupimport urllibimport urllib.requestim ...

  5. F#周报2019年第23期

    新闻 支持社区的WF与WCF开源项目 视频及幻灯片 F# MonoGame平台游戏系列:摄像头 Xamarin.Forms的F#与Fabulous ML.NET端到端之二:构建Web API 使用F# ...

  6. 用Python帮你上马,哪里无码打哪里

    目录 0 引言 1 环境 2 需求分析 3 代码实现 4 代码全景展示 5 后记 0 引言 所谓的像素图,就是对图像做一个颗粒化的效果,使其产生一种妙不可言的朦胧感.费话不多说,先来看一张效果图. ▲ ...

  7. WPF ListView ,XML

    <?xml version="1.0" encoding="utf-8" ?><PersonList> <Person Id=&q ...

  8. scrapy学习笔记(二)框架结构工作原理

    scrapy结构图: scrapy组件: ENGINE:引擎,框架的核心,其它所有组件在其控制下协同工作. SCHEDULER:调度器,负责对SPIDER提交的下载请求进行调度. DOWNLOADER ...

  9. CTF必备技能丨Linux Pwn入门教程——PIE与bypass思路

    Linux Pwn入门教程系列分享如约而至,本套课程是作者依据i春秋Pwn入门课程中的技术分类,并结合近几年赛事中出现的题目和文章整理出一份相对完整的Linux Pwn教程. 教程仅针对i386/am ...

  10. bootstrap的表单form

    (1)默认表单 <form> <div class="form-group"> <label class="control-label&qu ...