data structure
-
array
Topic: 136. Numbers that occur only once
Title: 169. Majority elements
Topic: 15. The sum of three numbers (difficult)
This method is what we often call "double pointer". When we need to enumerate two elements in the array, if we find that the second element is decremented as the first element increases, then we can use the double pointer method.
If the number needs to be different from the last enumeration, this technique is also used in permutation and combination problems:
if first > 0 and nums[first] == nums[first - 1]: continue
That is, if they are the same, directly judge the next element
Title: 75. Color classification (Dutch flag problem, classic)
Title: 56. Merging Intervals
Topic: 706. Designing Hash Maps
Title: 119. Yang Hui Triangle II
Title: 48. Rotate an image
Main diagonal flip + horizontal flip, or, y=x flip + vertical flip, the answer is the former, the code is more concise
Title: 59. Spiral Matrix II
Topic: 240. Searching Two-Dimensional Matrices II
Title: 435. Non-overlapping intervals
class Solution: def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int: if not intervals: return 0 intervals.sort(key=lambda x: x[1]) n = len(intervals) right = intervals[0][1] ans = 1 for i in range(1, n): if intervals[i][0] >= right: ans += 1 right = intervals[i][1] return n - ans
Greedy, sort by the left endpoint of the interval, add the ones that meet the requirements, and delete the rest
Title: 334. Incrementing Ternary Subsequences
Consider left and right; greedy, memorize method
Topic: 238. Product of arrays other than themselves
Prefix sum + suffix sum, IQ rolling
-
string
Title: 415. Adding Strings
Topic: 409. Longest Palindrome
Topic: 290. Word Rules
Topic: 763. Dividing Alphabet Intervals
Topic: 49. Grouping of anagrams
Title: 43. Multiply Strings
Title: 187. Repeated DNA Sequences
Topic: 5. Longest Palindromic Substring
The horse-drawn cart still doesn't understand, go listen to Zuo Shen again, and write the code yourself
-
linked list
Topic: 2. Adding two numbers
Title: 142. Circular Linked List II
Topic: 160. Intersecting linked lists (pointing to each other's head nodes)
Title: 82. Delete Duplicate Elements in a Sorted List II
Dumb node + fast and slow pointer implementation does not take up extra space
Topic: 24. Exchange nodes in a linked list pairwise
Topic: 707. Designing Linked Lists
Topic: 25. A group of K flipped linked lists
Topic: 143. Rearranging linked lists (classic questions)
-
stack/queue
Title: 155. Minimal Stack
It is said that there is a practice of not using additional space (auxiliary stack)
Subject: 1249. Remove Invalid Parentheses
Title: 1823. Find the Winner of the Game ( Joseph Ring )
-
Tree
Topic: 108. Convert Sorted Array to Binary Search Tree
Title: 105. Construct Binary Trees from Preorder and Inorder Traversals
Title: 103. Zigzag Level Order Traversal of Binary Trees
Title: 199. Right View of a Binary Tree
You can also use dfs
Title: 113. Path sums II
(Unscheduled) Topic: 437. Pathways to Sum III
Use the prefix and
Title: 450. Delete a node in a binary search tree (classic question)
Topic: 230. The Kth Smallest Element in a Binary Search Tree
Title: 173. Binary Search Tree Iterators
Title: 236. The nearest common ancestor of a binary tree
Title: 297. Serialization and deserialization of binary trees
-
picture
Topic: 997. Finding the Town's Judge
Title: 1557. The Minimum Number of Points All Points Can Be Reached
Topic: 841. Keys and Rooms
-
priority queue (heap)
Title: 215. The Kth Largest Element in an Array
Title: 347. Top K High Frequency Elements
algorithm
-
two points
Topic: 34. Find the first and last position of an element in a sorted array
Topic: 33. Searching a Rotated Sorted Array (This question is very troublesome, the answer is very good)
Topic: 74. Searching a two-dimensional matrix (2 degree bisection is more common)
Topic: 153. Find the minimum value in a rotated sorted array
Topic: 162. Finding Peaks (Auxiliary Functions Handling Boundaries, Conditional Simplification)
-
double pointer
Title: 82. Delete Duplicate Elements in a Sorted List II
Elegant solution, one traversal
Topic: 15. The sum of three numbers
God K's solution is better understood, the second brush still can't (OvO)
Subject: 844. Comparing Strings with Backspaces
Simple questions, but examine the coding ability
-
sliding window
Topic: 438. Find all anagrams in a string (emphasis)
This question is very similar to the previous one. You can use diff to record different numbers, so you only need to judge the situation of entry and exit
Title: 713. Subarrays whose product is less than K
Title: 209. Minimum Length Subarray
-
dfs/bfs
Topic: 200. Number of Islands
Title: 547. Number of Provinces
As an introductory question for dfs/bfs of a graph, it is an undirected and acyclic graph
Title: 117. Filling the next right node pointer of each node II
Topic: 572. Subtree of another tree (this simple question is not easy)
Title: 1091. Shortest Paths in a Binary Matrix
- dfs: determine whether the path/result exists
- bfs: Find the shortest/optimal path
Here is a solution to the problem, the analysis of dfs and bfs is better, and why the visited of bfs can be used globally, https://leetcode-cn.com/problems/shortest-path-in-binary-matrix/solution/bfszui-duan-lu-jing-wen-ti-bfsdfsde-si-k-ngc5/
Topic: 130. Surrounded areas
Reverse Thinking
Title: 797. All Possible Paths
Directed acyclic graph, memorized together with the above
-
recursion/backtracking
Title: 78. Subsets
Title: 90. Subset II
Subset problem:
- Backtracking method, increase the condition as the length of the path, add a valid answer after the length is satisfied, and return
- binary mask
Contains duplicate values: sort the array, de-duplicate processing (the last one is not selected, and the current one is the same as the previous one, skip it)
(Unplanned) Topic: 46. Full Arrangement
Title: 47. Full Arrangement II
Arrangement problem:
- Backtracking method, the most direct and classic solution, but it is not easy to deal with the situation where there are repeated elements
- Exchange method, taking K God as an example
With duplicate values:
- (For the backtracking method) sorting, using dictionaries and element judgments at the same time, the judgment here is more complicated and difficult to understand
- (for the exchange method) only dictionary processing is required
Backtracking method:
Specifically, we only need to set a rule in the recursive function to ensure that repeated characters will only be filled once when filling each blank. Specifically, we first sort the original string to ensure that the same characters are adjacent to each other. In the recursive function, we limit that the character filled in each time must be "the first unfilled character from left to right" in the repeated character set where this character is located, that is, the following judgment conditions :
if j in vis or (j-1 not in vis and j > 0 and nums[j] == nums[j-1]): continue
Here, the element cannot be hashed directly, only the subscript corresponding to the element can be hashed, and then judged:
class Solution: def permuteUnique(self, nums: List[int]) -> List[List[int]]: def backtrack(i): # 满足条件,这里不能用输入变量了 if len(path) == len(nums): res.append(path[:]) return # 要考虑所有的元素,而不是只考虑当前位置后面的元素,所以用for for j in range(len(nums)): # 上一个选择和本次重复 # 这里为什么是或? # j已经选择过 或者 j-1没选过但是j和j-1对应元素相等 if j in vis or (j-1 not in vis and j > 0 and nums[j] == nums[j-1]): continue vis.add(j) path.append(nums[j]) backtrack(j) path.pop() vis.remove(j) res = [] path = [] vis = set() nums.sort() backtrack(0) return res
Exchange method:
class Solution: def permuteUnique(self, nums: List[int]) -> List[List[int]]: # 交换法 def backtrack(i): if i == len(nums)-1: res.append(nums[:]) return dct = set() for j in range(i, len(nums)): # 先去重 if nums[j] in dct: continue dct.add(nums[j]) nums[i], nums[j] = nums[j], nums[i] backtrack(i+1) nums[i], nums[j] = nums[j], nums[i] res = [] backtrack(0) return res
The exchange method only needs a simple dictionary judgment, there are several differences:
- Dictionary initialization is not a global concept in backtracking; because it is only necessary to judge whether the position of this backtracking is repeated
- Dictionary elements are array elements, not subscript indexes
- No need to introduce additional paths, directly operate and add the original array to the answer
Topic: 39. Combinatorial sums
Title: 40. Combinatorial sums II
Combination questions feel more like subsets, but not all subsets, but conditional subsets
- Current element selection/unselection (suitable for reusing the same element), double branch
- Use a for loop to verify all of the following
Topic: 17. Alphabet combinations of telephone numbers
Topic: 22. Bracket generation (note that there can be multiple backtracking conditions)
Topic: 79. Word Search (Classic)
-
dynamic programming
(Unplanned) Topic: 198. Robbery
Title: 213. House Robbery II
The maximum value of 2 cases: If the first house is stolen, the last house cannot be stolen, so the range of houses stolen is from the first house to the last second house; if the last house is stolen, the first house cannot be stolen, so the range of houses stolen is from the second house to the last house.
Title: 55. Jumping Game
Typical greedy , remember the method; very similar to Byte’s summer internship written test questions (the title is called “Byte Beat”)
Title: 45. Jumping Game II (Continuing Greedy Strategy from I)
Title: 62. Different paths
Topic: 5. The longest palindrome substring (it's a bit strange to put it here, the optimal solution to the horse-drawn cart is success)
Topic: 413. Arithmetic Sequence Division
It is very similar to a certain question, I remember that question can not only consider the adjacent 3 elements
Title: 91. Decoding Methods
The current situation is equal to the addition of two situations, but both situations need to be judged
Title: 139. Word Splitting
Double loop type dp, the second loop judges whether a previous state is satisfied, that is, the current value can only be obtained when a certain small condition is satisfied (the section of the enumerated value is in the dictionary)
Title: 300. Longest Increasing Subsequence
Similar to the above question, the second cycle judges each situation, that is, the current value can only be obtained when a certain small condition is satisfied (the end value is greater than the enumerated value)
Title: 673. Number of Longest Increasing Subsequences
It is more difficult. On the basis of the above, both the small loop and the large loop update the number dp array
Title: 1143. Longest Common Subsequence
The public subsequence problem must be memorized!
Title: 583. Deletion of Two Strings (Problem Transformation)
Topic: 72. Edit distance (difficult question, so far not)
Topic: 322. Changing Change
In the same case
139
,300
double loops, the enumeration of the inner loop must have a relationship with the enumeration value of the outer loopTitle: 343. Integer Splitting
The problem of cutting the rope, both dp and greedy
-
bit operation
Topic: 201. Bitwise AND of ranges of numbers (longest common prefix)
-
other
Subject: 384. Shuffling Arrays
Topic: 202. Happy Numbers
Title: [149. The most points on a straight line](