随笔分类 - LeetCode
摘要:1332. 删除回文子序列 Solution 思路: 回文子序列 所以最多两次,如果一开始就是回文串的话 就是一次。 class Solution { public int removePalindromeSub(String s) { int i =0, j = s.length() - 1; w
阅读全文
摘要:2029. 石子游戏 IX Solution 自己思路: 一开始也是分类讨论了,不过是从价值是3的倍数的数字的个数和总和是否是3的倍数来讨论的。 正确思路:首先3的倍数的数字就是双方交替的。但是由于取完也会是Bob获胜,所以这里要讨论3的倍数cnt0的个数的奇偶性。同时要对非3的倍数细分,分为余数为
阅读全文
摘要:219. 存在重复元素 II Solution 思路1: 重复元素 然后就用HashMap不断更新最新出现的位置 与k值判断即可。 class Solution { public boolean containsNearbyDuplicate(int[] nums, int k) { Map<Int
阅读全文
摘要:539. 最小时间差 Solution 思路: 将所有时间字符串排序后 最小时间只可能是 前后相邻时间 和 首位时间。1440可以剪枝处理,因为24*60。 class Solution { public int findMinDifference(List<String> timePoints)
阅读全文
摘要:传送门 Solution 首先找规律可以看出来每个位数$digit$的数字位数之和为$sum_{digit}=digit*10^{digit-1}*9$,所以可以处理出第$n$位所在的数字$value$,根据数字位数之和与$n$的关系,可以得出$value$的位数$digit$,所以可以计算出$di
阅读全文
摘要:传送门 Solution 思路1: 滑动窗口。维护窗口内字符的数量,判断与$p$字符串数量是否相等。复杂度$O((sLen-pLen)*26)$ class Solution { public List<Integer> findAnagrams(String s, String p) { int
阅读全文
摘要:传送门 Solution 思路1: 暴力 class Solution { public int[] nextGreaterElement(int[] nums1, int[] nums2) { int[] ans = new int[nums1.length]; int cur = 0; for
阅读全文
摘要:传送门 Solution 思路1: 暴力搜索 class Solution { public boolean searchMatrix(int[][] matrix, int target) { for (int[] row: matrix) { for (int x : row) { if (x
阅读全文

浙公网安备 33010602011771号