LeetCode Solutions

05/27/2016 题解合集 Algorithm LeetCode OJ

近日闲来无事,基本把LeetCode刷了一遍。打算再闲的时候就写写题解,总结一下。因为题目较多,为了避免Leetcode题解泛滥于本博客,所以只用这一篇博客作为所有题解的入口,作为索引或目录页。

以下为刷过的题目列表

ID Title Acceptance Difficulty Editorial
1 Two Sum 23.8% Easy 查看题解
2 Add Two Numbers 23.4% Medium 查看题解
3 Longest Substring Without Repeating Characters 22.2% Medium 查看题解
4 Median of Two Sorted Arrays 18.8% Hard 查看题解
5 Longest Palindromic Substring 23.3% Medium 查看题解
6 ZigZag Conversion 24.2% Easy 查看题解
7 Reverse Integer 23.7% Easy 查看题解
8 String to Integer (atoi) 13.5% Easy 查看题解
9 Palindrome Number 32.0% Easy 查看题解
10 Regular Expression Matching 22.2% Hard 查看题解
11 Container With Most Water 34.8% Medium 查看题解
12 Integer to Roman 39.3% Medium 查看题解
13 Roman to Integer 39.9% Easy 查看题解
14 Longest Common Prefix 28.4% Easy 查看题解
15 3Sum 19.0% Medium 查看题解
16 3Sum Closest 29.3% Medium 查看题解
17 Letter Combinations of a Phone Number 29.0% Medium 查看题解
18 4Sum 24.0% Medium 查看题解
19 Remove Nth Node From End of List 29.8% Easy 查看题解
20 Valid Parentheses 29.7% Easy 查看题解
21 Merge Two Sorted Lists 35.7% Easy 查看题解
22 Generate Parentheses 37.4% Medium 查看题解
23 Merge k Sorted Lists 23.6% Hard 查看题解
24 Swap Nodes in Pairs 35.5% Easy 查看题解
25 Reverse Nodes in k-Group 27.8% Hard 查看题解
26 Remove Duplicates from Sorted Array 33.7% Easy 查看题解
27 Remove Element 34.2% Easy 查看题解
28 Implement strStr() 25.1% Easy 查看题解
29 Divide Two Integers 15.7% Medium 查看题解
30 Substring with Concatenation of All Words 20.9% Hard 查看题解
31 Next Permutation 26.7% Medium 查看题解
32 Longest Valid Parentheses 22.5% Hard 查看题解
33 Search in Rotated Sorted Array 30.5% Hard 查看题解
34 Search for a Range 29.3% Medium 查看题解
35 Search Insert Position 37.5% Medium 查看题解
36 Valid Sudoku 31.0% Easy 查看题解
37 Sudoku Solver 25.2% Hard 查看题解
38 Count and Say 29.4% Easy 查看题解
39 Combination Sum 31.3% Medium 查看题解
40 Combination Sum II 27.8% Medium 查看题解
41 First Missing Positive 24.0% Hard 查看题解
42 Trapping Rain Water 32.5% Hard 查看题解
43 Multiply Strings 23.7% Medium 查看题解
44 Wildcard Matching 17.6% Hard 查看题解
45 Jump Game II 25.4% Hard 查看题解
46 Permutations 36.1% Medium 查看题解
47 Permutations II 28.2% Medium 查看题解
48 Rotate Image 34.9% Medium 查看题解
49 Group Anagrams 27.9% Medium 查看题解
50 Pow(x, n) 27.9% Medium 查看题解
51 N-Queens 26.1% Hard 查看题解
52 N-Queens II 39.7% Hard 查看题解
53 Maximum Subarray 36.8% Medium 查看题解
54 Spiral Matrix 22.7% Medium 查看题解
55 Jump Game 28.4% Medium 查看题解
56 Merge Intervals 25.6% Hard 查看题解
57 Insert Interval 24.0% Hard 查看题解
58 Length of Last Word 29.5% Easy 查看题解
59 Spiral Matrix II 35.3% Medium 查看题解
60 Permutation Sequence 25.4% Medium 查看题解
61 Rotate List 23.0% Medium 查看题解
62 Unique Paths 36.6% Medium 查看题解
63 Unique Paths II 29.5% Medium 查看题解
64 Minimum Path Sum 35.1% Medium 查看题解
65 Valid Number 12.2% Hard 查看题解
66 Plus One 34.0% Easy 查看题解
67 Add Binary 27.8% Easy 查看题解
68 Text Justification 16.3% Hard 查看题解
69 Sqrt(x) 25.5% Medium 查看题解
70 Climbing Stairs 36.9% Easy 查看题解
71 Simplify Path 22.1% Medium 查看题解
72 Edit Distance 28.8% Hard 查看题解
73 Set Matrix Zeroes 33.6% Medium 查看题解
74 Search a 2D Matrix 34.0% Medium 查看题解
75 Sort Colors 34.9% Medium 查看题解
76 Minimum Window Substring 21.5% Hard 查看题解
77 Combinations 34.7% Medium 查看题解
78 Subsets 31.7% Medium 查看题解
79 Word Search 23.2% Medium 查看题解
80 Remove Duplicates from Sorted Array II 32.9% Medium 查看题解
81 Search in Rotated Sorted Array II 32.0% Medium 查看题解
82 Remove Duplicates from Sorted List II 27.1% Medium 查看题解
83 Remove Duplicates from Sorted List 37.0% Easy 查看题解
84 Largest Rectangle in Histogram 24.3% Hard 查看题解
85 Maximal Rectangle 23.7% Hard 查看题解
86 Partition List 29.7% Medium 查看题解
87 Scramble String 26.7% Hard 查看题解
88 Merge Sorted Array 30.2% Easy 查看题解
89 Gray Code 36.7% Medium 查看题解
90 Subsets II 30.8% Medium 查看题解
91 Decode Ways 17.7% Medium 查看题解
92 Reverse Linked List II 28.1% Medium 查看题解
93 Restore IP Addresses 23.7% Medium 查看题解
94 Binary Tree Inorder Traversal 40.0% Medium 查看题解
95 Unique Binary Search Trees II 29.1% Medium 查看题解
96 Unique Binary Search Trees 37.7% Medium 查看题解
97 Interleaving String 22.6% Hard 查看题解
98 Validate Binary Search Tree 21.1% Medium 查看题解
99 Recover Binary Search Tree 26.7% Hard 查看题解
100 Same Tree 43.5% Easy 查看题解
101 Symmetric Tree 34.1% Easy 查看题解
102 Binary Tree Level Order Traversal 33.1% Easy 查看题解
103 Binary Tree Zigzag Level Order Traversal 28.9% Medium 查看题解
104 Maximum Depth of Binary Tree 48.1% Easy 查看题解
105 Construct Binary Tree from Preorder and Inorder Traversal 28.9% Medium 查看题解
106 Construct Binary Tree from Inorder and Postorder Traversal 29.2% Medium 查看题解
107 Binary Tree Level Order Traversal II 34.2% Easy 查看题解
108 Convert Sorted Array to Binary Search Tree 37.4% Medium 查看题解
109 Convert Sorted List to Binary Search Tree 30.7% Medium 查看题解
110 Balanced Binary Tree 34.2% Easy 查看题解
111 Minimum Depth of Binary Tree 30.8% Easy 查看题解
112 Path Sum 31.5% Easy 查看题解
113 Path Sum II 28.5% Medium 查看题解
114 Flatten Binary Tree to Linked List 31.3% Medium 查看题解
115 Distinct Subsequences 29.0% Hard 查看题解
116 Populating Next Right Pointers in Each Node 36.6% Medium 查看题解
117 Populating Next Right Pointers in Each Node II 32.8% Hard 查看题解
118 Pascal's Triangle 33.7% Easy 查看题解
119 Pascal's Triangle II 32.6% Easy 查看题解
120 Triangle 30.4% Medium 查看题解
121 Best Time to Buy and Sell Stock 36.3% Easy 查看题解
122 Best Time to Buy and Sell Stock II 42.7% Medium 查看题解
123 Best Time to Buy and Sell Stock III 26.5% Hard 查看题解
124 Binary Tree Maximum Path Sum 23.5% Hard 查看题解
125 Valid Palindrome 24.0% Easy 查看题解
126 Word Ladder II 13.7% Hard 查看题解
127 Word Ladder 19.7% Medium 查看题解
128 Longest Consecutive Sequence 32.5% Hard 查看题解
129 Sum Root to Leaf Numbers 32.9% Medium 查看题解
130 Surrounded Regions 16.2% Medium 查看题解
131 Palindrome Partitioning 27.7% Medium 查看题解
132 Palindrome Partitioning II 21.9% Hard 查看题解
133 Clone Graph 24.9% Medium 查看题解
134 Gas Station 27.5% Medium 查看题解
135 Candy 22.6% Hard 查看题解
136 Single Number 50.1% Medium 查看题解
137 Single Number II 37.9% Medium 查看题解
138 Copy List with Random Pointer 26.1% Hard 查看题解
139 Word Break 25.6% Medium 查看题解
140 Word Break II 20.0% Hard 查看题解
141 Linked List Cycle 36.7% Easy 查看题解
142 Linked List Cycle II 31.4% Medium 查看题解
143 Reorder List 23.0% Medium 查看题解
144 Binary Tree Preorder Traversal 40.2% Medium 查看题解
145 Binary Tree Postorder Traversal 35.8% Hard 查看题解
146 LRU Cache 15.8% Hard 查看题解
147 Insertion Sort List 29.6% Medium 查看题解
148 Sort List 25.1% Medium 查看题解
149 Max Points on a Line 14.4% Hard 查看题解
150 Evaluate Reverse Polish Notation 23.8% Medium 查看题解
151 Reverse Words in a String 15.7% Medium 查看题解
152 Maximum Product Subarray 22.4% Medium 查看题解
153 Find Minimum in Rotated Sorted Array 36.5% Medium 查看题解
154 Find Minimum in Rotated Sorted Array II 34.5% Hard 查看题解
155 Min Stack 22.6% Easy 查看题解
160 Intersection of Two Linked Lists 30.4% Easy 查看题解
162 Find Peak Element 33.0% Medium 查看题解
164 Maximum Gap 26.9% Hard 查看题解
165 Compare Version Numbers 17.7% Easy 查看题解
166 Fraction to Recurring Decimal 15.3% Medium 查看题解
168 Excel Sheet Column Title 22.0% Easy 查看题解
169 Majority Element 41.7% Easy 查看题解
171 Excel Sheet Column Number 42.1% Easy 查看题解
172 Factorial Trailing Zeroes 33.0% Easy 查看题解
173 Binary Search Tree Iterator 35.0% Medium 查看题解
174 Dungeon Game 21.0% Hard 不日更新
179 Largest Number 19.3% Medium 查看题解
187 Repeated DNA Sequences 25.8% Medium 查看题解
188 Best Time to Buy and Sell Stock IV 22.5% Hard 查看题解
189 Rotate Array 21.5% Easy 查看题解
190 Reverse Bits 29.3% Easy 查看题解
191 Number of 1 Bits 37.5% Easy 查看题解
198 House Robber 34.7% Easy 查看题解
199 Binary Tree Right Side View 35.0% Medium 查看题解
200 Number of Islands 28.0% Medium 查看题解
201 Bitwise AND of Numbers Range 30.7% Medium 查看题解
202 Happy Number 36.9% Easy 查看题解
203 Remove Linked List Elements 29.0% Easy 查看题解
204 Count Primes 24.7% Easy 查看题解
205 Isomorphic Strings 30.0% Easy 查看题解
206 Reverse Linked List 40.0% Easy 查看题解
207 Course Schedule 27.3% Medium 查看题解
208 Implement Trie (Prefix Tree) 25.3% Medium 查看题解
209 Minimum Size Subarray Sum 27.2% Medium 查看题解
210 Course Schedule II 21.0% Medium 查看题解
211 Add and Search Word - Data structure design 20.2% Medium 查看题解
212 Word Search II 19.8% Hard 不日更新
213 House Robber II 31.0% Medium 查看题解
214 Shortest Palindrome 20.0% Hard 查看题解
215 Kth Largest Element in an Array 33.7% Medium 查看题解
216 Combination Sum III 36.3% Medium 查看题解
217 Contains Duplicate 41.6% Easy 查看题解
218 The Skyline Problem 22.7% Hard 查看题解
219 Contains Duplicate II 30.2% Easy 查看题解
220 Contains Duplicate III 18.7% Medium 查看题解
221 Maximal Square 23.8% Medium 查看题解
222 Count Complete Tree Nodes 25.3% Medium 查看题解
223 Rectangle Area 30.1% Easy 查看题解
224 Basic Calculator 22.6% Hard 查看题解
225 Implement Stack using Queues 30.6% Easy 查看题解
226 Invert Binary Tree 46.0% Easy 查看题解
227 Basic Calculator II 25.4% Medium 查看题解
228 Summary Ranges 24.6% Medium 查看题解
229 Majority Element II 25.8% Medium 查看题解
230 Kth Smallest Element in a BST 38.2% Medium 查看题解
231 Power of Two 37.1% Easy 查看题解
232 Implement Queue using Stacks 34.1% Easy 查看题解
233 Number of Digit One 24.9% Hard 查看题解
234 Palindrome Linked List 28.5% Easy 查看题解
235 Lowest Common Ancestor of a Binary Search Tree 37.6% Easy 查看题解
236 Lowest Common Ancestor of a Binary Tree 28.7% Medium 查看题解
237 Delete Node in a Linked List 43.9% Easy 查看题解
238 Product of Array Except Self 43.2% Hard 查看题解
239 Sliding Window Maximum 27.8% Hard 查看题解
240 Search a 2D Matrix II 34.7% Medium 查看题解
241 Different Ways to Add Parentheses 35.6% Medium 查看题解
242 Valid Anagram 42.2% Easy 查看题解
257 Binary Tree Paths 29.0% Easy 查看题解
258 Add Digits 48.7% Easy 查看题解
260 Single Number III 44.3% Medium 查看题解
263 Ugly Number 37.0% Easy 查看题解
264 Ugly Number II 28.6% Medium 查看题解
268 Missing Number 40.5% Medium 查看题解
273 Integer to English Words 18.9% Hard 查看题解
274 H-Index 29.7% Medium 查看题解
275 H-Index II 32.7% Medium 查看题解
278 First Bad Version 22.6% Easy 查看题解
279 Perfect Squares 33.0% Medium 查看题解
282 Expression Add Operators 25.3% Hard 查看题解
283 Move Zeroes 44.6% Easy 查看题解
284 Peeking Iterator 33.7% Medium 查看题解
287 Find the Duplicate Number 39.1% Hard 查看题解
289 Game of Life 34.7% Medium 查看题解
290 Word Pattern 29.6% Easy 查看题解
292 Nim Game 53.2% Easy 查看题解
295 Find Median from Data Stream 22.2% Hard 查看题解
297 Serialize and Deserialize Binary Tree 28.1% Hard 查看题解
299 Bulls and Cows 30.0% Easy 查看题解
300 Longest Increasing Subsequence 34.5% Medium 查看题解
301 Remove Invalid Parentheses 32.2% Hard 不日更新
303 Range Sum Query - Immutable 24.7% Easy 查看题解
304 Range Sum Query 2D - Immutable 22.2% Medium 查看题解
306 Additive Number 25.7% Medium 查看题解
307 Range Sum Query - Mutable 17.6% Medium 查看题解
309 Best Time to Buy and Sell Stock with Cooldown 36.9% Medium 查看题解
310 Minimum Height Trees 26.9% Medium 查看题解
312 Burst Balloons 37.3% Hard 查看题解
313 Super Ugly Number 34.8% Medium 查看题解
315 Count of Smaller Numbers After Self 31.1% Hard 不日更新
316 Remove Duplicate Letters 25.3% Hard 查看题解
318 Maximum Product of Word Lengths 39.9% Medium 查看题解
319 Bulb Switcher 40.9% Medium 查看题解
321 Create Maximum Number 21.1% Hard 查看题解
322 Coin Change 25.0% Medium 查看题解
324 Wiggle Sort II 22.5% Medium 查看题解
326 Power of Three 37.1% Easy 查看题解
327 Count of Range Sum 26.9% Hard 不日更新
328 Odd Even Linked List 38.5% Medium 查看题解
329 Longest Increasing Path in a Matrix 32.0% Hard 查看题解
330 Patching Array 29.5% Medium 查看题解
331 Verify Preorder Serialization of a Binary Tree 31.9% Medium 查看题解
332 Reconstruct Itinerary 24.3% Medium 查看题解
334 Increasing Triplet Subsequence 33.9% Medium 查看题解
335 Self Crossing 19.9% Hard 不日更新
336 Palindrome Pairs 20.0% Hard 不日更新
337 House Robber III 37.4% Medium 查看题解
338 Counting Bits 56.4% Medium 查看题解
341 Flatten Nested List Iterator 27.2% Medium 查看题解
342 Power of Four 33.8% Easy 查看题解
343 Integer Break 40.6% Medium 查看题解
344 Reverse String 58.4% Easy 查看题解
345 Reverse Vowels of a String 35.2% Easy 查看题解
347 Top K Frequent Elements 41.9% Medium 查看题解
349 Intersection of Two Arrays 45.0% Easy 查看题解
350 Intersection of Two Arrays II 41.2% Easy 查看题解