<BackTracking> Combination, DFS :216 DP: 377
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的更多相关文章
- UvaLive6661 Equal Sum Sets dfs或dp
UvaLive6661 PDF题目 题意:让你用1~n中k个不同的数组成s,求有多少种组法. 题解: DFS或者DP或打表. 1.DFS 由于数据范围很小,直接dfs每种组法统计个数即可. //#pr ...
- P1021 邮票面值设计(dfs+背包dp)
P1021 邮票面值设计 题目传送门 题意: 给定一个信封,最多只允许粘贴N张邮票,计算在给定K(N+K≤15N+K≤15)种邮票的情况下 (假定所有的邮票数量都足够),如何设计邮票的面值,能得到最大 ...
- dfs与dp算法之关系与经典入门例题
目录 声明 dfs与dp的关系 经典例题-数字三角形 - POJ 1163 题目 dfs思路 解题思路 具体代码 dp思路 解题思路 具体代码 声明 本文不介绍dfs.dp算法的基础思路,有想了解的可 ...
- DFS与DP算法
名词解释: DFS(Dynamic Plan):动态规划 DFS(Depth First Search):深度优先搜索 DFS与DP的关系 很多情况下,dfs和dp两种解题方法的思路都是很相似的,这两 ...
- B. Kay and Snowflake 解析(思維、DFS、DP、重心)
Codeforce 685 B. Kay and Snowflake 解析(思維.DFS.DP.重心) 今天我們來看看CF685B 題目連結 題目 給你一棵樹,要求你求出每棵子樹的重心. 前言 完全不 ...
- 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 ...
- UVALive 6257 Chemist's vows --一道题的三种解法(模拟,DFS,DP)
题意:给一个元素周期表的元素符号(114种),再给一个串,问这个串能否有这些元素符号组成(全为小写). 解法1:动态规划 定义:dp[i]表示到 i 这个字符为止,能否有元素周期表里的符号构成. 则有 ...
- 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 ...
- 杭电OJ——1011 Starship Troopers(dfs + 树形dp)
Starship Troopers Problem Description You, the leader of Starship Troopers, are sent to destroy a ba ...
随机推荐
- 【转】Linux设置定时任务方法
设置:每天4点运行脚本/var/x/web/train/modeltrain [root@T-XXX-ML-01 log]# crontab -e0 4 * * * /var/x/web/train/ ...
- webrtc笔记(3): 多人视频通讯常用架构Mesh/MCU/SFU
问题:为什么要搞这么多架构? webrtc虽然是一项主要使用p2p的实时通讯技术,本应该是无中心化节点的,但是在一些大型多人通讯场景,如果都使用端对端直连,端上会遇到很带宽和性能的问题,所以就有了下图 ...
- Java连载38-对象封装性格式、构造方法
一.封装的步骤 (1)所有属性私有化,使用private关键字进行修饰,private表示私有的,修饰的所有数据只能在本类中进行访问. (2)对外提供简单的操作入口,也就是说以后外部程序要想访问age ...
- 基于OceanStor Dorado V3存储之数据保护 Hyper 特性
基于OceanStor Dorado V3存储之数据保护 Hyper 特性 1.1 快照 1.2 HyperCDP 1.3 HyperCopy 1.4 克隆(HyperClone) 1.5 ...
- [Zabbix] 安装MySQL5.7, 部署Zabbix到CentOS 7日记
安装环境:CentOS7 64位,安装MySQL5.7 一.安装 MySQL 1.配置YUM源 在MySQL官网中下载YUM源rpm安装包:http://dev.mysql.com/downloads ...
- Winform中设置ZedGraph鼠标焦点位置画出十字线并在鼠标移出时十字线消失
场景 Winforn中设置ZedGraph曲线图的属性.坐标轴属性.刻度属性: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/10 ...
- charles注册码及中文版本,支持window和mac
安装证书: 安装完证书之后设置代理 2个* ,代表全部 注册码: Registered Name: https://zhile.io License Key: 48891cf209c6d32bf4 破 ...
- Python常用代码,置顶备用!
1.jupyter notebook 设置全部行输出: # 设置全部行输出 from IPython.core.interactiveshell import InteractiveShellInte ...
- Webpack相关原理浅析
基本打包机制 本质上,webpack 是一个现代 JavaScript 应用程序的静态模块打包器(module bundler).当 webpack 处理应用程序时,它会递归地构建一个依赖关系图(de ...
- linux源代码获取
Ubuntu获取 # which ls /bin/ls # dpkg -S /bin/ls coreutils: /bin/ls # apt-get source coreutils CentOS获取 ...