LeetCode
TIMELIMITE
Time is not enough. I must hurry up !
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
leetcode 1233 前缀字典树
前缀字典树原创 2022-07-09 15:33:33 · 500 阅读 · 0 评论 -
leetcode895 最大频次栈 hashmap
leetcode895 最大频次栈, hashmap应用原创 2022-07-03 17:36:36 · 411 阅读 · 0 评论 -
Leetcode 460 LFU缓存机制(最低频次最少使用)
LFU缓存机制,比LRU缓存更加复杂,具体为当需要移除key时,需要删除频次最低的key,而当频次最低key有多个时,删除最久未使用的key,原创 2022-07-03 01:27:11 · 414 阅读 · 0 评论 -
Leetcode 380 随机获取的数据结构
使用数组+hashmap,O(1) 获取数据,O(1) 删除数据原创 2022-07-02 23:12:20 · 245 阅读 · 0 评论 -
Leetcode 146 缓存算法 hashmap+双向链表
Leetcode 146 缓存算法 hashmap+双向链表原创 2022-07-02 22:00:23 · 300 阅读 · 0 评论 -
Leetcode 703 数据流中的第k大元素 堆
数据流中的第k大元素 使用堆原创 2022-07-02 20:51:40 · 181 阅读 · 0 评论 -
Leetcode 295 数据流中的中位数
在数据流中寻找中位数原创 2022-07-02 20:30:53 · 188 阅读 · 0 评论 -
LeetCode 23 合并k个排序链表 优先队列堆优化
优先队列堆优化原创 2022-07-02 19:35:07 · 347 阅读 · 0 评论 -
LeetCode 51 N皇后 回溯
n皇后回溯法原创 2022-07-01 15:52:58 · 354 阅读 · 0 评论 -
leetcode 394 解压字符串 栈
栈的基本操作原创 2022-06-27 23:56:44 · 234 阅读 · 0 评论 -
1202. 交换字符串中的元素 并查集+排序
// 将同一个联通部分的字符拿出来, sort下// 然后逐个填入....class Solution { private int[] father; public int find(int x) { if (x == father[x]) return x; return father[x] = find(father[x]); } public void union(int u, int v) { .原创 2022-04-27 17:21:18 · 235 阅读 · 0 评论 -
316 & 1081. 去除重复字母 (单调栈)
// 如果不需要保持原有顺序的话,直接一个set就行了// 但需要保存顺序就是用单调栈了// 栈中元素单调, 每来一个字符c, // 判断是否栈中存在, 存在直接pass// 不存在, 就与栈中字符比较, 如果栈顶字符比c大,并且后续仍会出现// 就将栈顶元素弹出, 更新标记// 最后栈中所有字符就是最终答案, 逆序输出即可class Solution { public String smallestSubsequence(String s) { ArrayDequ.原创 2022-04-27 14:41:59 · 212 阅读 · 0 评论 -
1541. 平衡括号字符串的最少插入次数 栈
// "(" 进栈// "))" 两种情况: 栈为空, 添加一个'('计数+1 不为空直接弹出// ")" 两种情况:// 栈为空, 所以必须加个"()" 计数+2// 栈不为空, 必须加个')' 计数+1// 最后统计栈中'('的数量class Solution { public boolean match(char a, char b) { if (a == '(' && b == ')') return true; .原创 2022-04-27 12:08:01 · 191 阅读 · 0 评论 -
921. 使括号有效的最少添加 栈
栈的使用原创 2022-04-27 11:48:22 · 209 阅读 · 0 评论 -
leetcode 92 反转链表 II O(n)
// 从前往后遍历, 保留left前一个节点pre位置, 保留right后一个节点的位置q// 同时保留lef所在的位置rTail, 保留right所在位置p// 翻转left到right的链表// 把p接到pre后面,把q接到rTail后面// 特判pre是否为null, 此时head可能是q的位置// 细节特别多// 完事儿class Solution { public int n; public ListNode reverseBetween(ListNode h.原创 2022-04-17 16:27:04 · 455 阅读 · 0 评论 -
201 数字范围按位与 二进制转换
// 如果left和right不在同一个[2^i, 2^(i + 1)]范围内一定是0// 因为此时按照二进制分解left高位是0, 而区间内肯定有高位是1后面全0的数// 比如5, 9 左边left为0101, 区间内有8,其二进制形式为1000// 在同一个范围时, 看高位有多少个相同的1,累加即可// 更好的是不断去除末位的1class Solution {public: int rangeBitwiseAnd(int left, int right) { .原创 2022-04-10 23:22:03 · 397 阅读 · 0 评论 -
leetcode 300 最长递增子序列 动态规划+二分优化
// 经典dp// dp[i]表示以i为结尾的最长公共子序列长度// dp[i] = max(dp[j]) + 1 if (a[i] > a[j])// 否则dp[i] = 1// 优化版在后面//class Solution {//public:// int lengthOfLIS(vector<int>& nums) {// int n = nums.size();// vector<int> dp(n + 1.原创 2022-04-10 11:18:02 · 861 阅读 · 0 评论 -
leetcode 139 单词拆分 一维dp
// dp[i]表示1到i是否能拼成// dp[i] = dp[i] | dp[j] if (j, i)能找到单词匹配class Solution {public: bool wordBreak(string s, vector<string>& wordDict) { int n = s.length(); vector<bool> dp(n + 1); dp[0] = true; for (.原创 2022-04-10 10:18:58 · 203 阅读 · 0 评论 -
leetcode 91 解码方法 动态规划 递推
// 各种特殊情况处理// 选择从右往左,从左往右也是一样class Solution {public: int numDecodings(string s) { int n = s.length(); if (s[0] == '0') return 0; vector<int> dp(n); dp[n - 1] = 1; for (int i = n - 2; i >= 0; i--) {.原创 2022-04-10 01:42:22 · 1155 阅读 · 0 评论 -
leetcode 413 等差数列划分 动态规划 简单递推
// 递推, 每个数除了第一个,都可形成长度为2的序列// 如果差值和之前相同, 那么序列加一, 否则长度为2// 倒着找最长的, 每段长度L的序列数为(L - 2) * (L - 1) / 2class Solution {public: int numberOfArithmeticSlices(vector<int>& nums) { int n = nums.size(); vector<int> dp(n); .原创 2022-04-10 00:49:11 · 691 阅读 · 0 评论 -
45 跳跃游戏二 (贪心)
// 贪心 每次加一步的时候,找能走得最远的那一步class Solution {public: int jump(vector<int>& nums) { int n = nums.size(); if (nums[0] == 0) { return 0; } int far = 0; int cnt = 0; for (int i = 0; i &.原创 2022-04-09 23:43:01 · 2794 阅读 · 0 评论 -
// LeetCode 20 有效的括号 栈
// 经典栈的应用// 左括号,直接入栈,右括号判断是否匹配,最后// 判断栈是否为空,为空就表示合法,否则非法// 注意题目中提到了空串也合法class Solution { public boolean isValid(String s) { if (s == null || s.length() == 0) return true;...原创 2019-04-17 21:12:22 · 194 阅读 · 0 评论 -
LeetCode 75 颜色分类 荷兰国旗问题
// 双下标,begin和end表示中间白色的开始和末尾.再对每个元素进行遍历,// 设为current.分情况讨论:// 当前是红色: 需要跟begin位置进行交换,begin++, current++;// 当前是白色: 保持不变,current++;// 当前是蓝色: 与end位置进行交换,end--,但此时current可能是0, 1, 2,所以保持不变// 开始想错了,三个...原创 2019-08-25 14:38:47 · 334 阅读 · 0 评论 -
LeetCode 74 搜索二维矩阵
// 二分,当成一维数组,下标转换class Solution { public boolean searchMatrix(int[][] matrix, int target) { int n = matrix.length; if (n == 0) return false; int m = matrix[0].length; ...原创 2019-08-25 12:19:54 · 291 阅读 · 0 评论 -
LeetCode 48 翻转图像 水题
// LeetCode 48 翻转图像// 先转置然后再垂直翻转class Solution { public void rotate(int[][] matrix) { int n = matrix.length; int temp = 0; for (int i = 0;i < n;i ++){ fo...原创 2019-08-20 22:19:00 · 332 阅读 · 0 评论 -
LeetCode 66 加一
// 简易版的大数加法模拟class Solution { public int[] plusOne(int[] digits) { int c = 1; int n = digits.length; int temp; for (int i = n - 1; i >= 0 ;i --){ ...原创 2019-08-24 16:59:09 · 226 阅读 · 0 评论 -
LeetCode 78 子集
// 二进制枚举class Solution { public List<List<Integer>> subsets(int[] nums) { int n = nums.length; List<List<Integer>> res = new ArrayList<>(); ...原创 2019-08-25 15:00:24 · 296 阅读 · 0 评论 -
LeetCode 79 单词搜索
// 记录起点,然后dfsclass Solution { int[] dx = {-1 , 0, 1, 0}; int[] dy = {0, 1, 0, -1}; int n; int m; boolean[][] vis; public boolean exist(char[][] board, String word) { ...原创 2019-08-25 18:43:24 · 283 阅读 · 0 评论 -
LeetCode 80 删除排序数组中的重复项 II
// 记下重复的第二次class Solution { public int removeDuplicates(int[] nums) { int n = nums.length; if (n == 0) return 0; int count = 0; boolean flag = false; in...原创 2019-08-25 19:09:25 · 212 阅读 · 0 评论 -
LeetCode 81 搜索旋转排序数组 II
// 找出旋转点,二分class Solution { public boolean search(int[] nums, int target) { int sep = -1; int n = nums.length; for (int i = 0;i < n - 1; i++){ if (nums[i]...原创 2019-08-25 19:22:50 · 321 阅读 · 0 评论 -
LeetCode 54 螺旋矩阵
// LeetCode 54 螺旋矩阵// 分情况讨论,注意终止条件,奇偶统一,用上取整class Solution { public List<Integer> spiralOrder(int[][] matrix) { if (matrix.length == 0) return new ArrayList<>(); Li...原创 2019-08-22 20:29:40 · 336 阅读 · 0 评论 -
LeetCode 55 跳跃游戏
// LeetCode 55 跳跃游戏// 贪心地每次走最远class Solution { public boolean canJump(int[] nums) { int currentEnd = 0; int currentFar = 0; boolean res = false; for (int i =...原创 2019-08-22 20:47:15 · 396 阅读 · 0 评论 -
LeetCode 64 最小路径和
// dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + dp[i][j];// 注意初始化横竖累和class Solution { public int minPathSum(int[][] grid) { int n = grid.length; int m = grid[0].length; ...原创 2019-08-24 16:33:31 · 293 阅读 · 0 评论 -
LeetCode 63 不同路径II
// 跟62差不多 https://blog.youkuaiyun.com/TIMELIMITE/article/details/100053683// 就是对当前格子判断下,初始化的时候横竖碰到1就不能走了class Solution { public int uniquePathsWithObstacles(int[][] obstacleGrid) { int n = ...原创 2019-08-24 16:18:04 · 196 阅读 · 0 评论 -
LeetCode 62 不同路径
// 经典dp, dp[i][j] = dp[i - 1][j] + dp[i][j - 1];// 注意边界 dp[1][1..n] = dp[1...n][1] = 1;// 因为只能向右和向下走// 但是数据应该没这么强,试了下100,100都超long了...class Solution { public int uniquePaths(int m, int n) ...原创 2019-08-24 15:55:44 · 202 阅读 · 0 评论 -
LeetCode 31 下一个排列
// 之前觉得从后往前找到一个a[i] > a[i - 1]将其调换就行// 直到交了以后发现, 1 3 2 样例过不去// 看了下解法,才发现原来不是将a[i] 与 a[i - 1]交换// 而是要在a[i] ~ a[n]这部分找到刚好大于a[i- 1]的这个数index// 将a[index]与a[i - 1]交换,并将 i~n处变为升序class Solution {...原创 2019-04-28 22:13:02 · 196 阅读 · 0 评论 -
LeetCode 34 二分查找区间
//分别找最左边和最右边class Solution { public int[] searchRange(int[] nums, int target){ if (nums.length == 0) return new int[]{-1, -1}; int left = searchLeft(nums, target); i...原创 2019-05-03 11:29:52 · 487 阅读 · 0 评论 -
LeetCode 24 两两交换链表中的节点
// 利用P,Q两个指针两两分组操作,记得用Pre保存之前翻转好的链表末尾// 以方便将新翻转的链表连上class Solution { public ListNode swapPairs(ListNode head){ if (head == null) return null; if (head.next == null) return head...原创 2019-04-18 21:28:00 · 263 阅读 · 0 评论 -
LeetCode 23 合并K个排序链表
// 解法一: 采用归并的思路,两两合并// 解法二:小子只是想熟悉Java优先队列使用方法,但提交报错class Solution { public ListNode mergeKLists(ListNode[] lists) { if (lists == null) return null; if (lists.leng...原创 2019-04-18 20:57:05 · 286 阅读 · 0 评论 -
LeetCode 22 括号生成
// 简单递归,因为右括号个数只能比左括号剩余个树多才能放置// 换句话说,先放了多少个左括号,就可以放置多少个右括号class Solution { public List<String> res = new ArrayList<>(); public List<String> generateParenthesis(int n) {...原创 2019-04-18 19:18:38 · 278 阅读 · 0 评论
分享