![](https://img-blog.csdnimg.cn/20190927151026427.png?x-oss-process=image/resize,m_fixed,h_224,w_224)
Leetcode算法之路-Hard
Leetcode困难题记录
Love 6
腾讯WXG在职后台小鹅
展开
-
Leetcode 44. 通配符匹配(DAY 317)---- 后端面试题
文章目录原题题目代码实现(首刷自解)原题题目在这里插入图片描述代码实现(首刷自解)class Solution { public: bool Judge(vector& dp, const string& s, const string& p, int spos, int ppos) { if (p[ppos - 1] == '*') return ppos == 1 || dp[spos][ppos - 1] || spos && (dp[spos ...原创 2022-06-23 13:35:12 · 697 阅读 · 0 评论 -
Leetcode 815. 公交路线(DAY 312)---- 后端面试题
文章目录原题题目代码实现(首刷部分看解)原题题目在这里插入图片描述代码实现(首刷部分看解)class Solution { public: int numBusesToDestination(vector& routes, int source, int target) { if (source == target) return 0; unordered_map routemap; vector...原创 2022-06-18 23:19:07 · 229 阅读 · 0 评论 -
Leetcode 440. 字典序的第K小数字(DAY 306)---- 后端面试题
Leetcode 440. 字典序的第K小数字原创 2022-06-08 13:42:38 · 560 阅读 · 1 评论 -
Leetcode 135. 分发糖果(DAY 305)---- 后端面试题
分发糖果原创 2022-06-07 09:00:02 · 409 阅读 · 0 评论 -
Leetcode 224. 基本计算器(DAY 264)---- 后端面试题(双栈计算器 需要考虑边界)
文章目录原题题目代码实现(首刷自解)原题题目代码实现(首刷自解)class Solution {public: inline void cal(const char op,stack<int>& num_stack) { auto num1 = num_stack.top(); num_stack.pop(); auto num2 = num_stack.top(); num_stack.原创 2022-03-12 10:18:07 · 7455 阅读 · 0 评论 -
Leetcode 679. 24 点游戏(DAY 262)---- 后端面试题(绝对的回溯递归难题)
文章目录原题题目代码实现(首刷自解)原题题目代码实现(首刷自解)class Solution {public: static constexpr double dif = 1e-6; static constexpr double target = 24; bool backtracking(vector<double>& elems) { if(elems.size() == 0) return false;原创 2022-03-10 09:37:36 · 4269 阅读 · 0 评论 -
Leetcode 214. 最短回文串(DAY 241)---- 后端面试题
文章目录原题题目代码实现(首刷小部分看解 复习了一遍KMP 较为困难)原题题目代码实现(首刷小部分看解 复习了一遍KMP 较为困难)class Solution {public: void getnext(vector<int>& next,string& str) { int nextpos = 1,pos = 0,size = str.size(); while(nextpos < size)原创 2022-02-12 12:23:00 · 1164 阅读 · 0 评论 -
Leetcode 1095. 山脉数组中查找目标值(DAY 234)---- 后端面试题
文章目录原题题目代码实现(首刷绝大部分看解 先求峰顶 再按照二分查找 右半边需要翻转查找)原题题目代码实现(首刷绝大部分看解 先求峰顶 再按照二分查找 右半边需要翻转查找)/** * // This is the MountainArray's API interface. * // You should not implement it, or speculate about its implementation * class MountainArray { * public原创 2022-01-30 13:20:16 · 642 阅读 · 0 评论 -
Leetcode 295. 数据流的中位数(DAY 227)---- 后端面试题
文章目录原题题目代码实现(首刷自解)原题题目代码实现(首刷自解)class MedianFinder {public: priority_queue<int,vector<int>,greater<int>> minheap; priority_queue<int,vector<int>,less<int>> maxheap; MedianFinder() {}原创 2021-12-18 12:59:07 · 141 阅读 · 0 评论 -
Leetcode 37. 解数独(DAY 220)---- 后端面试题
文章目录原题题目代码实现(首刷自解)原题题目代码实现(首刷自解)class Solution {public: bool backtracking(vector<vector<char>>& board,vector<vector<bool>>& part_visit,vector<vector<bool>>& row_visit,vector<vector<bool>原创 2021-12-09 17:11:29 · 90 阅读 · 0 评论 -
Leetcode 25. K 个一组翻转链表(DAY 199)
文章目录原题题目代码实现(首刷自解 DAY 199 C++)原题题目代码实现(首刷自解 DAY 199 C++)/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x), next(nullptr原创 2021-11-07 14:23:58 · 140 阅读 · 0 评论 -
Leetcode 460. LFU 缓存(DAY 197)---- LFU机制
文章目录原题题目代码实现(首刷自解 熟悉LFU机制 超高时间复杂度)代码实现(首刷 双哈希表 LFU机制 C++)原题题目代码实现(首刷自解 熟悉LFU机制 超高时间复杂度)class LFUCache {public: int size; int capacity; set<int> set{}; unordered_map<int,int> cnt; unordered_map<int,list<int>原创 2021-11-05 15:03:51 · 138 阅读 · 0 评论 -
Leetcode 315. 计算右侧小于当前元素的个数(DAY 194)---- 归并算法学习期
文章目录原题题目代码实现(首刷自解 归并难题)原题题目代码实现(首刷自解 归并难题)class Solution {public: void merge(vector<int>& nums,vector<int>& tmp,vector<int>& precount,vector<int>& count,vector<int>& nowpos,vector<int>&am原创 2021-11-01 11:09:48 · 110 阅读 · 0 评论 -
Leetcode 327. 区间和的个数(DAY 193)---- 归并算法学习期
文章目录原题题目代码实现(首刷 归并算法解决)原题题目代码实现(首刷 归并算法解决)class Solution {public: int ret; void merge(vector<long>& nums,vector<long>& tmp,int lower,int upper,int lo,int hi) { int l = lower,mid = (lower + (upper - lower) /原创 2021-10-31 11:21:57 · 84 阅读 · 0 评论 -
Leetcode 剑指 Offer 51. 数组中的逆序对(DAY 193)---- 归并算法学习期
文章目录原题题目代码实现(首刷归并算法 解决逆序对)原题题目代码实现(首刷归并算法 解决逆序对)class Solution {public: int ret = 0; void merge(vector<int>& nums,vector<int>& tmp,int left,int right) { int l = left,mid = (left + (right - left) / 2),r = mid原创 2021-10-31 09:38:13 · 141 阅读 · 1 评论 -
Leetcode 862. 和至少为 K 的最短子数组(DAY 192)---- 滑动窗口好题(恢复算法状态中)
文章目录原题题目代码实现(首刷自解 968ms)代码实现(首刷 大部分看解 滑动窗口)原题题目代码实现(首刷自解 968ms)class Solution {public: int shortestSubarray(vector<int>& nums, int k) { deque<long> deque; unordered_map<long,int> map; int ret = IN原创 2021-10-29 14:34:08 · 120 阅读 · 0 评论 -
Leetcode 887. 鸡蛋掉落(DAY 192)---- 最经典的DP+二分问题
文章目录原题题目代码实现(首刷 学习经典dp k*n^2 TLE 64 / 121)代码实现(首刷二分优化 此题仍需多次消化 - -)原题题目代码实现(首刷 学习经典dp k*n^2 TLE 64 / 121)class Solution {public: int superEggDrop(int k, int n) { vector<vector<int>> dp(k+1,vector<int>(n+1,0));原创 2021-10-29 10:57:01 · 110 阅读 · 0 评论 -
Leetcode 363. 矩形区域不超过 K 的最大数值和(DAY 191)
文章目录原题题目代码实现(首刷自解 n^4超高时间复杂度)代码实现(首刷优化原题题目代码实现(首刷自解 n^4超高时间复杂度)class Solution {public: int maxSumSubmatrix(vector<vector<int>>& matrix, int k) { int ret = INT_MIN; vector<vector<int>> row_cal(matrix.原创 2021-10-21 13:19:20 · 84 阅读 · 0 评论 -
Leetcode 1713. 得到子序列的最少操作次数(DAY 191)
文章目录原题题目代码实现(首刷dp超时 TLE 76 / 82)代码实现(首刷自解优化 二分 + 贪心优化)原题题目代码实现(首刷dp超时 TLE 76 / 82)class Solution {public: int minOperations(vector<int>& target, vector<int>& arr) { int last_pos = -1; unordered_map<int,in原创 2021-10-18 11:06:03 · 74 阅读 · 0 评论 -
Leetcode 668. 乘法表中第k小的数(DAY 190)---- 二分查找学习期
文章目录原题题目代码实现(首刷超时 TLE 优先队列 59/70)代码实现(首刷 看了思路 二分优化)原题题目代码实现(首刷超时 TLE 优先队列 59/70)class Solution {public: int findKthNumber(int m, int n, int k) { struct node { int val,x,y; node(int val,int x,int y):val(v原创 2021-10-15 11:19:53 · 73 阅读 · 0 评论 -
Leetcode 154. 寻找旋转排序数组中的最小值 II(DAY 190)---- 二分查找学习期
文章目录原题题目代码实现(首刷自解)原题题目代码实现(首刷自解)class Solution {public: int visit(vector<int>& nums,int left,int right) { if(nums[left] < nums[right]) return nums[left]; if(left == right) return nums[left]; int mi原创 2021-10-15 09:28:58 · 138 阅读 · 0 评论 -
Leetcode 149. 直线上最多的点数(DAY 172)---- LeetCode 精选 TOP 面试题
文章目录原题题目代码实现(首刷自解)原题题目代码实现(首刷自解)class Solution {public: int maxPoints(vector<vector<int>>& points) { int ret = 0; for(int i=0;i<points.size();++i) { ret = max(ret,1); unordered原创 2021-09-16 09:21:59 · 112 阅读 · 0 评论 -
Leetcode 41. 缺失的第一个正数(DAY 171)---- LeetCode 精选 TOP 面试题
文章目录原题题目代码实现(首刷小部分自解 - -)原题题目代码实现(首刷小部分自解 - -)class Solution {public: int firstMissingPositive(vector<int>& nums) { for(int i=0;i<nums.size();++i) { while(nums[i] >= 1 && nums[i] <= nums.s原创 2021-09-15 11:55:55 · 154 阅读 · 0 评论 -
Leetcode 127. 单词接龙(DAY 169)---- LeetCode 精选 TOP 面试题
文章目录原题题目代码实现(首刷超时 Dijkstra算法)代码实现(首刷自解 C++ 600ms)代码实现(首刷优化 vector改queue 256ms)原题题目代码实现(首刷超时 Dijkstra算法)class Solution {public: int ladderLength(string beginWord, string endWord, vector<string>& wordList) { wordList.emplace_ba原创 2021-09-13 11:05:37 · 154 阅读 · 0 评论 -
Leetcode 329. 矩阵中的最长递增路径(DAY 169)---- LeetCode 精选 TOP 面试题
文章目录原题题目代码实现(首刷超时 TLE 135/138)代码实现(首刷自解 回溯法DFS+记忆化)原题题目代码实现(首刷超时 TLE 135/138)class Solution {public: int ret; void backtracking(vector<vector<int>>& matrix,vector<vector<int>>& visit,int x,int y,int step,int原创 2021-09-13 08:51:22 · 166 阅读 · 0 评论 -
Leetcode 212. 单词搜索 II(DAY 114) ---- 回溯算法学习期
文章目录原题题目代码实现(首刷TLE 39\40)代码实现(首刷自解优化 双超80)原题题目代码实现(首刷TLE 39\40)class Solution {public: bool backtracking(const vector<vector<char>>& board,vector<vector<bool>>& visit,vector<string>& ret,const string&原创 2021-05-16 17:56:40 · 119 阅读 · 0 评论 -
Leetcode 51. N 皇后(DAY 113) ---- 回溯算法学习期(准备星期一高数期中 im genius 2天速成)
原题题目代码实现(首刷自解)class Solution {public: void backtracking(vector<vector<string>>& ret,vector<string>& temp,vector<int>& pos,const string& str,int visit,int line,int n) { if(line == n) {原创 2021-05-15 21:18:05 · 321 阅读 · 0 评论 -
Leetcode 980. 不同路径 III(DAY 113) ---- 回溯算法学习期
原题题目代码实现(首刷自解)class Solution {public: void backtracking(const vector<vector<int>>& grid,vector<vector<bool>>& visit,int nowx,int nowy,int count,int tempcount,int& ret) { if(visit[nowx][nowy] || grid原创 2021-05-15 21:58:09 · 117 阅读 · 0 评论 -
Leetcode 10. 正则表达式匹配(DAY 103) ---- Leetcode Hot 100(唯一真蝶回归)
原题题目代码实现(首刷自解)class Solution {public: bool isMatch(string s, string p) { vector<bool> judge; string temp; for(const auto& chr:p) //前置初始化 { if(chr != '*') { judge.em原创 2021-05-05 13:02:23 · 282 阅读 · 0 评论 -
Leetcode 301. 删除无效的括号(DAY 102) ---- Leetcode Hot 100
原题题目代码实现(首刷自解 128ms 未优化 之后有时间再优化优化 感觉面试想出来优化的也不是很多 ????一下 二刷必优化)class Solution {public: int mindel = INT_MAX; void backtracking(unordered_set<string>& set,vector<string>& ret,const string& s,string& temp,int left,in原创 2021-05-04 13:31:21 · 219 阅读 · 2 评论 -
Leetcode 32. 最长有效括号(DAY 102) ---- Leetcode Hot 100
原题题目代码实现(首刷自解)class Solution {public: int longestValidParentheses(string s) { stack<int> intstack,tempstack; int ret = 0,temp = 0,charstack = 0; for(const auto& chr:s) { if(chr == '(')原创 2021-05-04 12:23:31 · 206 阅读 · 0 评论 -
Leetcode 4. 寻找两个正序数组的中位数(DAY 102) ---- Leetcode Hot 100(恢复状态中)
原题题目代码实现(首刷自解)class Solution {public: double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) { double ret = 0; int size1 = nums1.size(),size2 = nums2.size(),times = (size1+size2)/2,flag = 0,count原创 2021-05-04 11:31:27 · 205 阅读 · 0 评论 -
Leetcode 84. 柱状图中最大的矩形(DAY 101) ---- Leetcode Hot 100
原题题目代码实现(首刷TLE 88/96)class Solution {public: int largestRectangleArea(vector<int>& heights) { int size = heights.size(),ret = 0; for(int i=0;i<size;++i) { ret = max(heights[i],ret); int原创 2021-05-03 18:26:35 · 221 阅读 · 0 评论 -
Leetcode 76. 最小覆盖子串(DAY 101) ---- Leetcode Hot 100
原题题目代码实现(首刷自解)class Solution {public: string minWindow(string s, string t) { unordered_map<char,int> map; unordered_multiset<char> multiset; unordered_set<char> set; for(const auto& ch:t)原创 2021-05-03 12:45:45 · 196 阅读 · 0 评论 -
Leetcode 124. 二叉树中的最大路径和(DAY 100) ---- Leetcode Hot 100
原题题目代码实现(首刷自解)/** * 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), rig原创 2021-04-29 12:52:10 · 230 阅读 · 0 评论 -
Leetcode 23. 合并K个升序链表(DAY 99) ---- Leetcode Hot 100
文章目录原题题目代码实现(首刷自解)代码实现(二刷评论区简洁代码 priority_queue lambda定义)原题题目代码实现(首刷自解)/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x),原创 2021-04-28 15:35:03 · 243 阅读 · 0 评论 -
Leetcode 239. 滑动窗口最大值(DAY 100) ---- Leetcode Hot 100
原题题目代码实现(首刷自解 单调栈344ms 超百分之25)class Solution {public: vector<int> maxSlidingWindow(vector<int>& nums, int k) { deque<int> s; vector<int> ret; int size = nums.size(); for(int i=0;i<size原创 2021-04-29 12:28:05 · 230 阅读 · 0 评论 -
Leetcode 128. 最长连续序列(DAY 99)---- Leetcode Hot 100
原题题目代码实现(首刷稍微瞄了一眼点播 做出来的)class Solution {public: int longestConsecutive(vector<int>& nums) { int ret = 0; if(nums.empty()) return ret; unordered_map<int,int> m; for(int i=0;i<nums.size();++i)原创 2021-04-28 16:36:52 · 206 阅读 · 0 评论 -
Leetcode 5729. 求出 MK 平均值(DAY 82) ---- 周赛题目
原题题目代码实现(首刷小部分看解绝大部分自解)class MKAverage {public: int m1; int k1; list<int> l; multiset<int> low,med,high; MKAverage(int m, int k) { m1 = m; k1 = k; } void addElement(int num) { l.push原创 2021-04-11 17:14:32 · 118 阅读 · 0 评论 -
Leetcode 188. 买卖股票的最佳时机 IV(DAY 81) ---- 动态规划学习期(dp困难终拿下一题)
原题题目代码实现(首刷自解)class Solution {public: int maxProfit(int k, vector<int>& prices) { int size = prices.size(),ret = 0; if(!size || !k) return 0; vector<vector<int>> dp(k+1,vector<int>(2,0));原创 2021-04-10 17:46:41 · 126 阅读 · 0 评论