当前位置: 首页 > news >正文

【刷题】DFS

DFS

递归:
1.判断是否失败终止
2.判断是否成功终止,如果成功的,记录一个成果
3.遍历各种选择,在这部分可以进行剪枝
4.在每种情况下进行DFS,并进行回退。

199. 二叉树的右视图

给定一个二叉树的 根节点 root,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。
示例 1:
在这里插入图片描述
输入: [1,2,3,null,5,null,4]
输出: [1,3,4]
示例 2:
输入: [1,null,3]
输出: [1,3]
示例 3:
输入: []
输出: []

class Solution {
public:vector<int> rightSideView(TreeNode* root) {unordered_map<int, int> rightmostValueAtDepth;int max_depth = -1;stack<TreeNode*> nodeStack;stack<int> depthStack;nodeStack.push(root);depthStack.push(0);while (!nodeStack.empty()) {TreeNode* node = nodeStack.top();nodeStack.pop();int depth = depthStack.top();depthStack.pop();if (node != NULL) {// 维护二叉树的最大深度max_depth = max(max_depth, depth);// 如果不存在对应深度的节点我们才插入if (rightmostValueAtDepth.find(depth) == rightmostValueAtDepth.end()) {rightmostValueAtDepth[depth] =  node -> val;}nodeStack.push(node -> left);nodeStack.push(node -> right);depthStack.push(depth + 1);depthStack.push(depth + 1);}}vector<int> rightView;for (int depth = 0; depth <= max_depth; ++depth) {rightView.push_back(rightmostValueAtDepth[depth]);}return rightView;}
};

39. 组合总和

给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target ,找出 candidates 中可以使数字和为目标数 target 的 所有 不同组合 ,并以列表形式返回。你可以按 任意顺序 返回这些组合。
candidates 中的 同一个 数字可以 无限制重复被选取 。如果至少一个数字的被选数量不同,则两种组合是不同的。
对于给定的输入,保证和为 target 的不同组合数少于 150 个。
示例 1:
输入:candidates = [2,3,6,7], target = 7
输出:[[2,2,3],[7]]
解释:
2 和 3 可以形成一组候选,2 + 2 + 3 = 7 。注意 2 可以使用多次。
7 也是一个候选, 7 = 7 。
仅有这两种组合。
示例 2:
输入: candidates = [2,3,5], target = 8
输出: [[2,2,2,2],[2,3,3],[3,5]]
示例 3:
输入: candidates = [2], target = 1
输出: []

class Solution {
public:void dfs(vector<int>& candidates, int target, vector<vector<int>>& ans, vector<int>& combine, int index) {if (index >= candidates.size()) return;if (target==0) {ans.emplace_back(combine);return;}dfs(candidates, target, ans, combine, index+1);if (candidates[index]<=target){combine.push_back(candidates[index]);dfs(candidates, target-candidates[index], ans, combine, index);combine.pop_back();}}vector<vector<int>> combinationSum(vector<int>& candidates, int target) {vector<vector<int>> ans;vector<int> combine;dfs(candidates, target, ans, combine, 0);return ans;}
};
http://www.lryc.cn/news/248321.html

相关文章:

  • Gin投票系统(2)
  • docker (简介、dcoker详细安装步骤、容器常用命令)一站打包- day01
  • 请简要说明 Mysql 中 MyISAM 和 InnoDB 引擎的区别
  • Nginx漏洞复现与分析
  • Go 中切片(Slice)的长度与容量
  • 顶级大厂Quora如何优化数据库性能?
  • Java第二十章多线程
  • 家庭教育,培养娃什么最重要?
  • Linux 进程(一)
  • vue中的keep-alive详解与应用场景
  • 软件设计师——程序设计语言基础(一)
  • Apache简介与安装
  • set与map
  • 基于单片机智能液位水位监测控制系统
  • C#,《小白学程序》第十七课:随机数(Random)第四,移动平均值(Moving Average)的计算方法与代码
  • 行情分析——加密货币市场大盘走势(11.29)
  • C++——string的字符串比较,字符存取,插入和删除和子串
  • 字节10年经验之谈 —— 从0到1开发自动化测试框架!
  • Mysql(基本介绍+下载安装+服务器+基本使用+建库建表+navicat/mybitas工具+外键及实例)
  • Python+requests+Jenkins接口自动化测试实例
  • SpringBoot3核心原理
  • JS常用数据类型转换(数字型和字符串型之间转换)
  • 算法通关村第一关—青铜挑战—用Java基本实现各种链表操作
  • SparkRDD及算子-python版
  • 嵌入式设备与PC上位机通信协议设计的几点原则
  • Go 内置运算符
  • Table和HashBasedTable的使用案例
  • 【执行批处理后 executeBatch() 没反应,一个参数相信就能搞定】
  • 【LeetCode】每日一题 2023_11_25 二叉树中的伪回文路径(dfs,数组/位运算)
  • 什么是海外私人IP代理?是纯净独享的代理吗?