<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 ...
随机推荐
- golang module 在 spacemcs 中的配置
概述 golang 官方的包管理从 1.11 版本就开始支持了, 之前尝试了几次, 效果都不理想, 就一直用 dep 来管理 package. 最近 1.13 版本发布了, 使用 go module ...
- Win10修改hosts文件并配置DNS
1.打开C:\Windows\System32\drivers\etc目录 2.去掉hosts文件的只读属性 3.添加dns解析配置 127.0.0.1 www.example.c ...
- WebBrowser中html元素如何触发winform事件 z
只要注册一下事件就可以了. C#代码如下: using System;using System.ComponentModel;using System.Windows.Forms; namespace ...
- 动手学深度学习10- pytorch多层感知机从零实现
多层感知机 定义模型的参数 定义激活函数 定义模型 定义损失函数 训练模型 小结 多层感知机 import torch import numpy as np import sys sys.path.a ...
- LeetCode 283:移动零 Move Zeroes
给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序. Given an array nums, write a function to move all 0' ...
- 生成 RSA 公钥和私钥的方法
在使用 RSA 加密算法时,需要使用到一对 公钥 和 私钥,生成 公钥 和 私钥 需要借助 openssl 这款工具,下载这款工具的地址如下: http://slproweb.com/products ...
- 机器学习(十一)-------- 异常检测(Anomaly Detection)
异常检测(Anomaly Detection) 给定数据集
- 一个人的公众号,我写了1w+
大家好,我是Bypass,一个人一直保持着写博客的习惯,为此维护了一个技术公众号,致力于分享原创高质量干货,写的内容主要围绕:渗透测试.WAF绕过.代码审计.应急响应.企业安全. 一直以来,我把它当成 ...
- C#调用Activex中串口电子秤的数据,并将电子秤的数据显示到前端页面
大二的一个项目需要用到Activex技术将读取到串口中的数据在后台获取到,并将串口的数据写入数据库,这个过程需要在后台使用C#调用Activex控件已经使用的方法,然后在前端通过JavaScript进 ...
- Python实现MQTT接收订阅数据
一.背景 目前MQTT的标准组织官网:http://www.mqtt.org,里面列出了很多支持的软件相关资源. 一个轻量级的MQTT服务器是:http://www.mosquitto.org,可以运 ...