当前位置:网站首页>每日一题-DFS
每日一题-DFS
2022-08-05 05:17:00 【菜鸡程序媛】
目录
组合总和
- 时间:0727
- 题目
给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target ,找出 candidates 中可以使数字和为目标数 target 的 所有 不同组合 ,并以列表形式返回。你可以按 任意顺序 返回这些组合。
- 思路
- 通过深度遍历的方式,找到满足条件的总和
- 当满足的时候,开始回溯,再从新的分支继续找,回溯的时候一定要记得状态重置
- ️ dfs向下遍历的时候,下标要用循环中的i而不是begin,如果用begin的话,会导致前面的数字会重复遍历并错误使用
- 代码
class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
if(candidates == null || candidates.length <= 0)
return null;
List<List<Integer>> res = new ArrayList<>();
LinkedList<Integer> sb = new LinkedList<>();
dfs(candidates, 0, target, res, sb);
return res;
}
private void dfs(int[] candidates, int begin, int target, List<List<Integer>> res, LinkedList<Integer> sb){
if(target < 0)
return;
if(target == 0){
res.add(new ArrayList<>(sb));
begin += 2;
return;
}
for(int i = begin; i < candidates.length; i ++){
sb.addLast(candidates[i]);
//这里一定要是i,如果是begin的话,就会造成重复查找
dfs(candidates, i, target - candidates[i], res, sb);
sb.removeLast();
}
}
}全排列
- 时间:0729
- 题目
给定一个不含重复数字的数组 nums ,返回其 所有可能的全排列 。你可以 按任意顺序 返回答案。
- 思路
- 这个题目是找到所有可能的排列组合,组合内的数字不能重复出现,这就要求有一个数组记录状态
- 这个题和上面的题目除了满足的目标条件不一样,还有一点就是上面的题目是要找等于目标值的子数组,前面的数字用过了就不需要二次访问了;但是本题全排列,每个数字都需要出现在所有的结果子集里面,所以就不需要记录当前已经访问到哪个下标了。
- 代码
class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
if(nums == null || nums.length == 0)
return res;
LinkedList<Integer> list = new LinkedList<>();
// 用来存储已经访问过的元素
boolean[] used = new boolean[nums.length];
dfs(nums, res, list, used);
return res;
}
private void dfs(int[] nums, List<List<Integer>> res, LinkedList<Integer> list, boolean[] used){
if(list.size() == nums.length){
res.add(new ArrayList<>(list));
return;
}
for(int i = 0; i < nums.length; i ++){
if(used[i])
continue;
list.addLast(nums[i]);
used[i] = true;
dfs(nums, res, list, used);
used[i] = false;
list.removeLast();
}
}
}边栏推荐
- idea 快速日志
- LeetCode刷题之第74题
- 「实用」运维新手一定不能错过的17 个技巧
- ECCV2022 | RU & Google propose zero-shot object detection with CLIP!
- CVPR2021 - Inception Convolution with Efficient Dilation Search
- MaskDistill - Semantic segmentation without labeled data
- 深度学习系列(二)优化器 (Optimization)
- 【论文阅读-表情捕捉】High-quality Real Time Facial Capture Based on Single Camera
- 教你如何封装功能组件和页面组件
- 九、响应处理——内容协商底层原理
猜你喜欢

leetCode刷题之第31题

十、视图解析原理与源码分析

伪RTOS-ProroThread在CH573芯片上的移植

6k+ star,面向小白的深度学习代码库!一行代码实现所有Attention机制!

读论文 - Unpaired Portrait Drawing Generation via Asymmetric Cycle Mapping

C语言入门笔记 —— 分支与循环

CVPR 2022 | 70% memory savings, 2x faster training

LeetCode刷题之第54题

【UiPath2022+C#】UiPath If条件语句
![[Database and SQL study notes] 8. Views in SQL](/img/22/82f91388f06ef4f9986bf1e90800f7.png)
[Database and SQL study notes] 8. Views in SQL
随机推荐
LeetCode刷题之第55题
【UiPath2022+C#】UiPath 练习-数据操作
HuiFer 带你读懂 BeanFactory getBean 方法
1008 数组元素循环右移问题 (20 分)
表情捕捉的指标/图像的无参考质量评价
「实用」运维新手一定不能错过的17 个技巧
C语言入门笔记 —— 初识
(C语言)strlen、strcpy、strcat、strcmp、strstr函数的模拟实现
电子产品量产工具(5)- 页面系统实现
如何组织一场安全、可靠、高效的网络实战攻防演习?
面向小白的深度学习代码库,一行代码实现30+中attention机制。
ECCV2022 | RU&谷歌提出用CLIP进行zero-shot目标检测!
LeetCode刷题之第530题
十一、拦截器运行原理
ACL 的一点心得
网络信息安全运营方法论 (中)
基于STM32F407的WIFI通信(使用的是ESP8266模块)
发顶会顶刊论文,你应该这样写作
CVPR最佳论文得主清华黄高团队提出首篇动态网络综述
[After a 12] No record for a whole week