leetcode

Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

LeetCode刷题分类⼿册

这是⼀本指导初学者刷题,并给出刷题顺序和分类的⼿册。
LeetCode是我们每个CS⼈都不得不面对的⼀道坎。但目前1400道LeetCode题⾜以“劝退”每⼀个想要
在北美找⼯转码的同学。在盲目开始之前,我们应该先思考如下⼏个问题:

刷多少题才够?
把⼀千多题题从头到尾刷完,对⼤多数⼈来说,是不现实也不值得提倡的,LeetCode题库相似题
型很多,刷题重在总结⽽不仅仅是数量,所以先给结论:
刷400道⾼频题完全⾜够。
要怎么刷才快?
接上面。400道题是否意味着从第1题刷到第400题呢,⽽⾼频题又是怎么定义的呢?

——这就是这本⼿册存在的意义。

⼀⾔以蔽之,刷题最重要的是要按分类刷,是要刷⾼频题,是要得出最优解,也要有⾼效率的重
复和⾼质量的总结。基于此,我们把 LeetCode做了⼀个分类:
1. 400题是我们从LeetCode1-1000题里挑选出来的⾼频(面试常出现)或有代表性(代表⼀整类题
型)的题目。
2. 对上述400题,本⼿册以tag为依据分⼤类,再以题型为依据分小类、旨在提供科学、⾼效的刷
题顺序。

关于我们
如果有同学想看到这些⾼频题的最优解讲解视频,请到www.flageek996.com购买后观看。
如果觉得直接上⼿LeetCode吃⼒,我们有算法专题课程(CS301A和CS301B)作为过渡;如果某⼀
块知识点是短板,那么专题系列课程(CS202/CS203/CS204/CS205)就是你的选择;对于刷题已经
过关的同学,简历修改服务(CS105)和模拟面试服务(CS502)也值得尝试。
这本刷题⼿册的内容会持续更新,题目也会不断补充。所有信息会统⼀在公众号和小助⼿朋友圈
发布,欢迎⼤家围观:

小助⼿微信:flageek996
公众号:FLAGeek频道

小助⼿微信 微信公众号

1
String
CS301B Chapter 2

14 Longest Common prefix


58 Length Of Last Word
387 First Unique Character in a String
383 Ransom Note
415 Add Strings
205 Isomorphic Strings
451 Sort Characters By Frequency
293 Flip Game
294 Flip Game ||
290 Word Pattern
249 Group Shifted Strings
161 One Edit Distance
819 Most Common Word
38 Count and Say
771 Jewels and Stones
316 Remove Duplicate Letters
168 Excel Sheet Column Title
171 Excel Sheet Column Number
13 Roman to Integer
12 Integer to Roman
246 Strobogrammatic Number
247 Strobogrammatic Number ||
248 Strobogrammatic Number |||
157 Read N Characters Given Read4
539 Minimum Time Difference
556 Next Greater Element |||
395 Longest Substring with At Least K Repeating Characters
419 Battleships in a Board
1055 Shortest Way to Form String
String类之翻转
344 Reverse String
151 Reverse Words in a String
186 Reverse Words in a String ||
557 Reverse Words in a String |||
345 Reverse Vowels of a String

2
CS301B Chapter 2

541 Reverse String ||


String类之回⽂类
125 Valid Palindrome
266 Palindrome Permutation
5 Longest Palindromic Substring
9 Palindrome Number
516 Longest Palindromic Subsequence
647 Palindromic Substrings
336 Palindrome Pairs
680 Valid Palindrome ||
String类之⼦序列
392 Is Subsetence
115 Distinct Subsequences
334 Increasing Triplet Subsequence
String类之括号类问题
20 Valid Parentheses
32 Longest Valid Parenthese
301 Remove Invalid Parentheses
241 Different Ways to Add Parentheses
String类之加密与解密
271 Encode and Decode Strings
443 String Compression
535 Encode and Decode TinyURL
394 Decode String
820 Short Encoding of Words
String类之Anagrams
242 Valid Anagram
49 Group Anagrams
438 Find All Anagrams in a String
String类之⼦集类问题
28 Implement strStr()
3 Longest Substring Without Repeating Characters
340 Longest Substring with At Most K Distinct Characters
395 Longest Substring with At Least K Repeating Characters
159 Longest Substring with At Most Two Distinct Characters
76 Minimum Window Substring

3
CS301B Chapter 2

巩固练习
1004 Max Consecutive Ones |||
811 Subdomain Visit Count
761 Special Binary String
981 Time Based Key-Value Store

4
Binary Search
CS301B Chapter 1

704 Binary Search


278 First Bad Version
35 Search Insert Position
33 Search in Rotated Sorted Array
81 Search in Rotated Sorted Array
153 Find Minimum in Rotated Sorted Array
154 Find Minimum in Rotated Sorted Array ||
528 Random Pick with Weight
162 Find Peak Element
34 Search for a Range
349 Intersection of Two Arrays
350 Intersection of Two Arrays ||
410 Split Array Largest Sum
658 Find K Closest Elements
1060 Missing Element in Sorted Array
1011 Capacity To Ship Packages Within D Days
315 Count of Smaller Numbers After Self
875 Koko Eating Bananas

5
LinkedList
CS301A Chapter 3 & CS301B Chapter 3

876 Middle of the Linked List


2 Add Two Numbers
445 Add Two Numbers ||
21 Merge Two Sorted Lists
369 Plus One Linked List
160 Intersection of Two Linked Lists
24 Swap Nodes in Pairs
328 Odd Even Linked List
206 Reverse Linked List
92 Reverse Linked List ||
141 Linked List Cycle
61 Rotate List
19 Remove Nth Node from End of List
708 Insert into a Sorted Circular Linked List
234 Palindrome Linked List
138 Copy List with Random Pointer
237 Delete Node in a Linked List
86 Partition List
430 Flatten a Multilevel Doubly Linked List
23 Merge K Sorted Lists
25 Reverse Nodes in k-Group

6
Matrix

48 Rotate Image
54 Spiral Matrix
59 Spiral Matrix ||
74 Search a 2D Matrix
240 Search a 2D Matrix ||
498 Diagonal Traverse
311 Sparse Matrix Multiplication
79 Word Search
36 Valid Sudoku
37 Sudoku Solver

7
Math

1 Two sum
167 Two Sum II - Input array is sorted
15 3Sum
16 3Sum Closest
259 3Sum Smaller
18 4Sum
560 Subarray Sum Equals K
525 Contiguous Array
523 Continuous Subarray Sum
412 Fizz Buzz
7 Reverse Integer
165 Compare Version Numbers
66 Plus One
8 String to Integer (atoi)
258 Add Digits
67 Add Binary
43 Multiply Strings
29 Divide Two Integers
69 Sqrt(x)
50 Pow(x,n)
204 Count Primes
836 Rectangle Overlap
414 Third Maximum Number
593 Valid Square
892 Surface Area of 3D Shapes
390 Elimination Game
628 Maximum Product of Three Numbers
780 Reaching Points
829 Consecutive Numbers Sum
991 Broken Calculator
1041 Robot Bounded In Circle

8
Array
CS301A Chapter 2

27 Remove Element
26 Remove Duplicate from Sorted Array
80 Remove Duplicate from Sorted Array ||
316 Remove Duplicate Letters
442 Find All Duplicates in an Array
448 Find All Numbers Disappeared in an Array
325 Maximum Size Subarray Sum Equals k
581 Shortest Unsorted Continuous Subarray
277 Find the Celebrity
189 Rotate Array
384 Shuffle an Array
hard41 First Missing Positive
299 Bulls and Cows
845 Longest Mountain in Array
134 Gas Station
118 Pascal’s Triangle
119 Pascal’s Triangle ||
169 Majority Element
229 Majority Element ||
274 H-Index
275 H-Index ||
243 Shortest Word Distance
244 Shortest Word Distance ||
245 Shortest Word Distance |||
217 Contains Duplicate
219 Contains Duplicate ||
220 Contains Duplicate |||
55 Jump Game
45 Jump Game ||
307 Range Sum Query - Mutable
308 Range Sum Query 2D - Mutable
121 Best Time to Buy and Sell Stock
122 Best Time to Buy and Sell Stock ||
123 Best Time to Buy and Sell Stock |||
188 Best Time to Buy and Sell Stock IV

9
CS301A Chapter 2

309 Best Time to Buy and Sell Stock with Cooldown


11 Container With Most Water
42 Trapping Rain Water
334 Increasing Triplet Subsequence
128 Longest Consecutive Sequence
287 Find the Duplicate Number
611 Valid Triangle Number
239 Sliding Window Maximum
295 Find Median from Data Stream
53 Maximum Subarray
325 Maximum Size Subarray Sum Equals k
209 Minimum Size Subarray Sum
238 Product of Array Except Self
152 Maximum Product Subarray
228 Summary Ranges
巩固练习
532 K-diff Pairs in an Array
713 Subarray Product Less Than K
974 Subarray Sums Divisible by K
945 Minimum Increment to Make Array Unique
406 Queue Reconstruction by Height
621 Task Scheduler
321 Create Maximum Number
403 Frog Jump
289 Game of Life
1029 Two City Scheduling
1052 Grumpy Bookstore Owner
755 Pour Water
986 Interval List Intersections
953 Verifying an Alien Dictionaryy
939 Minimum Area Rectangle
733 Flood Fill
697 Degree of an Array
723 Candy Crush
957 Prison Cells After N Days
1031 Maximum Sum of Two Non-Overlapping Subarrays
1007 Minimum Domino Rotations For Equal Row

10
CS301A Chapter 2

1167 Minimum Cost to Connect Sticks


1010 Pairs of Songs With Total Durations Divisible by 60
4 Median of Two Sorted Arrays
Array类之Interval
56 Merge Intervals
252 Meeting Rooms
253 Meeting Rooms ||
57 Insert Interval
Array类之Sort
88 Merge Sorted Array
75 Sort Colors
283 Move Zeros
280 Wiggle Sort
324 Wiggle Sort ||

11
Tree
CS301A Chapter 6 & CS301B Chapter 9

94 Binary Tree Inorder Traversal


95 Unique Binary Search Trees ||
144 Binary Tree Preorder Traversal
145 Binary Tree Postorder Traversal
314 Binary Tree Vertical Order Tradersal
366 Find Leaves of Binary Tree
333 Largest BST Subtree
Tree类之“⼈字形”(第⼀类)
104 Maximum Depth of Binary Tree
111 Minimum Depth of Binary Tree
543 Diameter of Binary Tree
572 Subtree of Another Tree
110 Balanced Binary Tree
250 Count Univalue Subtrees
124 Binary Tree Maximum Path Sum
235 Lowest Common Ancestor of a Binary Search Tree
236 Lowest Common Ancestor of a Binary Tree
98 Validate Binary Search Tree
270 Closest Binary Search Tree Value
Tree类之“前缀路径问题”(第⼆类)
112 Path Sum
113 Path Sum ||
437 Path Sum |||
129 Sum Root to Leaf Numbers
257 Binary Tree Paths
298 Binary Tree Longest Consecutive Sequence
Tree类之“BST中的Inorder,Successor, Predecessor问题”(第三类)
285 Inorder Successor in BST
510 Inorder Successor in BST ||
272 Closest Binary Search Tree Value ||
230 Kth Smallest Element in a BST
173 Binary Search Tree Iterator
450 Delete a Node in a BST
99 Recover Binary Search Tree
114 Flatten Binary Tree to Linked List

12
CS301A Chapter 6 & CS301B Chapter 9

Tree类之“序列化Serialization”(第四类)
105 Construct Binary Tree from Preorder and Inorder Traversal
106 Construct Binary Tree from Inorder and Postorder Traversal
538 Convert BST to Greater Tree
108 Convert Sorted Array to Binary Search Tree
109 Convert Sorted List to Binary Search Tree
297 Serialize and Deserialize Binary Tree
Tree类之“去序列化Deserialization”(第五类)
449 Serialize and Deserialize Binary Search Tree
428 Serialize and Deserialize N-ary Tree
Tree类之“Level Order”(第六类)
107 Binary Tree Level Order Traversal ||
103 Binary Tree Zigzag Level Order Traversal
199 Binary Tree Right Side View
222 Count Complete Tree Nodes
102 Binary Tree Level Order Traversal
巩固练习
100 Same Tree
101 Symmetric Tree
226 Invert Binary Tree
255 Verify Preorder Sequence in Binary Search Tree
116 Populating Next Right Pointers in Each Node
117 Populating Next Right Pointers in Each Node ||
426 Convert Binary Search Tree to Sorted Doubly Linked List
958 Check Completeness of a Binary Tree
637 Average of Levels in Binary Tree
96 Unique Binary Search Trees
1130 Minimum Cost Tree From Leaf Values
1110 Delete Nodes And Return Forest
987 Vertical Order Traversal of a Binary Tree
545 Boundary of Binary Tree
742 Closest Leaf in a Binary Tree
156 Interleaving String

13
DFS & BFS
CS301B Chapter 5

DFS类之“0/1 DFS” (第⼀类)


78 Subsets
77 Combinations
40 Combination Sum ||
216 Combination Sum |||
DFS类之“多叉DFS” (第⼆类)
39 Combination Sum
254 Factor Combinations
322 Coin Change
90 Subsets ||
DFS类之“Selecting DFS” (第三类)
46 Permutations
47 Permutations ||
31 Next Permutation
DFS类之“连通分量搜索” (第四类)
200 Number of Islands
305 Number of Islands ||
286 Walls and Gates
130 Surrounded Regions
695 Max Area of Island
巩固练习
547 Friend Circles
529 Mine Sweeper
306 Additive Number
337 House Robber |||
425 Word Squares
339 Nested List Weight Sum
341 Flatten Nested List Iterator
364 Nested List Weight Sum ||
51 N-Queens
79 Word Search
140 Word Break ||
131 Palindrome Partitioning
267 Palindrome Partitioning ||

14
CS301B Chapter 5

17 Letter Combinations of a Phone Number


93 Restore IP Addresses
694 Number of Distinct Islands
698 Partition to K Equal Sum Subsets
863 All Nodes Distance K in Binary Tree
721 Accounts Merge
797 All Paths From Source to Target
1096 Brace Expansion ||
329 Longest Increasing Path in a Matrix
351 Android Unlock Patterns
212 Word Search ||
BFS CS301B Chapter 5
785 Is Graph Bipartite?
958 Check Completeness of a Binary Tree
127 Word Ladder
126 Word Ladder ||
210 Course Schedule ||
490 The Maze
784 Letter Case Permutation
417 Pacific Atlantic Water Flow
317 Shortest Distance from All Buildings
909 Snakes and Ladders
994 Rotting Oranges

15
Stack
CS301A Chapter 5 & CS301B Chapter 7

225 Implement Stack using Queues


232 Implement Queue using stacks
150 Evaluate Reverse Polish Notation
155 Min Stack
716 Max Stack
636 Exclusive Time of Functions
1047 Remove All Adjacent Duplicates in String
402 Remove K Digits
71 Simplify Path
224 Basic Calculator
227 Basic Calculator ||
739 Daily Temperatures
735 Asteroid Collision
772 Basic Calculator |||

16
PriorityQueue
CS301B Chapter 7

332 Reconstruct Itinerary


347 Top K Frequent Elements
692 Top K Frequent Words
973 K Closest Points to Origin
373 Find K Pairs with Smallest Sums
215 Kth Largest Element in an Array
218 The Skyline Problem
414 Third Maximum Number
787 Cheapest Flights Within K Stops
378 Kth Smallest Element in a Sorted Matrix
759 Employee Free Time

17
DP
CS205 DP专题

62 Unique Paths
63 Unique Paths ||
70 Climbing Stairs
120 Triangle
321 Create Maximum Number
518 Coin Change 2
139 Word Break
279 Perfect Squares
64 Minimum Path Sum
256 Paint House
718 Maximum Length of Repeated Subarray
416 Partition Equal Subset Sum
304 Range Sum Query 2D - Immutable
486 Predict the Winner
312 Burst Balloons
72 Edit Distance
221 Maximal Square
85 Maximal Rectangle
276 Paint Fence
198 House Robber
213 House Robber ||
53 Maximum Subarray
152 Maximum Product Subarray
1027 Longest Arithmetic Sequence
1048 Longest String Chain
10 Regular Expression Matching
740 Delete and Earn
97 Interleaving String

18
Bit Manipulation

421 Maximum XOR of Two Numbers in an Array


89 Gray Code
190 Reverse Bits
191 Number of 1 Bits
201 Bitwise AND of Numbers Range

19
Design

380 Insert Delete GetRandom O(1)


346 Moving Average from Data Stream
622 Design Circular Queue
641 Design Circular Deque
705 Design HashSet
706 Design HashMap
353 Design Snake Game
348 Design Tic-Tac-Toe
362 Design Hit Counter
379 Design Phone Directory
381 Insert Delete GetRandom O(1) - Duplicates Allowed
359 Logger Rate Limiter
170 Two Sum ||| - Data structure Design
207 Course Schedule
208 Implement Trie(Prefix Tree)
211 Add and Search Word - Data structure design
251 Flatten 2D Vector
1188 Design Bounded Blocking Queue
472 Concatenated Words 2
1114 Print in Order
146 LRU Cache
460 LFU Cache
642 Design Search Autocomplete System
281 Zigzag Iterator
284 Peeking Iterator
269 Alien Dictionary

20
Graph

785 Is Graph Bipartite?


323 Number of Connected Components in an Undirected Graph
261 Graph Valid Tree
399 Evaluate Division

21

You might also like