01 2023 档案
摘要:class Solution { public: TreeNode* trimBST(TreeNode* root, int low, int high) { if (root == nullptr ) return nullptr; if (root->val < low) { TreeNode*
阅读全文
摘要:235. 二叉搜索树的最近公共祖先 class Solution { public: TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {if(root==NULL)return NULL; if((ro
阅读全文
摘要:530. 二叉搜索树的最小绝对差 遇到在二叉搜索树上求什么最值,求差值之类的,都要思考一下二叉搜索树可是有序的,要利用好这一特点。 class Solution { public: vector<int>vec; void getvec(TreeNode* root){ if(root==NULL)
阅读全文
摘要:654. 最大二叉树 class Solution { public: TreeNode* constructMaximumBinaryTree(vector<int>& nums) { if(nums.size()==1){ return new TreeNode(nums[0]); } int
阅读全文
摘要:513. 找树左下角的值 下面运用层序遍历法比较简单,当遍历到一层时设立一个值去不断覆盖一层的队头,即最左边元素 class Solution { public: int findBottomLeftValue(TreeNode* root) { int leftnum; queue<TreeNod
阅读全文
摘要:110. 平衡二叉树 注意概念 二叉树节点的深度:指从根节点到该节点的最长简单路径边的条数。 二叉树节点的高度:指从该节点到叶子节点的最长简单路径边的条数。 class Solution { public: int getHeight(TreeNode* root){ if(root==NULL)r
阅读全文
摘要:104. 二叉树的最大深度 class Solution { public: int maxDepth(TreeNode* root) { if(root==NULL)return 0; return 1+max(maxDepth(root->left),maxDepth(root->right))
阅读全文
摘要:102. 二叉树的层序遍历 class Solution { public: vector<vector<int>> levelOrder(TreeNode* root) { vector<vector<int>>result; queue<TreeNode*>qe; if(root!=nullpt
阅读全文
摘要:144. 二叉树的前序遍历 class Solution { public: vector<int> v; vector<int> preorderTraversal(TreeNode* root) { if(root==NULL)return v; v.push_back(root->val);
阅读全文
摘要:239. 滑动窗口最大值 这题利用单调队列,保证最大值在队头,并且保证队列塞入数大于某些数时排出小于他的数,维护有可能成为最大值的数。如3 1 下一次进来的是2 就需要变成 3 2。 “单调队列真是一种让人感到五味杂陈的数据结构,它的维护过程更是如此.....就拿此题来说,队头最大,往队尾方向单调.
阅读全文
摘要:LeetCode20 有效的括号 https://leetcode.cn/problems/valid-parentheses/submissions/ 流程为遍历每一个字符并判断是否为左括号还是有括号,若为左括号则放入栈中,若为有括号则判断栈是否为空,若不为空则检查当前字符是否匹配栈顶元素,若匹配
阅读全文
摘要:232. 用栈实现队列 https://leetcode.cn/problems/implement-queue-using-stacks/ 利用两个栈来实现队列,一个先存储进去的,一个存储IN栈倒出来的元素,这样就可以获取到队首了 class MyQueue { public: stack<int
阅读全文
摘要:459重复的子字符串 https://leetcode.cn/problems/repeated-substring-pattern/ class Solution { public: int* getNext(string s){//创建next[i]为最长相等前后缀长度的next数组 int *
阅读全文
摘要:344 反转字符串 https://leetcode.cn/problems/reverse-string/ 双指针 class Solution { public: void reverseString(vector<char>& s) { int left=0;int right=s.size(
阅读全文
摘要:454. 四数相加 II https://leetcode.cn/problems/4sum-ii/ 采用哈希表法,能比暴力法减少时间复杂度,先用两个数之和做出哈希表,再用剩下两数之和寻找哈希表中的数 class Solution { public: int fourSumCount(vector<
阅读全文
摘要:当我们遇到了要快速判断一个元素是否出现集合里的时候,就要考虑哈希法 242.有效的字母异位词 https://leetcode.cn/problems/valid-anagram/ 由于哈希表只有26个元素,故采用数组做哈希表比较合适。 class Solution { public: bool i
阅读全文
浙公网安备 33010602011771号