摘要: 455.分发饼干 题目链接 文章讲解 视频讲解 class Solution { public: int findContentChildren(vector<int>& g, vector<int>& s) { sort(g.begin(), g.end()); sort(s.begin(), s 阅读全文
posted @ 2024-06-07 22:11 深蓝von 阅读(2) 评论(0) 推荐(0) 编辑
摘要: 51.N 皇后 题目链接 文章讲解 视频讲解 递归三部曲 递归函数参数 需要传入当前chessBoard和棋盘大小n,以及当前要放置皇后的行数rowvoid backtracking(vector<string>& chessBoard, int n, int row); 递归终止条件 当最后一个皇 阅读全文
posted @ 2024-06-07 17:07 深蓝von 阅读(2) 评论(0) 推荐(0) 编辑
摘要: 491.非递减子序列 题目链接 文章讲解 视频讲解 层间去重: 回溯法相当于深搜,所以所以是一直递归到叶节点才开始回溯; 每次进入backtracking也就进入了搜索树的下一层,所以每进入一层需要用一个used_set来记录使用过的元素; class Solution { private: vec 阅读全文
posted @ 2024-06-06 21:52 深蓝von 阅读(1) 评论(0) 推荐(0) 编辑
摘要: 93.复原IP地址 题目链接 文章讲解 视频讲解 class Solution { private: vector<string> ip; vector<string> result; public: vector<string> restoreIpAddresses(string s) { bac 阅读全文
posted @ 2024-06-06 16:24 深蓝von 阅读(2) 评论(0) 推荐(0) 编辑
摘要: 39.组合总和 题目链接 文章讲解 视频讲解 class Solution { private: vector<int> combine; vector<vector<int>> result; int count = 0; public: vector<vector<int>> combinati 阅读全文
posted @ 2024-06-05 14:52 深蓝von 阅读(1) 评论(0) 推荐(0) 编辑
摘要: 组合总和III 题目链接 文章讲解 视频讲解 class Solution { public: vector<vector<int>> combinationSum3(int k, int n) { vector<int> path; vector<vector<int>> result; back 阅读全文
posted @ 2024-06-04 19:31 深蓝von 阅读(2) 评论(0) 推荐(0) 编辑
摘要: 回溯算法理论基础 文章讲解 视频讲解 回溯是递归的副产品,只要有回溯就会有递归 回溯的本质是琼剧,所以效率不高 回溯法可以解决的问题 组合问题 切割问题 子集问题 排列问题 棋盘问题 如何理解回溯 回溯算法的问题都可以抽象为树形结构 集合的大小就构成了书的快读,递归的深度就构成了树的深度 回溯法模板 阅读全文
posted @ 2024-06-04 12:31 深蓝von 阅读(2) 评论(0) 推荐(0) 编辑
摘要: 669.修剪二叉搜索树 题目链接 文章讲解 视频讲解 class Solution { public: TreeNode* trimBST(TreeNode* root, int low, int high) { if(root == nullptr) return nullptr; // 当前值小 阅读全文
posted @ 2024-06-03 21:35 深蓝von 阅读(1) 评论(0) 推荐(0) 编辑
摘要: 235.二叉搜索树的最近公共祖先 题目链接 文章讲解 视频讲解 思路:递归遍历二叉搜索树 如果当前值大于p和q的值,向左遍历 如果当前值小于p和q的值,向右遍历 否则说明当前值介于p和q之间,直接返回当前节点 class Solution { public: TreeNode* lowestComm 阅读全文
posted @ 2024-06-03 14:05 深蓝von 阅读(2) 评论(0) 推荐(0) 编辑
摘要: 530.二叉搜索树的最小绝对差 题目链接 文章讲解 视频讲解 关键词: 二叉搜索树 --> 中序遍历 关于递归的返回值 由于需要遍历整棵二叉树,所以返回值为void,如果不是遍历整棵二叉树,需要在得到结果时立即返回结果,此时返回值才不为空 怎样使用两个指针pre和cur使得pre始终指向cur的前一 阅读全文
posted @ 2024-06-02 22:40 深蓝von 阅读(1) 评论(0) 推荐(0) 编辑