Skip to content

AshleyXM/Leetcode_Solutions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

LeetCode_Solutions

Record my solutions to LeetCode problems

Note: The solutions are not necessarily optimal.

Note: The cost information in each problem is for reference only, since I found that even if it was submitted with exactly the same code at different times, the time efficiency and memory efficiency will vary a lot.

Statistics

Problem ID Problem Description My Solution Difficulty Problem Type Accepted Date Note
0001 Two Sum Java Easy Array, Hash Table 5/23/2023
0002 Add Two Numbers Java Medium Linked List, Math, Recursion 6/15/2023 I made a mistake in this problem which cost me a long time to fix it: Changing the pointer of the next node DOES NOT mean having changed the next pointer of the current node. In detail, we have to use "node.next = newNode"; however, "node = node.next; node = newNode" will not work.
The solution 2 is really ingenious and its logic is really easy to understand.
0003 Longest Substring Without Repeating Characters Java Medium String, Hash Table, Sliding Window 5/23/2023
0007 Reverse Integer Java Medium Math 6/15/2023 The idea of reversing a problem is really important. E.g. "rvs <= (Integer.MAX_VALUE - remainder) / 10" is equivalent to "rvs * 10 + remainder <= Integer.MAX_VALUE" (but the latter one will not work).
0008 String to Integer (atoi) Java Medium String 6/15/2023
0009 Palindrome Number Java Easy Math 5/27/2023
0011 Container With Most Water Java Medium Array, Two Pointers, Greedy 6/16/2023 The reason why to move the pointer which points to smaller height is important.
0012 Integer to Roman Java Medium Hash Table, Math, String 4/8/2024 这题切勿用分支来进行判断,而是应该用两个数组/HashMap来实现。
0013 Roman to Integer Java Easy String, Math, Hash Table 5/25/2023
0015 3Sum Java Medium Array, Two Pointers, Sorting 6/15/2023 DO NOT use triple-nested loops (the brute-force attack), otherwise you will encounter runtime error.
0016 3Sum Closest Java Medium Array, Two Pointers, Sorting 4/12/2024 运用排序 + 三指针来解题。
0017 Letter Combinations of a Phone Number Java Medium Hash Table, String, Backtracking 4/12/2024 The key point is using the hashmap data structure and forEach loop of String.
0020 Valid Parentheses Java Easy String, Stack 5/26/2023
0021 Merge Two Sorted Lists Java Easy Linked List, Recursion 6/13/2023 It challenged me for a while on my flight to Seattle. But I had so much fun solving this problem and I learnt a lot. Worth a try!
0022 Generate Parentheses Java Medium String, Dynamic Programming, Backtracking 4/13/2024 这道题大多数题解采用了 backtracking 的方法解题,个人觉得复杂且有点难理解。深受某个评论的启发,得出此解:每多一对括号,只需在之前得到的结果里,尝试在每一个结果的每个位置(字符串开头 - 字符串最后)依次添加"()",并去重来解决(采用 HashSet 数据结构即可)。
0026 Remove Duplicates from Sorted Array Java Easy Array, Two Pointers 6/12/2023
0027 Remove Element Java Easy Array, Two Pointers 6/12/2023
0028 Find the Index of the First Occurrence in a String Java Easy String, String Matching, Two Pointers 5/27/2023
0033 Search in Rotated Sorted Array Java Medium Array, Binary Search 6/18/2023
0034 Find First and Last Position of Element in Sorted Array Java Medium Array, Binary Search 6/21/2023
0035 Search Insert Position Java Easy Array, Binary Search 5/27/2023
0036 Valid Sudoku Java Medium Array, Hash Table, Matrix 6/21/2023 The solution 2 which utilizes HashSet structure is really subtle.
0038 Count and Say Java Medium String 4/13/2024 再次复习 String 对象的 for each 循环 - 调用 String 对象的 toCharArray() 方法。
0039 Combination Sum Java Medium Array, Backtracking 4/30/2024 这个题目用的是 backtracking,即回溯法。解题思路是将所有的组合都尝试一遍,和为 target 组合就加入到结果里。
0040 Combination Sum II Java Medium Array, Backtracking 4/30/2024 这道题目和 0039 很相似,但是 candidates 数组有重复的数字,且每个数字只能使用一次,加上 candiates 数组的长度比 0039 大,因此如果只是修改 dfs 函数的 left 索引值,会导致超时,这题需要用到剪枝(pruning)的技巧。
0046 Permutations Java Medium Array, Backtracking 4/30/2024 这道题目和 0039 相似,解题思路也是非常类似的。flags 数组其实可以改成 boolean 类型,因为之前的方法用的是数组中 1 的个数来判断是否到达 base case,但是其实没有必要,可以直接通过 path 的结点个数来判断即可,因此这部分其实可以优化一下。
0047 Permutations II Java Medium Array, Backtracking 4/30/2024 这道题目和 0046 基本一致,因为 nums 的大小最大为 8,所以不剪枝也不会超时。剪枝的方法也放到最后作为注释了。
0048 Rotate Image Java Medium Array, Math, Matrix 5/1/2024 这个题目有一定的技巧性,如向右旋转 90° 实质上是先上下翻转,然后沿着对角线翻转。同理,向左旋转 90° 其实是其逆过程,先沿着对角线翻转,然后上下翻转。如果是向右翻转 180°,那就是先上下翻转,然后左右翻转。以此类推。。
0049 Group Anagrams Java Medium Array, Hash Table, String, Sorting 6/22/2023 The main idea of the solution is a little similar to 0036.
0050 Pow(x, n) Java Medium Math, Recursion 5/1/2024 这个题目的解题关键点在于将幂 n 逐步分解为 n/2 -> n/4 -> n/8 -> ...,同时还得注意 n 的正负性和奇偶性。
0053 Maximum Subarray Java Medium Array, Divide and Conquer, Dynamic Programming 6/18/2023
0054 Spiral Matrix Java Medium Array, Matrix, Simulation 5/1/2024 这道题目解题要点在于掌握移动的方向和边界条件。我们可以通过四个变量来记录上下左右边界的位置,从而简便地解题(无需设置一个二维数组记录移动方向)。
0055 Jump Game Java Medium Array, Dynamic Programming, Greedy 6/21/2023 The solution 1 plus is really subtle since it simplifies the condition of the "for" loop.
0056 Merge Intervals Java Medium Array, Sorting 6/22/2023 Similar to 0057. The method to sort the 2d array and the method to convert a list into an array are really subtle!!!
0057 Insert Interval Java Medium Array 6/19/2023 A little bit tricky! The key idea of my solution is to discuss the start point and end point of the new interval separately.
0058 Length of Last Word Java Easy String 5/27/2023
0059 Spiral Matrix II Java Medium Array, Matrix, Simulation 5/1/2024 这道题目是 0054 的变式,是其逆过程,解法是类似的,也是通过上下边界和左右边界来逐步缩小来解题的。
0061 Rotate List Java Medium Linked List, Two Pointers 5/1/2024 这个题目可以通过找规律来解题:每移动一次关注新尾巴的变化,移动第一次,新 tail 为原来倒数第二个结点(正数总结点数量 - 1 的位置),移动第二次,新 tail 为原来倒数第三个结点(正数总结点数量 - 2 的位置),以此类推。。
0062 Unique Paths Java Medium Math, Dynamic Programming, Combinatorics 5/3/2024 这个题目看似用组合数计算比较简便,但是实际上用循环来实现的时候有雷点,如循环控制变量的初始值,以及如何用一个循环来实现等等。推荐用动态规划来实现更简单。
0066 Plus One Java Easy Array, Math 6/8/2023
0067 Add Binary Java Easy String, Math, Bit Manipulation 5/27/2023
0069 Sqrt(x) Java Easy Math, Binary Search 5/28/2023 Similar to 0035.
0070 Climbing Stairs Java Easy Math, Dynamic Programming, Memoization 5/28/2023
0072 Edit Distance Java Medium String, Dynamic Programming 1/28/2024
0074 Search a 2D Matrix Java Medium Array, Binary Search, Matrix 6/18/2023 The solution 2 is really easy to understand and ingenious.
0075 Sort Colors Java Medium Array, Two Pointers, Sorting 6/30/2023 Actually I do not think it is a medium problem but an easy one.
0081 Search in Rotated Sorted Array II Java Medium Array, Binary Search 9/8/2023 The enhanced version of 0033. It is not worth a try since if you would like to use binary search, you would find that the time complexity is the same as the brutal-force method, which is sequential search.
0083 Remove Duplicates from Sorted List Java Easy Linked List 6/14/2023
0088 Merge Sorted Array Java Easy Array, Two Pointers, Sorting 5/29/2023
0090 Subsets II Java Medium Array, Backtracking, Bit Manipulation 6/21/2023 The statement of this problem is unclear since the number of the examples is too small to clarify it.
0094 Binary Tree Inorder Traversal Java Easy Stack, Tree, Depth-First Search, Binary Tree 6/5/2023 Typical knowledge point and frequently be tested in interviews. (DFS in Tree)
0095 Unique Binary Search Trees II Java Medium Dynamic Programming, Backtracking, Tree, Binary Search Tree, Binary Tree 9/26/2024 这道题目运用分治法的思想,将 1 ~ n 问题规模逐渐缩小:分别以 1 ~ n 为 root,当以 i 为 root 时,左子树则为 1 ~ (i - 1),右子树为 (i + 1) ~ n。
0096 Unique Binary Search Trees Java Medium Math, Dynamic Programming, Tree, Binary Search Tree, Binary Tree 9/30/2024 这道题目如果用普通的递归会导致超时,因此我们需要利用前面计算出的结果来推后面的结果,也就是找出动态规划的推导式,一旦找出推导式了,那么这个问题就迎刃而解了。
0098 Validate Binary Search Tree Java Medium Tree, Depth-First Search, Binary Search Tree, Binary Tree 6/26/2023 The first solution is a little bit tricky about the initial minimum and maximum value, since the value of the node can be Integer.MIN_VALUE or Integer.MAX_VALUE.
0100 Same Tree Java Easy Tree, Depth-First Search, Breadth-First Search, Binary Tree 6/14/2023 An easy but interesting recursive tree-related problem.
0101 Symmetric Tree Java Easy Tree, Depth-First Search, Breadth-First Search, Binary Tree 6/6/2023 Tree-related. Quite interesting.
0104 Maximum Depth of Binary Tree Java Easy Tree, Depth-First Search, Breadth-First Search, Binary Tree 6/9/2023
0108 Convert Sorted Array to Binary Search Tree Java Easy Array, Divide and Conquer, Tree, Binary Search Tree, Binary Tree 7/21/2024 这道题目虽然是 easy 题目,但是解题的思路很重要:需要将待解的问题分为左半部分和右半部分(分治法),知道了用分治法还不够,因为 helper function 的写法很重要。在这道题目里,helper function 返回创建的结点比较简单,而不是在函数创建结点并赋值给 left 和 right 指针(函数值返回为 void)。后者会使得整个解法比较复杂。
0110 Balanced Binary Tree Java Easy Tree, Depth-First Search, Binary Tree 6/6/2023 Tree-related. The solution 2 is really ingenious. The method to get the depth of a tree node is fantastic!
0111 Minimum Depth of Binary Tree Java Easy Tree, Depth-First Search, Breadth-First Search, Binary Tree 7/21/2024 解法一到解法二的优化很重要:当一种情况的写法很复杂时,考虑其相反情况的写法会不会更容易。
0112 Path Sum Java Easy Tree, Depth-First Search, Breadth-First Search, Binary Tree 6/20/2023
0113 Path Sum II Java Medium Backtracking, Tree, Depth-First Search, Binary Tree 6/20/2023 The variant of 0112. One tricky point of this problem is that, once we update the value of a list, the original reference will update at the same time. To be more specific, "result.add(current);", where both result and current are of type List, if we update the value of current, then the value of result will update.
0118 Pascal's Triangle Java Easy Array, Dynamic Programming 5/28/2023
0119 Pascal's Triangle II Java Easy Array, Dynamic Programming 5/28/2023 The variant of 0118.
0121 Best Time to Buy and Sell Stock Java Easy Array, Dynamic Programming 5/28/2023 A little bit tricky!
0125 Valid Palindrome Java Easy Two Pointers, String 5/27/2023
0128 Longest Consecutive Sequence Java Medium Array, Hash Table, Union Find 6/16/2023
0133 Clone Graph Java Medium Hash Table, Depth-First Search, Breadth-First Search, Graph 9/27/2024 这道题目因为图是一个无向图,所以不同结点的邻居可以是相同结点,因此需要在创建 clone 结点的时候记录其值,具体方法为创建一个 HashMap,记录原结点和 clone 结点之间的对应关系。
0136 Single Number Java Easy Array, Bit Manipulation 6/9/2023
0139 Word Break Java Medium Array, Hash Table, String, Dynamic Programming, Trie, Memoization 9/2/2023 A really good example of Dynamic Programming. Worth a try!
0141 Linked List Cycle Java Easy Hash Table, Linked List, Two Pointers 5/28/2023
0144 Binary Tree Preorder Traversal Java Easy Stack, Tree, Depth-First Search, Binary Tree 8/29/2023
0145 Binary Tree Postorder Traversal Java Easy Stack, Tree, Depth-First Search, Binary Tree 8/29/2023
0153 Find Minimum in Rotated Sorted Array Java Medium Array, Binary Search 6/23/2023
0160 Intersection of Two Linked Lists Java Easy Hash Table, Linked List, Two Pointers 5/28/2023
0162 Find Peak Element Java Medium Array, Binary Search 9/27/2024 这道题目如果不限制时间复杂度为 O(log n) 其实难度不大,可以直接用循环 - 如果当前的值比后一个值小继续往后遍历,否则返回当前的索引。
但是加上了时间复杂度的限制为 O(log n) 后,我们需要考虑如何运用二分法来求解:比较当前值和后一个值的大小,如果当前大于后一个的值,说明有一个 peak 在当前或其前面,因此将右指针置为当前索引;如果当前小于后一个的值,说明有一个 peak 在后一个或其后面,因此将左指针置为当前索引 + 1。一直重复上述过程,直到左右指针重复,返回左指针所指向的索引的【注意不是返回中间指针】。
0167 Two Sum II - Input Array Is Sorted Java Medium Array, Two Pointers, Binary Search 9/27/2024 典型二分法。
0168 Excel Sheet Column Title Java Easy Math, String 5/29/2023 A little bit tricky!
0169 Majority Element Java Easy Array, Hash Table, Divide and Conquer, Sorting 5/29/2023
0171 Excel Sheet Column Number Java Easy Math, String 5/29/2023 The inverse process of 0168.
0175 Combine Two Tables MySQL Easy Database 5/31/2023
0181 Employees Earning More Than Their Managers MySQL Easy Database 5/31/2023 The efficiency of my solution is so low.
0182 Duplicate Emails MySQL Easy Database 6/1/2023
0183 Customers Who Never Order MySQL Easy Database 9/19/2023
0189 Rotate Array Java Medium Array, Math, Two Pointers 6/18/2023
0195 Tenth Line Bash Easy Shell 10/10/2023 Do not write spaces around the assignment sign, otherwise it would run into error.
0198 House Robber Java Medium Array, Dynamic Programming 6/23/2023 The method of dynamic programming is really subtle.
0200 Number of Islands Java Medium Array, Depth-First Search, Breadth-First Search, Union Find, Matrix 10/11/2024 这题注意数组是 char 类型而不是 int 类型,在编程的时候容易犯错去和 int 类型的数值判断了,需要特别注意。
相关题目:0463 Island Perimeter, 0695 Max Area of Island, 0827 Making A Large Island.
0202 Happy Number Java Easy Math, Hash Table, Two Pointers 6/5/2023 A little bit tricky about how to record the loop.
0205 Isomorphic Strings Java Easy String, Hash Table 5/31/2023
0206 Reverse Linked List Java Easy Linked List, Recursion 6/14/2023 A typical link list problem (reverse the link list), worth a try!
0207 Course Schedule Java Medium Depth-First Search, Breadth-First Search, Graph, Topological Sort 10/13/2024 这个题目使用的策略是拓扑排序,核心在于记录每门课程的入度和前置课和后置课之间的关系。将入度为 0 的课程存到一个队列里,之后每次找入度为 0 的课程,找到后将这门课从队列移除,并将其后置课程的入度减 1,如果有新的课程入度为 0,则将这门课添加到队列中,重复以上步骤直至队列为空。当队列为空时,所有的课程都从队列中出队了,说明不会有相互依赖的情况出现。
0209 Minimum Size Subarray Sum Java Medium Array, Binary Search, Sliding Window, Prefix Sum 9/27/2024 方法一:sliding window 方法,设置两个 pointer,一个指向 start,一个指向 end。固定 start,移动 end,当 start 到 end 的值初次大于等于 target 时,继续start 到 end 之间的数字个数 count,start 向后移,重复之前步骤,找到最小的 count。
0210 Course Schedule II Java Medium Depth-First Search, Breadth-First Search, Graph, Topological Sort 10/13/2024 这个题目是 0207 Course Schedule 的变式,只需要多记录一个访问的顺序而已。如果掌握了 0207,这个问题就没有什么难度。
0215 Kth Largest Element in an Array Java Medium Array, Divide and Conquer, Sorting, Heap (Priority Queue), Quickselect 9/8/2023 Usually, problems with the form of asking the k-th largest or smallest number should utilize the structure of priority queue.
0217 Contains Duplicate Java Easy Array, Hash Table, Sorting 6/1/2023 Really interesting when you are trying to find a better solution.
0219 Contains Duplicate II Java Easy Array, Hash Table, Sliding Window 8/29/2023
0222 Count Complete Tree Nodes Java Easy Binary Search, Bit Manipulation, Tree, Binary Tree 9/27/2024 这个题目如果不利用完全二叉树的性质解题,仅仅通过递归每个结点就可以求解。但是这种解法效率不高 - 时间复杂度为 O(n)。
如果利用完全二叉树的性质解题可以提高效率,对于一个节点 root,如果其左子树的高度比右子树大,那么可算出除了左子树的结点个数为 (2^rightHeight - 1) + 1 = 2^rightHeight【+1是加上 root 结点】;而如果左右子树高度一样,则可以计算出除了右子树的结点个数为 (2^leftHeight - 1) + 1 = 2^leftHeight【道理类似前面所说】。这个方法的时间复杂度为 O(log^2 n)。参考 此 leetcode 题解
0226 Invert Binary Tree Java Easy Tree, Depth-First Search, Breadth-First Search, Binary Tree 6/5/2023 I am weak at solving problems related to Tree. It took me a relatively long time to solve this problem.
0228 Summary Ranges Java Easy Array 6/13/2023 It takes me a while to figure out the problem.
0231 Power of Two Java Easy Math, Bit Manipulation, Recursion 6/4/2023 It is easy to misunderstand the problem, so more attention is needed.
0234 Palindrome Linked List Java Easy Linked List, Two Pointers, Stack, Recursion 6/14/2023
0238 Product of Array Except Self Java Medium Array, Prefix Sum 6/19/2023 More like a easy problem than a medium one.
0242 Valid Anagram Java Easy String, Hash Table, Sorting 5/31/2023
0257 Binary Tree Paths Java Easy String, Backtracking, Tree 5/31/2023 Not that easy, worth a try.
0258 Add Digits Java Easy Math, Simulation, Number Theory 5/31/2023 Very interesting! Once you find the pattern, it can be solved with three lines of code.
0263 Ugly Number Java Easy Math 8/29/2023
0264 Ugly Number II Java Medium Hash Table, Math, Dynamic Programming, Heap (Priority Queue) 9/28/2024 这道题目目前采用的是最小堆的方式来解题的:首先将 1 添加到最小堆里,取最小堆的 root,然后依次将最小堆里 root 的 2 倍,3 倍和 5 倍添加进来(如果要添加的数已经存在就不添加进树里了),重复以上步骤直到取出了 n 个数。但是这个解法效率不高,后续待补充动态规划的解法
0268 Missing Number Java Easy Array, Hash Table, Math, Binary Search, Bit Manipulation, Sorting 6/9/2023
0278 First Bad Version Java Easy Binary Search, Interactive 6/10/2023 A little bit tricky to use proper data structure.
0283 Move Zeroes Java Easy Array, Two Pointers 6/9/2023
0290 Word Pattern Java Easy Hash Table, String 8/30/2023 Pay attention to the problem description. It is a little bit tricky.
0292 Nim Game Java Easy Math, Brainteaser, Game Theory 8/31/2023 Not worth trying. The idea is to find the pattern of the solution, like when n = 1, 2, 3, 4 and n = 5, 6, 7, 8 and so on and so forth.
0303 Range Sum Query - Immutable Java Easy Array, Design, Prefix Sum 8/31/2023
0310 Minimum Height Trees Java Medium Depth-First Search, Breadth-First Search, Graph, Topological Sort 10/13/2024 这个题难点在于如何找到它和拓扑排序之间的关联,也就是为什么要一层一层去掉度数为 1 的结点(这个结论的证明不是太理解,有兴趣可以参考 力扣中国区官方题解 的证明)。
如果理解了其解题逻辑,写代码其实并不是最难的部分,多花一点时间还是可以写出来的,注意边界情况即可。
0326 Power of Three Java Easy Math, Recursion 8/31/2023
0338 Counting Bits Java Easy Dynamic Programming, Bit Manipulation 5/30/2023 Not that easy, worth a try.
0344 Reverse String Java Easy String, Two Pointers 6/6/2023
0345 Reverse Vowels of a String Java Easy String, Two Pointers 6/11/2023
0347 Top K Frequent Elements Java Medium Array, Hash Table, Divide and Conquer, Sorting, Heap (Priority Queue), Bucket Sort, Counting, Quickselect 6/15/2023
0383 Ransom Note Java Easy Hash Table, String, Counting 6/14/2023
0384 Shuffle an Array Java Medium Array, Math, Randomized 10/13/2024 这道题目的难点在于如何使得元素的 shuffle 是随机的,这个其实是蓄水池算法的微缩版,可以参考博客园的这篇文章:谈谈游戏中的随机算法
简单来说,就是遍历 nums 数组,将第 i 个元素换到索引值 [i, nums.length - 1] 这个范围里随机的位置上即可(即相当于这 n 个元素有 n! 种排列方式)。
0392 Is Subsequence Java Easy Dynamic Programming, Two Pointers, String 5/30/2023
0409 Longest Palindrome Java Easy Hash Table, String, Greedy 7/9/2023
0412 Fizz Buzz Java Easy Math, String, Simulation 6/1/2023
0414 Third Maximum Number Java Easy Array, Sorting 6/1/2023
0415 Add Strings Java Easy Math, String, Simulation 6/1/2023
0435 Non-overlapping Intervals Java Medium Array, Dynamic Programming, Greedy, Sorting 9/25/2024 解题思想:贪心算法,按照 interval 的结束时间来排序,在选择的时候,优先早结束的 interval,因为它会为后面的 interval 留更多的时间(贪心算法)。
0436 Find Right Interval Java Medium Array, Binary Search, Sorting 9/23/2024 第一种解法:暴力解法,双重循环,时间复杂度为 O(n^2)
第二种解法:二分搜索法,时间复杂度优化为 O(nlogn)
0455 Assign Cookies Java Easy Array, Two Pointers, Greedy, Sorting 7/9/2023
0463 Island Perimeter Java Easy Array, Depth-First Search, Breadth-First Search, Matrix 10/11/2024 这道题目看似简单,如果没有弄清原理其实也不算简单。计算岛屿的周长的核心思路是当一个点的上下左右任意相邻的一边的点越界或为 0 时,这条边才为周长的一部分,否则只是岛屿内部的一条边而已。参考 此解
相关题目:0200 Number of Islands, 0695 Max Area of Island, 0827 Making A Large Island.
0501 Find Mode in Binary Search Tree Java Easy Tree, Depth-First Search, Binary Search Tree, Binary Tree 7/18/2024 Takeaway of solution 2 with O(1) space complexity: 利用 BST 的有序性,中序遍历 BST 可以得到有序数列,因此题目可以转换为在一个有序数列里找众数。
0509 Fibonacci Number Java Easy Math, Dynamic Programming, Recursion, Memoization 6/14/2023
0530 Minimum Absolute Difference in BST Java Easy Tree, Depth-First Search, Breadth-First Search, Binary Search Tree, Binary Tree 6/14/2023 This problem is almost the same as 0783. Try to make full use of the features of BST (Binary Search Tree)!
0543 Diameter of Binary Tree Java Easy Tree, Depth-First Search, Binary Tree 6/11/2023 A little bit tricky, not that easy! Worth a try.
0547 Number of Provinces Java Medium Depth-First Search, Breadth-First Search, Union Find, Graph 6/19/2023
10/14/2024
时隔一年再次做这道题,这次的解法更简洁了(利用 DFS 解题)。
0561 Array Partition Java Easy Array, Greedy, Sorting, Counting Sort 7/3/2023
0584 Find Customer Referee MySQL Easy Database 6/14/2023
0595 Big Countries MySQL Easy Database 6/14/2023
0605 Can Place Flowers Java Easy Array, Greedy 6/9/2023
0680 Valid Palindrome II Java Easy Two Pointers, String, Greedy 7/10/2023
0695 Max Area of Island Java Medium Array, Depth-First Search, Breadth-First Search, Union Find, Matrix 10/11/2024 相关题目:0200 Number of Islands, 0463 Island Perimeter, 0827 Making A Large Island.
0703 Kth Largest Element in a Stream Java Easy Tree, Design, Binary Search Tree, Heap (Priority Queue), Binary Tree, Data Stream 6/13/2023
0704 Binary Search Java Easy Array, Binary Search 6/8/2023
0705 Design HashSet Java Easy Array, Hash Table, Linked List, Design, Hash Function 6/9/2023
0744 Find Smallest Letter Greater Than Target Java Easy Array, Binary Search 6/9/2023
0746 Min Cost Climbing Stairs Java Easy Array, Dynamic Programming 6/27/2023
0783 Minimum Distance Between BST Nodes Java Easy Tree, Depth-First Search, Breadth-First Search, Binary Search Tree, Binary Tree 6/14/2023 This problem is almost the same as 0530. Try to make full use of the features of BST (Binary Search Tree)!
0807 Max Increase to Keep City Skyline Java Medium Array, Greedy, Matrix 7/10/2023 The method to calculate the maximum value in each row and each column with only one double nested for loop is fantastic.
0827 Making A Large Island Java Hard Array, Depth-First Search, Breadth-First Search, Union Find, Matrix 10/11/2024 相关题目:0200 Number of Islands, 0463 Island Perimeter, 0695 Max Area of Island.
0860 Lemonade Change Java Easy Array, Greedy 7/9/2023
0873 Length of Longest Fibonacci Subsequence Java Medium Array,Hash Table,Dynamic Programming 2/1/2024
0874 Walking Robot Simulation Java Medium Array, Hash Table, Simulation 2/1/2024 这个题目看似简单,实际难点在于obstacles的判断:本题必须用HashSet而非List来进行obstacles的判定,因为hashset的查找效率更快,使用List会导致超时。另外就是如何将二维的坐标转化为一维的数据:这里是根据xi和yi的范围来实现的,详细实现参看代码。
0876 Middle of the Linked List Java Easy Linked List, Two Pointers 6/14/2023
0877 Stone Game Java Medium Array, Math, Dynamic Programming, Game Theory 1/30/2024
0880 Decoded String at Index Python Medium String, Stack 3/25/2024 This problem seems easy to solve, but how to minimize the memory is the key issue. The basic idea is to record how the total length would be influenced when one character is read, then retrodict which character would be at the given index.
0881 Boats to Save People Java Medium Array, Two Pointers, Greedy, Sorting 3/26/2024 This problem seems kind of tricky at first, but I found that I got the problem wrong twice. We need to take care of the requirement that each boat can carry at most two people at the same time.
0938 Range Sum of BST Java Easy Tree, Depth-First Search, Binary Search Tree, Binary Tree 7/18/2024
0942 DI String Match Java Easy Array, Two Pointers, String, Greedy 7/3/2023 The solution which used two pointers is really subtle.
0976 Largest Perimeter Triangle Java Easy Array, Math, Greedy, Sorting 7/9/2023
0977 Squares of a Sorted Array Java Easy Array, Two Pointers, Sorting 6/5/2023
0986 Interval List Intersections Java Medium Array, Two Pointers 9/22/2024 学到的内容:
1. 创建 int[] 类型的ArrayList
2. 求两个区间的交集:如果两个区间的起点的最大值小于等于终点的最小值,则说明有交集
3. int[] 类型的 List 转数组的方法:res.toArray(new int[0][])
1005 Maximize Sum Of Array After K Negations Java Easy Array, Greedy, Sorting 7/9/2023 Note that in the problem description we may choose the same index multiple times.
1013 Partition Array Into Three Parts With Equal Sum Java Easy Array, Greedy 7/10/2023
1025 Divisor Game Java Easy Math, Dynamic Programming, Brainteaser, Game Theory 6/29/2023 It depends heavily on mathematical induction. Not worth giving it a shot.
1027 Longest Arithmetic Subsequence Java Medium Array, Hash Table, Binary Search, Dynamic Programming 6/26/2023
1071 Greatest Common Divisor of Strings Java Easy Math, String 6/10/2023 A little tricky, not that easy! It is easy to misunderstand the problem.
1091 Shortest Path in Binary Matrix Java Medium Array, Breadth-First Search, Matrix 6/18/2023 The idea of the solution is similar to Bellman-Ford /algorithm. And the double-nested loop is the important part to simplify the solution which has eight "if" branches.
1137 N-th Tribonacci Number Java Easy Math, Dynamic Programming, Memoization 6/28/2023
1161 Maximum Level Sum of a Binary Tree Java Medium Tree, Depth-First Search, Breadth-First Search, Binary Tree 6/19/2023 Make sure you understand what the problem asks for first before starting to solve it.
1217 Minimum Cost to Move Chips to The Same Position Java Easy Array, Math, Greedy 7/7/2023 Pay attention to the cost part.
1221 Split a String in Balanced Strings Java Easy String, Greedy, Counting 7/2/2023
1232 Check If It Is a Straight Line Java Easy Array, Math, Geometry 6/8/2023
1318 Minimum Flips to Make a OR b Equal to c Java Medium Bit Manipulation 6/22/2023
1323 Maximum 69 Number Java Easy Math, Greedy 7/2/2023
1337 The K Weakest Rows in a Matrix Java Easy Array, Binary Search, Sorting, Heap (Priority Queue), Matrix 1/29/2024 The first method might be more efficient, but a lot can be learnt from the second method---the sorting of map.
1351 Count Negative Numbers in a Sorted Matrix Java Easy Array, Binary Search, Matrix 6/12/2023 The solution 2 is so ingenious a solution!
1353 Maximum Number of Events That Can Be Attended Java Medium Array, Greedy, Sorting, Heap (Priority Queue) 9/25/2024 这题主要是采用贪心算法,每往后遍历一天,先清除过期的 event,然后添加今天开始的 event,最后选择一个会议 - 结束时间最早的会议(贪心)。
1403 Minimum Subsequence in Non-Increasing Order Java Easy Array, Greedy, Sorting 7/10/2023
1480 Running Sum of 1d Array Java Easy Array, Prefix Sum 6/9/2023
1491 Average Salary Excluding the Minimum and Maximum Salary Java Easy Array, Sorting 6/1/2023
1502 Can Make Arithmetic Progression From Sequence Java Easy Array, Sorting 6/8/2023
1517 Find Users With Valid E-Mails Java Easy Database 6/30/2023
1518 Water Bottles Java Easy Math, Simulation 6/30/2023
1523 Count Odd Numbers in an Interval Range Java Easy Math 6/30/2023
1528 Shuffle String Java Easy Array, String 6/30/2023 The method of sorting a 2d array by one of the two dimensions is a really important method to solve problems.
1539 Kth Missing Positive Number Java Easy Array, Binary Search 1/28/2024
1603 Design Parking System Java Easy Design, Simulation, Counting 6/14/2023
1641 Count Sorted Vowel Strings Java Medium Math, Dynamic Programming, Combinatorics 7/13/2023
1646 Get Maximum in Generated Array Java Easy Array, Dynamic Programming, Simulation 6/28/2023
1689 Partitioning Into Minimum Number Of Deci-Binary Numbers Java Medium String, Greedy 7/10/2023
1710 Maximum Units on a Truck Java Easy Array, Greedy, Sorting 7/3/2023
1732 Find the Highest Altitude Java Easy Array, Prefix Sum 6/21/2023
1736 Latest Time by Replacing Hidden Digits Java Easy String, Greedy 7/9/2023
1768 Merge Strings Alternately Java Easy Two Pointers, String 6/14/2023
1822 Sign of the Product of an Array Java Easy Array, Math 6/1/2023
1827 Minimum Operations to Make the Array Increasing Java Easy Math, Greedy 7/2/2023
1903 Largest Odd Number in String Java Easy Math, String, Greedy 7/9/2023
1974 Minimum Time to Type Word Using Special Typewriter Java Easy String, Greedy 7/3/2023
2027 Minimum Moves to Convert String Java Easy String, Greedy 7/9/2023
2078 Two Furthest Houses With Different Colors Java Easy Array, Greedy 7/8/2023 The idea is subtle.
2089 Find Target Indices After Sorting Array Java Easy Array, Binary Search, Sorting 1/27/2024
2144 Minimum Cost of Buying Candies With Discount Java Easy Array, Greedy, Sorting 7/8/2023
2160 Minimum Sum of Four Digit Number After Splitting Digits Java Easy Math, Greedy, Sorting 7/2/2023
2215 Find the Difference of Two Arrays Java Easy Array, Hash Table 6/1/2023 The method I used is just like the most original one, which has a low efficiency.
2224 Minimum Number of Operations to Convert Time Java Easy String, Greedy 7/8/2023
2259 Remove Digit From Number to Maximize Result Java Easy String, Greedy, Enumeration 7/9/2023
2335 Minimum Amount of Time to Fill Cups Java Easy Array, Greedy, Sorting, Heap (Priority Queue) 7/9/2023 The usage of "diff / 2 + diff % 2 = (diff + 1) / 2" is really subtle.
2352 Equal Row and Column Pairs Java Medium Array, Hash Table, Matrix, Simulation 6/16/2023
2357 Make Array Zero by Subtracting Equal Amounts Java Easy Array, Hash Table, Greedy, Sorting, Heap (Priority Queue), Simulation 7/3/2023 It is my first time to use heap (Priority Queue).
2383 Minimum Hours of Training to Win a Competition Java Easy Array, Greedy 7/11/2023 The "initialExperience <= experience[i]" part is a little bit tricky.
2389 Longest Subsequence With Limited Sum Java Easy Array, Binary Search, Greedy, Sorting, Prefix Sum 7/4/2023 The second solution which adopts binary search and the third solution which uses the built-in function Arrays.binarySearch are worth learning.
2566 Maximum Difference by Remapping a Digit Java Easy Math, Greedy 7/8/2023
2578 Split With Minimum Sum Java Easy Math, Greedy, Sorting 7/7/2023
2591 Distribute Money to Maximum Children Java Easy Array, Greedy 6/12/2023 A little tricky, not that easy. It is important to think it thoroughly before you start coding.
2600 K Items With the Maximum Sum Java Easy Math, Greedy 7/8/2023
2656 Maximum Sum With Exactly K Elements Java Easy Array, Greedy 6/6/2023
2696 Minimum String Length After Removing Substrings Java Easy String, Stack, Simulation 6/6/2023 Involved with the use of the stack structure.
2706 Buy Two Chocolates Java Easy Array, Sorting 6/10/2023 Try to find a better solution to have some fun.
2708 Maximum Strength of a Group Java Medium Array, Greedy, Recursion, Sorting 6/22/2023
2710 Remove Trailing Zeros From a String Java Easy String 6/10/2023
2717 Semi-Ordered Permutation Java Easy Array, Simulation 6/10/2023 A little interesting to discuss the two cases.
2718 Sum of Matrix After Queries Java Medium Array, Hash Table 6/24/2023 The solution is really subtle about the starting value of the loop, cuz the latter assignment will overwrite the former assignments. So it is a good way to loop reveresely.
2729 Check if The Number is Fascinating Java Easy Hash Table, Math 6/11/2023
2733 Neither Minimum nor Maximum Java Easy Array, Sorting 6/12/2023 Pay more attention to the problem statement, especially the keyword any. Have fun to find better solution than ever before.
2739 Total Distance Traveled Java Easy Math, Simulation 6/23/2023 The second solution is really subtle with just one line of code.
2824 Count Pairs Whose Sum is Less than Target Java Easy Array, Two Pointers, Binary Search, Sorting 1/27/2024

About

Record my solutions of leetcode problems

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages