10 2022 档案
摘要:491. 递增子序列 class Solution { private: vector<vector<int>> result; vector<int> path; void backtracking(vector<int>& nums,int startIndex) { if (path.size
阅读全文
摘要:93. 复原 IP 地址 1 class Solution { 2 private: 3 vector<string> result;// 记录结果 4 // startIndex: 搜索的起始位置,pointNum:添加逗点的数量 5 void backtracking(string& s, in
阅读全文
摘要:39. 组合总和 1 // 版本一 2 class Solution { 3 private: 4 vector<vector<int>> result; 5 vector<int> path; 6 void backtracking(vector<int>& candidates, int tar
阅读全文
摘要:216. 组合总和 III 1 class Solution { 2 private: 3 vector<vector<int>> result; 4 vector<int> path; 5 //1、确定回溯函数返回值和参数 6 //n - 目标值, k - 集合数目, sum - 用来匹配的和,
阅读全文
摘要:77. 组合 解题步骤: 1、确定回溯函数参数及返回值; vector<vector<int>> result; vector<int> path; void backtracking (int n, int k, int startIndex) 2、确定回溯结束条件 单层路径大小为k if (pa
阅读全文
摘要:669. 修剪二叉搜索树 解题步骤: 1、确定函数返回值和参数; TreeNode* trimBST(TreeNode* root, int low, int high) 2、确定递归函数截止条件; if (root == nullptr) return nullptr; 3、确定单层递归逻辑; /
阅读全文
摘要:235. 二叉搜索树的最近公共祖先 1 class Solution { 2 private: 3 TreeNode* traversal(TreeNode* cur, TreeNode* p, TreeNode* q) { 4 if (cur == NULL) return cur; 5 // 中
阅读全文
摘要:530. 二叉搜索树的最小绝对差 解题步骤: 1、将二叉搜索树转化为有序数组; 2、按照重新排序后的数组进行遍历,获取最小的绝对值差。 1 class Solution { 2 private: 3 //将二叉搜索树转化为一个有序数组 4 vector<int> vec; 5 void traves
阅读全文
摘要:654. 最大二叉树 解题步骤: 1、确定递归函数的参数和返回值 TreeNode* constructMaximumBinaryTree(vector<int>& nums) 2、确定终止条件 1 TreeNode* node = new TreeNode(0); 2 if (nums.size(
阅读全文
摘要:513. 找树左下角的值 1 class Solution { 2 public: 3 int findBottomLeftValue(TreeNode* root) { 4 //创建result用于存储结果 5 int result = 0; 6 queue<TreeNode*> que; 7 i
阅读全文
摘要:110. 平衡二叉树 递归法: 1 class Solution { 2 public: 3 //递归三步走 4 //1、确定返回值和函数参数 5 int getHeight(TreeNode* node){ 6 //2、明确终止条件 7 if (node == nullptr) return 0;
阅读全文
摘要:104. 二叉树的最大深度 迭代法: 1 class Solution { 2 public: 3 int maxDepth(TreeNode* root) { 4 //创建变量用于存储深度 5 int ans = 0; 6 queue<TreeNode*> que; 7 if (root != n
阅读全文
摘要:102. 二叉树的层序遍历 1 class Solution { 2 public: 3 vector<vector<int>> levelOrder(TreeNode* root) { 4 //创建一个队列用于存储每一层的结点 5 queue<TreeNode*> que; 6 if (root
阅读全文
摘要:● 理论基础 1 //二叉树的定义 2 struct TreeNode { 3 int val; 4 TreeNode *left; 5 TreeNode *right; 6 TreeNode(int x) : val(x), left(NULL), right(NULL) {} 7 }; 递归算法
阅读全文
摘要:150. 逆波兰表达式求值 解题步骤: 第一步:创建一个栈; 第二步:遍历整个数组; 第三步:判断遍历到的元素是运算符还是数字; 数字:直接压入栈中 运算符:将栈顶两个元素做相应运算后再压入栈中 第四步:弹出栈顶元素。 1 class Solution { 2 public: 3 int evalR
阅读全文
摘要:232. 用栈实现队列 1 class MyQueue { 2 public: 3 4 //初始化入队栈和出队栈 5 stack<int> stIn; 6 stack<int> stOut; 7 8 MyQueue() { 9 10 } 11 12 //入队 13 void push(int x)
阅读全文
摘要:459. 重复的子字符串 1 class Solution { 2 public: 3 bool repeatedSubstringPattern(string s) { 4 string t = s + s; 5 //掐头去尾 6 t.erase(t.begin()); 7 t.erase(t.e
阅读全文
摘要:344. 反转字符串 1 class Solution { 2 public: 3 void reverseString(vector<char>& s) { 4 for (int i = 0, j = s.size() - 1; i < s.size() / 2; i++, j--){ 5 //交
阅读全文
浙公网安备 33010602011771号