<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 ...
随机推荐
- Java实现字符串反转【Leetcode】
Write a function that reverses a string. The input string is given as an array of characters char[]. ...
- Vue 学习记录(一)-创建项目
环境准备 node.js vue-cli 安装配置环境 1.下载node.js,使用默认配置安装 . 2.使用npm命令安装国内下载镜像(可选) cmd: npm install -g cnpm ...
- webpack与vue环境搭建(转载)
原文:https://www.cnblogs.com/lgx5/p/10732016.html npm安装教程 一.使用之前,我们先来掌握3个东西是用来干什么的. npm: Nodejs下的包管理 ...
- 在项目中使用Solr
web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=" ...
- 安装docker后修改docker文件目录
docker会下载容器,运行会挂载磁盘,所以我们需要把docker装在大容量的分区. 安装 https://docs.docker.com/install/linux/docker-ce/centos ...
- Entity Framework 6 中如何获取 EntityTypeConfiguration 的 Edm 信息?(三)
接着上一篇,我们继续来优化. 直接贴代码了: LambdaHelper.cs using System; using System.Collections.Generic; using System. ...
- ExcelHelper based on NPOI
//Export data to excel via NPOI public static void ExportDataTableToExcel(DataTable dataTable, strin ...
- 使用T4模板同时生成多个类文件
代码: <#@ template language="C#" debug="false" hostspecific="true"#&g ...
- Fiddler使用简单
一,fiddler简介 1.1,什么是fiddler Fiddler是一个http协议调试代理工具,它能够记录并检查所有你的电脑和互联网之间的http通讯,设置断点,查看所有的“进出”Fiddler ...
- spring事务的三种配置应用实例
0.项目结构 具体代码见:https://github.com/xkzhangsan/spring-transaction-practice.git,包括创建表sql在内. 1.编程式事务使用Data ...