- 博客(231)
- 资源 (2)
- 收藏
- 关注
原创 Leetcode 460 LFU缓存机制(最低频次最少使用)
LFU缓存机制,比LRU缓存更加复杂,具体为当需要移除key时,需要删除频次最低的key,而当频次最低key有多个时,删除最久未使用的key,
2022-07-03 01:27:11
347
原创 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
199
原创 316 & 1081. 去除重复字母 (单调栈)
// 如果不需要保持原有顺序的话,直接一个set就行了// 但需要保存顺序就是用单调栈了// 栈中元素单调, 每来一个字符c, // 判断是否栈中存在, 存在直接pass// 不存在, 就与栈中字符比较, 如果栈顶字符比c大,并且后续仍会出现// 就将栈顶元素弹出, 更新标记// 最后栈中所有字符就是最终答案, 逆序输出即可class Solution { public String smallestSubsequence(String s) { ArrayDequ.
2022-04-27 14:41:59
160
原创 1541. 平衡括号字符串的最少插入次数 栈
// "(" 进栈// "))" 两种情况: 栈为空, 添加一个'('计数+1 不为空直接弹出// ")" 两种情况:// 栈为空, 所以必须加个"()" 计数+2// 栈不为空, 必须加个')' 计数+1// 最后统计栈中'('的数量class Solution { public boolean match(char a, char b) { if (a == '(' && b == ')') return true; .
2022-04-27 12:08:01
150
原创 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
418
原创 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
353
原创 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
816
原创 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
167
原创 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
1119
原创 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
652
原创 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
2744
原创 Latex caption中使用\overrightarrow报错: Illegal parameter number in definition of \reserved@a
\caption{\overrightarrow{x}}会报错:Illegal parameter number in definition of \reserved@a解决方案:在\overrightarrow{x}前加\protect :\caption{\protect\overrightarrow{x}}
2021-07-27 15:39:30
2055
原创 MAC更新系统网络连接中断
MAC更新系统网络连接中断今天更新Catalina,可能挂了梯子,所以下载出问题,之后疯狂出错.只要删除/Library/updates/下,出问题的更新文件就好了但是要先将MAC系统的System Integrity Protection(系统完整性保护)关掉.具体步骤:开机时,按住command + R,进入恢复模式,出现macOS实用工具提示字样在上方工具栏"实用工具"这一项中...
2019-10-08 17:02:24
8916
原创 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
288
原创 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
181
原创 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
251
原创 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
261
原创 LeetCode 75 颜色分类 荷兰国旗问题
// 双下标,begin和end表示中间白色的开始和末尾.再对每个元素进行遍历,// 设为current.分情况讨论:// 当前是红色: 需要跟begin位置进行交换,begin++, current++;// 当前是白色: 保持不变,current++;// 当前是蓝色: 与end位置进行交换,end--,但此时current可能是0, 1, 2,所以保持不变// 开始想错了,三个...
2019-08-25 14:38:47
295
原创 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
257
原创 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
190
原创 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
259
原创 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
163
原创 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
173
原创 LeetCode 59 螺旋矩阵II
// LeetCode 59 螺旋矩阵II// 分情况套路,比n*m的简单,注意奇偶class Solution { public int[][] generateMatrix(int n) { int[][] res = new int[n][n]; int k = 0; int count = 1; while(...
2019-08-24 15:13:46
301
原创 LeetCode 56 合并区间
// LeetCode 56 合并区间// 二维排序,依次取出区间进行比较,O(nlogn))复杂度class Solution { public int[][] merge(int[][] intervals) { int n = intervals.length; if (n == 0) return new int[0][0]; ...
2019-08-23 16:31:33
203
原创 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
352
原创 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
309
原创 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
301
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人