- 博客(14)
- 资源 (5)
- 收藏
- 关注
原创 Leetcode|小根堆|23. 合并K个升序链表
文章目录1 两两链表合并(超时)2 将K个链表首个节点依次压入小根堆,然后逐个弹出1 两两链表合并(超时)class Solution {public: ListNode* merge2Lists(ListNode* l1, ListNode* l2) { auto prehead = new ListNode(); auto merge = prehead; while (l1 || l2) { if (!l1) {
2021-07-27 09:49:43 185
原创 Leetcode|有序数组合并|4. 寻找两个正序数组的中位数
1 有序数组合并class Solution {public: double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) { vector<int> merge; int size1 = nums1.size(); int size2 = nums2.size(); int sizem = siz..
2021-07-26 22:55:49 152
原创 Leetcode|索引置换|41. 缺失的第一个正数
腾讯秋招提前批AI Lab一面面试题1 原地数组索引置换[3, 4, -1, 1] => [1, -1, 3, 4],这样遍历到nums[1] != 2就返回缺失的2class Solution {public: int firstMissingPositive(vector<int>& nums) { int size = nums.size(); // 1.遇到与索引+1不同的数就置换,如[3,4,-1,1],其中nums[
2021-07-24 23:55:08 121
原创 Leetcode|BFS+DFS拓扑排序|210. 课程表 II
1 DFS拓扑排序class Solution {public: vector<vector<int>> edges; // 邻接矩阵 vector<int> visited; // 0-未搜索; 1-已访问; 2-已压栈 vector<int> res; // 最终结果 stack<int> stk; // 存放结果的栈,图的子节点先入栈,父节点.
2021-07-20 13:37:00 144
原创 Leetcode|BFS每层最后1个节点|199. 二叉树的右视图
1 BFS每层最后1个节点添加到解中思路: 利用 BFS 进行层次遍历,记录下每层的最后一个元素class Solution {public: vector<int> rightSideView(TreeNode* root) { if (!root) return {}; vector<int> res; queue<TreeNode*> q; q.push(root); .
2021-07-17 23:45:41 171 1
原创 Leetcode|找单链表中点+链表反转+链表合并|143. 重排链表
1 寻找单链表中点 + 链表反转 + 链表合并这道题是道综合题,把三个知识点串起来,非常适合复习链表处理的三个技巧【思路】:观察发现可以把链表后一半进行反转,然后当成两个链表的合并任务即可class Solution {public: void reorderList(ListNode* head) { if (!head) return; // 1.寻找链表中点(快慢指针) auto premid = findmid(head); .
2021-07-17 22:31:10 222
原创 Leetcode|固定四角从外围至内围|54. 螺旋矩阵
1 模拟class Solution {public: vector<int> spiralOrder(vector<vector<int>>& matrix) { vector<int> res; int rowlen = matrix.size(); // 行数 int collen = matrix[0].size(); // 列数 int left ..
2021-07-17 19:27:34 127
原创 Leetcode|双指针/快慢指针|19. 删除链表的倒数第 N 个结点
1 快慢指针class Solution {public: ListNode* removeNthFromEnd(ListNode* head, int n) { auto pre = new ListNode(); auto prehead = pre; pre->next = head; auto slow = head; auto fast = head; for (int i = 0;.
2021-07-17 15:08:44 88
原创 Leetcode|BFS+双端队列|103. 二叉树的锯齿形层序遍历
1 BFS + 双端队列/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(n.
2021-07-13 23:25:15 119
原创 Leetcode|sort+双指针|88. 合并两个有序数组
1 双指针class Solution {public: void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) { int idx1 = 0, idx2 = 0; vector<int> tmp; while (idx1 < m || idx2 < n) { if (idx1 == m.
2021-07-09 11:28:01 91
原创 Leetcode|7. 整数反转
1 直接求解class Solution {public: int reverse(int x) { long a = 0; while (x / 10 != 0 || x % 10 != 0) { // 比如从123中取出2,则a = 3 * 10 + 2 = 32 a = a * 10 + x % 10; x /= 10; // 保证在int范围内 .
2021-07-09 10:54:23 89
原创 Leetcode|短链表后补零|2. 两数相加
1 链表加和+考虑进位+短链表补零/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x), next(nullptr) {} * ListNode(int x, ListNode *next) ..
2021-07-09 10:30:08 142
原创 Leetcode|滑动窗口变种|3. 无重复字符的最长子串
1 滑动窗口变种class Solution {public: int lengthOfLongestSubstring(string s) { if (s.size() < 2) return s.size(); unordered_map<char, int> need; int left = 0, right = 0; int len = 0; while (right < s.siz.
2021-07-01 11:31:50 117
原创 Leetcode|滑动窗口|438. 找到字符串中所有字母异位词
1 滑动窗口class Solution {public: vector<int> findAnagrams(string s, string p) { if (s.empty()) return {}; unordered_map<char, int> need, window; for (auto& c : p) need[c]++; int left = 0, right = 0, vaild.
2021-07-01 09:27:44 109
Jclasslib_win64bit
2018-12-03
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人