LeetCode合并两个有序数组(逆向双指针)
摘要:原题解 题目 解法 逆向双指针 算法数学验证 是先假设符合空间要求,化简公式得到的p2正好取值在[0, n],符合要求 代码 public void merge(int[] nums1, int m, int[] nums2, int n) { int a = m - 1, b = n - 1, c
阅读全文
posted @
2024-04-04 00:43
垂序葎草
阅读(13)
推荐(0) 编辑
LeetCode 146. LRU 缓存(hash/手写链表)
摘要:> 20230711第一次学习 20230713顺利通过 [原题解](https://leetcode.cn/problems/lru-cache/solutions/259678/lruhuan-cun-ji-zhi-by-leetcode-solution/) ###题目 
推荐(0) 编辑
LeetCode 152. 乘积最大子数组
摘要:20230426 顺利通过 原题解 ###题目 约束 ###题解 class Solution { public: int maxProduct(vector<int>& nums) { int maxF = nums[0], minF = nums[0], ans = nums[0]; for (
阅读全文
posted @
2023-04-26 11:56
垂序葎草
阅读(14)
推荐(0) 编辑
LeetCode 148. 排序链表
摘要:20230426 顺利通过 前置题目 21. 合并两个有序链表 原题解 ###题目 约束 ###题解 ####方法一 class Solution { public: ListNode* sortList(ListNode* head) { return sortList(head, nullptr
阅读全文
posted @
2023-04-25 00:36
垂序葎草
阅读(12)
推荐(0) 编辑
LeetCode 169. 多数元素(/hash sort 随机化 分治 Boyer-Moore 投票算法)
摘要:原题解 ###题目 约束 ###题解 ####方法一:哈希表 class Solution { public: int majorityElement(vector<int>& nums) { unordered_map<int, int> counts; int majority = 0, cnt
阅读全文
posted @
2023-03-24 03:08
垂序葎草
阅读(21)
推荐(0) 编辑
LeetCode 160. 相交链表()
摘要:20230321 顺利通过 20230411 顺利通过 原题解 ###题目 约束 ###题解 ####方法一 class Solution { public: ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { unor
阅读全文
posted @
2023-03-21 02:29
垂序葎草
阅读(9)
推荐(0) 编辑
LeetCode 142. 环形链表 II()
摘要:20230320 顺利通过 20230410 和前一题没啥区别 原题解 ###题目 约束 ###题解 ####解法一 class Solution { public: ListNode *detectCycle(ListNode *head) { unordered_set<ListNode *>
阅读全文
posted @
2023-03-19 03:03
垂序葎草
阅读(57)
推荐(0) 编辑
LeetCode 141. 环形链表(/)
摘要:20230319 顺利通过 20230410 毫无思路 原题解 ###题目 约束 ###题解 ####方法一 class Solution { public: bool hasCycle(ListNode *head) { unordered_set<ListNode*> seen; while (
阅读全文
posted @
2023-03-19 02:55
垂序葎草
阅读(295)
推荐(0) 编辑
LeetCode 139. 单词拆分(/dp)
摘要:20230317 顺利通过 20230319 顺利通过 20230408 思路没记住 原题解 ###题目 约束 ###题解 class Solution { public: bool wordBreak(string s, vector<string>& wordDict) { auto wordD
阅读全文
posted @
2023-03-17 02:29
垂序葎草
阅读(15)
推荐(0) 编辑
LeetCode 136. 只出现一次的数字(/思维 异或)
摘要:20230317 顺利通过 20230319 顺利通过 20230406 顺利通过 原题解 ###题目 约束 ###题解 class Solution { public: int singleNumber(vector<int>& nums) { int ret = 0; for (auto e:
阅读全文
posted @
2023-03-17 02:25
垂序葎草
阅读(14)
推荐(0) 编辑
LeetCode 128. 最长连续序列(/)
摘要:20230317 顺利通过 20230319 顺利通过 20230403 if(!hashset.count(h - 1)) 原题解 ###题目 约束 ###题解 class Solution { public: int longestConsecutive(vector<int>& nums) {
阅读全文
posted @
2023-03-16 02:47
垂序葎草
阅读(190)
推荐(0) 编辑
LeetCode 124. 二叉树中的最大路径和(/)
摘要:20230317 顺利通过 20230319 顺利通过 20230403 ans = max(ans, left + right + root->val); return max(left, right) + root->val; ans = -0x3f3f3f3f; 原题解 ###题目 约束 ##
阅读全文
posted @
2023-03-16 02:24
垂序葎草
阅读(189)
推荐(0) 编辑
LeetCode 121. 买卖股票的最佳时机(/)
摘要:20230316 顺利通过 20230317 顺利通过 20230319 顺利通过 20230403 顺利通过 原题解 ###题目 约束 ###题解 ####方法一 class Solution { public: int maxProfit(vector<int>& prices) { int n
阅读全文
posted @
2023-03-16 02:11
垂序葎草
阅读(10)
推荐(0) 编辑
LeetCode 114. 二叉树展开为链表(/)
摘要:20230317 顺利通过 20230319 顺利通过 20230403 顺利通过 原题解 ###题目 约束 ###题解 ####方法一 递归 class Solution { public: void flatten(TreeNode* root) { vector<TreeNode*> l; p
阅读全文
posted @
2023-03-15 21:56
垂序葎草
阅读(251)
推荐(0) 编辑
LeetCode 104. 二叉树的最大深度(/dfs)
摘要:20230312 顺利通过 20230315 顺利通过 20230403 顺利通过 原题解 ###题目 约束 ###题解 ####解法一 class Solution { public: int maxDepth(TreeNode* root) { if (root == nullptr) retu
阅读全文
posted @
2023-03-08 19:22
垂序葎草
阅读(12)
推荐(0) 编辑
LeetCode 102. 二叉树的层序遍历(/bfs)
摘要:20230403 !root ans.back() 原题解 题目 约束 ###题解 class Solution { public: vector<vector<int>> levelOrder(TreeNode* root) { vector <vector <int>> ret; if (!ro
阅读全文
posted @
2023-03-08 00:21
垂序葎草
阅读(16)
推荐(0) 编辑
queue
摘要:来自我以前找的整理在doc上面的知识,来源已经不可考究了。 queue 和 stack 有一些成员函数相似,但在一些情况下,工作方式有些不同: front():返回 queue 中第一个元素的引用。如果 queue 是常量,就返回一个常引用;如果 queue 为空,返回值是未定义的。 back():
阅读全文
posted @
2023-03-08 00:16
垂序葎草
阅读(160)
推荐(0) 编辑
LeetCode 101. 对称二叉树(/dfs)
摘要:20230312 顺利通过 20230315 顺利通过 20230403 顺利通过 原题解 ###题目 约束 ###题解 ####解法一 class Solution { public: bool check(TreeNode *p, TreeNode *q) { if (!p && !q) ret
阅读全文
posted @
2023-03-07 00:28
垂序葎草
阅读(13)
推荐(0) 编辑
LeetCode 98. 验证二叉搜索树(/dfs)
摘要:20230315 顺利通过 20230403 root == nullptr && LONG_MIN, LONG_MAX 原题解 ###题目 约束 ###题解 ####解法一 class Solution { public: bool helper(TreeNode* root, long long
阅读全文
posted @
2023-03-07 00:20
垂序葎草
阅读(12)
推荐(0) 编辑
LeetCode 96. 不同的二叉搜索树(/dp)
摘要:20230306 顺利通过 20230312 顺利通过 20230315 顺利通过 原题解 ###题目 约束 ###题解 ####方法一 class Solution { public: int numTrees(int n) { vector<int> G(n + 1, 0); G[0] = 1;
阅读全文
posted @
2023-03-05 22:20
垂序葎草
阅读(12)
推荐(0) 编辑