Leetcode
文章平均质量分 51
金石可镂!金石可镂!
samarua
while "live" : print("Loli")
展开
-
【LeetCode】Sama的个人记录_76
在遍历过程中同时维护"最小值"和"第二最小值"即可实现O(1)的空间与O(n)的时间class Solution { public boolean increasingTriplet(int[] nums) { int min = Integer.MAX_VALUE; int mid = Integer.MAX_VALUE; for (int num : nums) { if (num <= min) { .原创 2022-01-17 16:32:00 · 945 阅读 · 1 评论 -
【LeetCode】Sama的个人记录_75
for循环的哈希降维问题经典的for循环哈希降维:a+b=sum --> b=sum-a(sum固定,循环a,哈希寻找b)本题的for循环哈希降维:a+b+c=d --> a+b=d-c(手动固定bc,循环d,哈希寻找a)一个细节是:逆序遍历b的话,每次只多了一种c的情况(b+1),因此c的位置不需要单独遍历class Solution { public int countQuadruplets(int[] nums) { int res = 0; .原创 2022-01-17 16:31:45 · 833 阅读 · 0 评论 -
【LeetCode】Sama的个人记录_74
一共有三种正确的大写用法:1)全小写(apple)2)全大写(APPLE)3)首字母大写(Apple)配合使用toLowerCase()、toUpperCase()、equals(),非常简单class Solution { public boolean detectCapitalUse(String word) { // 全小写 if (word.toLowerCase().equals(word)) { return true.原创 2021-12-21 22:23:15 · 2067 阅读 · 0 评论 -
【LeetCode】Sama的个人记录_73
一个小技巧是:用一个长度为26的字符串作为哈希表("23321222122233111121131313")。class Solution { public String[] findWords(String[] words) { // 字符串即哈希表 String rowIndex = "23321222122233111121131313"; List<String> resList = new ArrayList<>();.原创 2021-11-06 09:34:27 · 146 阅读 · 1 评论 -
【LeetCode】Sama的个人记录_72
不难想到,通过与"111...11111"异或即可得到所谓补数。唯一麻烦的是,我们需要构造出这个"111...11111"。class Solution { public int findComplement(int num) { int tmp = num; int xor = 0; while (tmp > 0) { xor = (xor << 1) | 1; tmp.原创 2021-10-26 15:40:51 · 173 阅读 · 2 评论 -
【LeetCode】Sama的个人记录_71
优雅的字符串动态规划。class Solution { public int minDistance(String word1, String word2) { int len1 = word1.length(); int len2 = word2.length(); // dp[i][j]的含义是,word1和word2转化为相同需要的删除操作次数 int[][] dp = new int[len1 + 1][len2 + 1].原创 2021-10-09 15:10:49 · 138 阅读 · 0 评论 -
【LeetCode】Sama的个人记录_70
一个朴素的思想是,对所有行、所有列、所有3x3块,使用HashSet检测重复。优化的措施是:不使用HashSet,直接使用记录数组即可,甚至可以进一步优化为位运算一个小技巧是:i / 3 * 3 + j / 3 可以计算出3x3块的编号,所以对数独进行一次遍历即可class Solution { public boolean isValidSudoku(char[][] board) { // 某行中,某位数字是否已经被摆放 boolean[][] row .原创 2021-09-25 21:19:21 · 125 阅读 · 0 评论 -
【LeetCode】Sama的个人记录_69
非常简单直观:前缀和+二分查找class Solution { public int chalkReplacer(int[] chalk, int k) { int len = chalk.length; // step1:前缀和 if (chalk[0] > k) { return 0; } for (int i = 1; i < len; i++) { .原创 2021-09-25 21:18:56 · 133 阅读 · 0 评论 -
【LeetCode】Sama的个人记录_68
经典的"差分数组"+"前缀和"class Solution { public int[] corpFlightBookings(int[][] bookings, int n) { // 差分数组 int[] diff = new int[n + 1]; for (int[] booking : bookings) { diff[booking[0] - 1] += booking[2]; diff[bo.原创 2021-09-08 11:39:36 · 143 阅读 · 0 评论 -
【LeetCode】Sama的个人记录_67
非常简单,分别用两个变量记录"累计缺勤"和"连续迟到"的天数即可。class Solution { public boolean checkRecord(String s) { int abstCnt = 0; int lateCnt = 0; for (char c : s.toCharArray()) { abstCnt = abstCnt + (c == 'A' ? 1 : 0); lateC.原创 2021-08-30 10:16:59 · 145 阅读 · 0 评论 -
【LeetCode】Sama的个人记录_66
先明确两点:1) 如果不按"之"字排列,那么就父子结点的关系就是father=son/22) "之"字排列的本质是,隔行进行对称变换我们的思路就是,"先不按之字排列得到序列,然后隔行对其进行修正"另外需要注意一个细节,修正时还要考虑序列长度的奇偶,关于这点可以找几个例子写写看举个例子,对于label=27:先不按之字排列得到序列:[1, 3, 6, 13, 27]然后隔行对其进行修正:[1, 2, 6, 10, 27]class Solution { public List&.原创 2021-08-21 18:27:03 · 141 阅读 · 0 评论 -
【LeetCode】Sama的个人记录_65
关键在于"哈希表"的使用。下面两种解法(迭代/递归)的写法都非常优美 >_<class Solution { public Node copyRandomList(Node head) { if (head == null) { return null; } Map<Node, Node> map = new HashMap<>(); // 先复制节点 .原创 2021-07-28 22:46:03 · 98 阅读 · 0 评论 -
【LeetCode】Sama的个人记录_64
二分法找边界问题。class Solution { public int search(int[] nums, int target) { if (nums.length == 0) { return 0; } int borderL = serchLeftBorder(nums, target); int borderR = serchRightBorder(nums, target); .原创 2021-07-28 22:41:54 · 75 阅读 · 0 评论 -
【LeetCode】Sama的个人记录_63
看似是个加符号表达式问题...其实不是,而且非常简单!我们把加正号和加负号的数字看成两个集合,分别用sum(+)和sum(-)表示,则:sum(+) - sum(-) = targetsum(+) - sum(-) + sum(+) + sum(-) = targrt + sum(+) + sum(-)2 * sum(+) = target + sum最终,转化为了一个0/1背包问题:nums[]有几种和为(target+sum)/2的子集?class Solution { pu.原创 2021-07-17 14:46:40 · 116 阅读 · 1 评论 -
【LeetCode】Sama的个人记录_62
升维的区间DP,算不上困难。记住区间DP的特殊填表顺序即可。class Solution { public int strangePrinter(String s) { char[] arr = s.toCharArray(); int len = s.length(); // dp[i][j]的含义是打印[i,j]需要的最小次数 int[][] dp = new int[len][len]; // 初始化 .原创 2021-07-17 14:45:57 · 98 阅读 · 1 评论 -
【LeetCode】Sama的个人记录_61
简单题目,但想要写的优雅并不简单。本题的核心思路是"在DFS时将父结点作为参数暂存"。class Solution { private TreeNode xParent; private TreeNode yParent; private int xDepth; private int yDepth; private void DFS(TreeNode node, TreeNode parent, int depth, int x, int y) { .原创 2021-05-21 16:13:38 · 87 阅读 · 0 评论 -
【LeetCode】Sama的个人记录_60
差分法。差分法就是不记录当前具体的值,而是记录值的变化量。出生/死亡人数、上/下公交车,都是这类题目。class Solution { public int maximumPopulation(int[][] logs) { // 差分法:arr记录人口变化量 int[] arr = new int[101]; for (int[] log : logs) { arr[log[0] - 1950]++; .原创 2021-05-12 10:00:51 · 94 阅读 · 0 评论 -
【LeetCode】Sama的个人记录_59
这道题目的关键点在于:如果我拿了一个数 n ,那么我必须拿走所有的 n ,放弃所有的 n-1 和 n+1 。这和“打家劫舍”系列问题有着异曲同工之妙,只不过打家劫舍是位置上的关系(偷了位置n,位置n-1和n+1就会发出警报),而本题是数字上的关系(拿走了数字n,就不能拿数字n-1和n+1)。所以,我们只需要多进行一次词频的统计就行。class Solution { public int deleteAndEarn(int[] nums) { // 用数组统计数字的频次 .原创 2021-05-12 09:59:26 · 61 阅读 · 0 评论 -
【LeetCode】Sama的个人记录_58
在这里插入代码片原创 2021-04-30 16:16:43 · 70 阅读 · 0 评论 -
【LeetCode】Sama的个人记录_57
下面这种KMP写法比较优雅,而且好背。可配合题解食用:https://leetcode-cn.com/problems/implement-strstr/solution/dai-ma-sui-xiang-lu-kmpsuan-fa-xiang-jie-mfbs/class Solution { public int strStr(String haystack, String needle) { if (needle.length() == 0) { .原创 2021-04-26 08:13:43 · 104 阅读 · 1 评论 -
【LeetCode】Sama的个人记录_56
不要想的太复杂。自定义一个排序器即可:Arrays.sort(strings, (o1, o2) -> (o2 + o1).compareTo(o1 + o2));说明:1. 这里的排序器的特殊之处在于,不是比较两个独立的元素,而是比较两个元素的组成结果———但这是可行的。2. 这道题其实是一道【贪心】,即为什吗两两的比较,可以得到全局的最优———这是需要证明的。class Solution { public String largestNumber(int[] nums) {.原创 2021-04-15 16:00:41 · 71 阅读 · 0 评论 -
【LeetCode】Sama的个人记录_55
>>> 解法一: 【单调栈】维护一个严格递减的单调栈,找到某个柱子右侧第一个大于等于它的柱子,组成一个容器。>>> 复杂度: O(n)的时间,每个元素最多入栈出栈一次;O(n)的空间,栈的大小最多为nclass Solution { public int trap(int[] height) { Deque<Integer> stack = new ArrayDeque<>(); int capaci.原创 2021-04-07 11:23:37 · 60 阅读 · 0 评论 -
【LeetCode】Sama的个人记录_54
>>> 位运算解法1:>>> res左移 -> 将n的二进制最后一位加在res最后一位上 -> n右移 (32次完成所有位的转移)class Solution { public int reverseBits(int n) { int res = 0; int cnt = 32; while (cnt > 0) { res <<= 1; .原创 2021-03-31 20:19:38 · 63 阅读 · 0 评论 -
【LeetCode】Sama的个人记录_53
>>> 这种"可嵌套的列表"实际上不是列表(list)数据结构,而是一个树形(tree)数据结构>>> 如果结点是一个数字,则它是一个叶子结点;如果结点是一个数字列表,则它是一个子树>>> 那么,遍历的过程就是DFS的过程!!!public class NestedIterator implements Iterator<Integer> { private List<Integer> list; pr.原创 2021-03-25 14:38:26 · 66 阅读 · 0 评论 -
【LeetCode】Sama的个人记录_52
Left Right ----------------------- Top | | | | | | | ----------------------- | | | | | | | -----------------------Bottom | | | | | | | -----------------------class Solution { public List<Integer> spiralOrder(int[][] matr...原创 2021-03-22 09:14:31 · 63 阅读 · 0 评论 -
【LeetCode】Sama的个人记录_51
>>> 两个经典模板:>>> 1. 回溯>>> 2. 动态规划找回文串class Solution { public List<List<String>> partition(String s) { traceBack(new ArrayList<>(), s, 0, getDp(s)); return res; } private List<L.原创 2021-03-09 00:04:23 · 65 阅读 · 0 评论 -
【LeetCode】Sama的个人记录_50
>>> 状态转移方程:dp[i] = dp[i >> 1] + (i & 1)>>> 解释:一个数1的个数 = 该数右移一位得到的数的1的个数 + 移出去的那个数是不是1>>> 补充:判断一个数的奇偶,可以让它与1进行与运算class Solution { public int[] countBits(int num) { int[] dp = new int[num + 1]; fo.原创 2021-03-06 21:12:23 · 54 阅读 · 1 评论 -
【LeetCode】Sama的个人记录_49
>>> 不要一个个斜行去判断,而是一个个横行去判断class Solution { public boolean isToeplitzMatrix(int[][] matrix) { for(int i = 1; i < matrix.length; i++) { for(int j = 1; j < matrix[0].length; j++) { if(matrix[i][j] != ma.原创 2021-03-02 10:48:37 · 68 阅读 · 0 评论 -
【LeetCode】Sama的个人记录_48
看样子是dp,然而是图论。图论无非就是【并查集/DFS/BFS】方法1———并查集这是个很巧妙的思路,首先将点的矩阵转化为边的集合(这一步可以当作模板记住),然后将这些边按所谓高度差绝对值由小到大排序;这些点看作一个个孤立的点(并查集初始化),然后用这些已排序的边一条条连通(并查集union操作),当左上角与右下角连通时即找到了最终答案。class Solution { public int minimumEffortPath(int[][] heights) { int.原创 2021-02-14 20:54:32 · 67 阅读 · 0 评论 -
【LeetCode】Sama的个人记录_47
>>> 经典的图论问题>>> 经典的三种解法———【DFS】【BFS】【并差集】>>> 关键在于【visited/draw】数组的使用 >_<class Solution { // DFS public int findCircleNum(int[][] isConnected) { int len = isConnected.length; boolean[] visited = new .原创 2021-01-22 10:32:38 · 77 阅读 · 1 评论 -
【LeetCode】Sama的个人记录_46
1原创 2021-01-08 22:38:04 · 68 阅读 · 0 评论 -
【LeetCode】Sama的个人记录_45
loli原创 2020-12-22 21:26:14 · 54 阅读 · 0 评论 -
【LeetCode】Sama的个人记录_44
原创 2020-12-02 09:10:59 · 76 阅读 · 0 评论 -
【LeetCode】Sama的个人记录_43
class Solution { // 思路很简单:二进制1计数 + 重写比较器 // 麻烦的一点是:比较器的参数需要是T,int[]需要包装成Integer[],然后解包装... public int[] sortByBits(int[] arr) { Integer[] Arr = Arrays.stream(arr).boxed().toArray(Integer[]::new); Arrays.sort(Arr, (o1, o2) -> { .原创 2020-11-23 09:25:58 · 98 阅读 · 0 评论 -
【LeetCode】Sama的个人记录_42
>>> 数据结构经典问题,这里给出非递归和递归两种解法class Solution { /** *【一遍扫描就可以>_< !!!】 * 双指针:快指针比慢指针快n,然后「共同移动」,当fast走到头时,slow就是我们需要操作的节点 * * 对于 0 -> 1 -> 2 -> 3 -> 4,考虑下面三种情况: * n = 3:正常情况 * n =原创 2020-11-21 10:31:09 · 53 阅读 · 0 评论 -
【LeetCode】Sama的个人记录_41
原创 2020-11-05 22:33:37 · 635 阅读 · 0 评论 -
【LeetCode】Sama的个人记录_40
【Q141】(ez) 环形列表原创 2020-10-23 16:17:48 · 95 阅读 · 0 评论 -
【LeetCode】Sama的个人记录_39
【Q538】(ez) 把二叉搜索树转换成累加树原创 2020-10-15 09:44:53 · 53 阅读 · 0 评论 -
【LeetCode】Sama的个人记录_38
【Q347】(md) 前K个高频元素原创 2020-09-26 23:02:14 · 73 阅读 · 0 评论 -
【LeetCode】Sama的个人记录_37
【Q257】(md) 二叉树的所有路径原创 2020-09-20 23:07:34 · 89 阅读 · 0 评论