摘要: 239. 滑动窗口最大值 这题利用单调队列,保证最大值在队头,并且保证队列塞入数大于某些数时排出小于他的数,维护有可能成为最大值的数。如3 1 下一次进来的是2 就需要变成 3 2。 “单调队列真是一种让人感到五味杂陈的数据结构,它的维护过程更是如此.....就拿此题来说,队头最大,往队尾方向单调. 阅读全文
posted @ 2023-01-10 15:07 芝士可乐 阅读(24) 评论(0) 推荐(0)
摘要: LeetCode20 有效的括号 https://leetcode.cn/problems/valid-parentheses/submissions/ 流程为遍历每一个字符并判断是否为左括号还是有括号,若为左括号则放入栈中,若为有括号则判断栈是否为空,若不为空则检查当前字符是否匹配栈顶元素,若匹配 阅读全文
posted @ 2023-01-07 17:41 芝士可乐 阅读(16) 评论(0) 推荐(0)
摘要: 232. 用栈实现队列 https://leetcode.cn/problems/implement-queue-using-stacks/ 利用两个栈来实现队列,一个先存储进去的,一个存储IN栈倒出来的元素,这样就可以获取到队首了 class MyQueue { public: stack<int 阅读全文
posted @ 2023-01-06 20:41 芝士可乐 阅读(17) 评论(0) 推荐(0)
摘要: 459重复的子字符串 https://leetcode.cn/problems/repeated-substring-pattern/ class Solution { public: int* getNext(string s){//创建next[i]为最长相等前后缀长度的next数组 int * 阅读全文
posted @ 2023-01-05 21:47 芝士可乐 阅读(16) 评论(0) 推荐(0)
摘要: 344 反转字符串 https://leetcode.cn/problems/reverse-string/ 双指针 class Solution { public: void reverseString(vector<char>& s) { int left=0;int right=s.size( 阅读全文
posted @ 2023-01-05 15:25 芝士可乐 阅读(16) 评论(0) 推荐(0)
摘要: 454. 四数相加 II https://leetcode.cn/problems/4sum-ii/ 采用哈希表法,能比暴力法减少时间复杂度,先用两个数之和做出哈希表,再用剩下两数之和寻找哈希表中的数 class Solution { public: int fourSumCount(vector< 阅读全文
posted @ 2023-01-04 12:02 芝士可乐 阅读(21) 评论(0) 推荐(0)
摘要: 当我们遇到了要快速判断一个元素是否出现集合里的时候,就要考虑哈希法 242.有效的字母异位词 https://leetcode.cn/problems/valid-anagram/ 由于哈希表只有26个元素,故采用数组做哈希表比较合适。 class Solution { public: bool i 阅读全文
posted @ 2023-01-02 21:22 芝士可乐 阅读(23) 评论(0) 推荐(0)
摘要: 24. 两两交换链表中的节点 https://leetcode.cn/problems/swap-nodes-in-pairs/ class Solution { public: ListNode* swapPairs(ListNode* head) { if(head==NULL||head->n 阅读全文
posted @ 2022-12-31 23:19 芝士可乐 阅读(39) 评论(0) 推荐(0)
摘要: 203.移除链表元素 https://leetcode.cn/problems/remove-linked-list-elements/ struct ListNode{ int val; ListNode* next; ListNode(){ val=0; next=NULL; } ListNod 阅读全文
posted @ 2022-12-31 13:13 芝士可乐 阅读(191) 评论(0) 推荐(0)
摘要: LeetCode 997. 有序数组的平方 https://leetcode.cn/problems/squares-of-a-sorted-array/ 暴力解法 int* sortedSquares(int* nums, int numsSize, int* returnSize){ *retu 阅读全文
posted @ 2022-12-29 22:23 芝士可乐 阅读(524) 评论(0) 推荐(0)