Skip to content

Latest commit

History

4,201 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

The number of LeetCode questions is increasing every week. For more questions and solutions, you can see my LintCode repository. I'll keep updating for full summary and better solutions. Stay tuned for updates. (Notes: "📖" means you need to subscribe to LeetCode premium membership for the access to premium questions.)

Algorithms

Database

Shell

Bit Manipulation

#TitleSolutionTimeSpaceDifficultyTagNote
136Single NumberC++PythonO(n)O(1)Easy
137Single Number IIC++PythonO(n)O(1)Medium
190Reverse BitsC++PythonO(1)O(1)Easy
191Number of 1 BitsC++PythonO(1)O(1)Easy
201Bitwise AND of Numbers RangeC++PythonO(1)O(1)Medium
231Power of TwoC++PythonO(1)O(1)EasyLintCode
260Single Number IIIC++PythonO(n)O(1)Medium
268Missing NumberC++PythonO(n)O(1)MediumLintCode
318Maximum Product of Word LengthsC++PythonO(n) ~ O(n^2)O(n)MediumBit Manipulation, Counting Sort, Pruning
342Power of FourC++PythonO(1)O(1)Easy
371Sum of Two IntegersC++PythonO(1)O(1)EasyLintCode
389Find the DifferenceC++PythonO(n)O(1)Easy
393UTF-8 ValidationC++PythonO(n)O(1)Medium
401Binary WatchC++PythonO(1)O(1)Easy
411Minimum Unique Word AbbreviationC++PythonO(2^n)O(n)Hard📖
421Maximum XOR of Two Numbers in an ArrayC++PythonO(n)O(1)Medium
461Hamming DistanceC++PythonO(1)O(1)Easy
462Minimum Moves to Equal Array Elements IIC++PythonO(n) on averageO(1)Medium
477Total Hamming DistanceC++PythonO(n)O(1)Medium
645Set MismatchC++PythonO(n)O(1)Easy

Array

#TitleSolutionTimeSpaceDifficultyTagNote
153 SumC++PythonO(n^2)O(1)MediumTwo Pointers
163 Sum ClosestC++PythonO(n^2)O(1)MediumTwo Pointers
184 SumC++PythonO(n^3)O(1)MediumTwo Pointers
26Remove Duplicates from Sorted ArrayC++PythonO(n)O(1)EasyTwo Pointers
27Remove ElementC++PythonO(n)O(1)Easy
31Next PermutationC++PythonO(n)O(1)MediumTricky
41First Missing PositiveC++PythonO(n)O(1)HardTricky
48Rotate ImageC++PythonO(n^2)O(1)Medium
54Spiral MatrixC++PythonO(m * n)O(1)Medium
59Spiral Matrix IIC++PythonO(n^2)O(1)Medium
66Plus OneC++PythonO(n)O(1)Easy
73Set Matrix ZeroesC++PythonO(m * n)O(1)Medium
80Remove Duplicates from Sorted Array IIC++PythonO(n)O(1)MediumTwo Pointers
118Pascal's TriangleC++PythonO(n^2)O(1)Easy
119Pascal's Triangle IIC++PythonO(n^2)O(1)Easy
121Best Time to Buy and Sell StockC++PythonO(n)O(1)Easy
128Longest Consecutive SequenceC++PythonO(n)O(n)HardTricky
157Read N Characters Given Read4C++PythonO(n)O(1)Easy📖
158Read N Characters Given Read4 II - Call multiple timesC++PythonO(n)O(1)Hard📖
163Missing RangesC++PythonO(n)O(1)Medium📖
169Majority ElementC++PythonO(n)O(1)Easy
189Rotate ArrayC++PythonO(n)O(1)Easy
209Minimum Size Subarray SumC++PythonO(n)O(1)MediumBinary Search
215Kth Largest Element in an ArrayC++PythonO(n) ~ O(n^2)O(1)MediumEPI
228Summary RangesC++PythonO(n)O(1)Medium
229Majority Element IIC++PythonO(n)O(1)Medium
238Product of Array Except SelfC++PythonO(n)O(1)MediumLintCode
240Search a 2D Matrix IIC++PythonO(m + n)O(1)MediumEPI, LintCode
243Shortest Word DistanceC++PythonO(n)O(1)Easy📖
245Shortest Word Distance IIIC++PythonO(n)O(1)Medium📖
251Flatten 2D VectorC++PythonO(1)O(1)Medium📖
277Find the CelebrityC++PythonO(n)O(1)Medium📖, EPI
289Game of LifeC++PythonO(m * n)O(1)Medium
293Flip GameC++PythonO(n * (c+1))O(1)Easy📖
296Best Meeting PointC++PythonO(m * n)O(m + n)Hard📖
311Sparse Matrix MultiplicationC++PythonO(m * n * l)O(m * l)Medium📖
334Increasing Triplet SubsequenceC++PythonO(n)O(1)Medium
370Range AdditionC++PythonO(k + n)O(1)Medium📖
384Shuffle an ArrayC++PythonO(n)O(n)MediumEPI
396Rotate FunctionC++PythonO(n)O(1)Easy
412Fizz BuzzC++PythonO(n)O(1)Easy
414Third Maximum NumberC++PythonO(n)O(1)Easy
419Battleships in a BoardC++PythonO(m * n)O(1)Medium
422Valid Word SquareC++PythonO(m * n)O(1)Easy📖
442Find All Duplicates in an ArrayC++PythonO(n)O(1)Medium
448Find All Numbers Disappeared in an ArrayC++PythonO(n)O(1)Easy
661Image SmootherC++PythonO(m * n)O(1)Easy
665Non-decreasing ArrayC++PythonO(n)O(1)Easy
667Beautiful Arrangement IIC++PythonO(n)O(1)Medium
670Maximum SwapC++PythonO(logn)O(logn)Medium
674Longest Continuous Increasing SubsequenceC++PythonO(n)O(1)Easy

String

#TitleSolutionTimeSpaceDifficultyTagNote
5Longest Palindromic SubstringC++PythonO(n)O(n)MediumManacher's Algorithm
6ZigZag ConversionC++PythonO(n)O(1)Easy
8String to Integer (atoi)C++PythonO(n)O(1)Easy
14Longest Common PrefixC++PythonO(n * k)O(1)Easy
28Implement strStr()C++PythonO(n + k)O(k)EasyKMP Algorithm
38Count and SayC++PythonO(n * 2^n)O(2^n)Easy
43Multiply StringsC++PythonO(m * n)O(m + n)Medium
58Length of Last WordC++PythonO(n)O(1)Easy
67Add BinaryC++PythonO(n)O(1)Easy
68Text JustificationC++PythonO(n)O(1)Hard
125Valid PalindromeC++PythonO(n)O(1)Easy
151Reverse Words in a StringC++PythonO(n)O(1)Medium
161One Edit DistanceC++PythonO(m + n)O(1)Medium📖
165Compare Version NumbersC++PythonO(n)O(1)Easy
186Reverse Words in a String IIC++PythonO(n)O(1)Medium📖
214Shortest PalindromeC++PythonO(n)O(n)HardKMP AlgorithmManacher's Algorithm
242Valid AnagramC++PythonO(n)O(1)EasyLintCode
271Encode and Decode StringsC++PythonO(n)O(1)Medium📖
273Integer to English WordsC++PythonO(logn)O(1)Hard
306Addictive NumberC++PythonO(n^3)O(n)Medium
383Ransom NoteC++PythonO(n)O(1)EasyEPI
405Convert a Number to HexadecimalC++PythonO(n)O(1)Easy
408Valid Word AbbreviationC++PythonO(n)O(1)Easy📖
415Add StringsC++PythonO(n)O(1)Easy
420Strong Password CheckerC++PythonO(n)O(1)Hard
434Number of Segments in a StringC++PythonO(n)O(1)Easy
459Repeated Substring PatternC++PythonO(n)O(n)EasyKMP Algorithm
468Validate IP AddressC++PythonO(1)O(1)Medium
556Next Greater Element IIIC++PythonO(1)O(1)Medium
557Reverse Words in a String IIIC++PythonO(n)O(1)Easy
647Palindromic SubstringsC++PythonO(n)O(n)MediumManacher's Algorithm
648Replace WordsC++PythonO(n)O(t)Mediumtrie
657Judge Route CircleC++PythonO(n)O(1)Easy
678Valid Parenthesis StringC++PythonO(n)O(1)Medium
680Valid Palindrome IIC++PythonO(n)O(1)Easy

Linked List

#TitleSolutionTimeSpaceDifficultyTagNote
2Add Two NumbersC++PythonO(n)O(1)Medium
21Merge Two Sorted ListsC++PythonO(n)O(1)Easy
23Merge k Sorted ListsC++PythonO(nlogk)O(1)HardHeap, Divide and Conquer
24Swap Nodes in PairsC++PythonO(n)O(1)Easy
25Reverse Nodes in k-GroupC++PythonO(n)O(1)Hard
61Rotate ListC++PythonO(n)O(1)Medium
82Remove Duplicates from Sorted List IIC++PythonO(n)O(1)Medium
83Remove Duplicates from Sorted ListC++PythonO(n)O(1)Easy
92Reverse Linked List IIC++PythonO(n)O(1)Medium
138Copy List with Random PointerC++PythonO(n)O(1)Hard
160Intersection of Two Linked ListsC++PythonO(m + n)O(1)Easy
203Remove Linked List ElementsC++PythonO(n)O(1)Easy
206Reverse Linked ListC++PythonO(n)O(1)Easy
234Palindrome Linked ListC++PythonO(n)O(1)Easy
237Delete Node in a Linked ListC++PythonO(1)O(1)EasyLintCode
328Odd Even Linked ListC++PythonO(n)O(1)Medium
369Plus One Linked ListC++PythonO(n)O(1)Medium📖Two Pointers
445Add Two Numbers IIC++PythonO(m + n)O(m + n)Medium

Stack

#TitleSolutionTimeSpaceDifficultyTagNote
20Valid ParenthesesC++PythonO(n)O(n)Easy
32Longest Valid ParenthesesC++PythonO(n)O(1)Hard
71Simplify PathC++PythonO(n)O(n)Medium
84Largest Rectangle in HistogramC++PythonO(n)O(n)HardAscending Stack, DP
85Maximal RectangleC++PythonO(m * n)O(n)HardEPIAscending Stack
101Symmetric TreeC++PythonO(n)O(h)Easy
150Evaluate Reverse Polish NotationC++PythonO(n)O(n)Medium
155Min StackC++PythonO(n)O(1)Easy
173Binary Search Tree IteratorC++PythonO(1)O(h)Medium
224Basic CalculatorC++PythonO(n)O(n)Hard
227Basic Calculator IIC++PythonO(n)O(n)Medium
232Implement Queue using StacksC++PythonO(1), amortizedO(n)EasyEPI, LintCode
255Verify Preorder Sequence in Binary Search TreeC++PythonO(n)O(1)Medium📖
272Closest Binary Search Tree Value IIC++PythonO(h + k)O(h)Hard📖
331Verify Preorder Serialization of a Binary TreeC++PythonO(n)O(1)Medium
341Flatten Nested List IteratorC++PythonO(n)O(h)Medium📖Iterator
385Mini ParserC++PythonO(n)O(h)Medium
394Decode StringC++PythonO(n)O(h)Medium
439Ternary Expression ParserC++PythonO(n)O(1)Medium📖
456132 PatternC++PythonO(n)O(n)Medium

Queue

#TitleSolutionTimeSpaceDifficultyTagNote
239Sliding Window MaximumC++PythonO(n)O(k)HardEPI, LintCode
281Zigzag IteratorC++PythonO(n)O(k)Medium📖
346Moving Average from Data StreamC++PythonO(1)O(w)Easy📖

Heap

#TitleSolutionTimeSpaceDifficultyTagNote
264Ugly Number IIC++PythonO(n)O(1)MediumCTCI, LintCodeBST, Heap
295Find Median from Data StreamC++PythonO(nlogn)O(n)HardEPI, LintCodeBST, Heap
313Super Ugly NumberC++PythonO(n * k)O(n + k)MediumBST, Heap
358Rearrange String k Distance ApartC++PythonO(n)O(n)Hard📖Greedy, Heap
373Find K Pairs with Smallest SumsC++PythonO(k * log(min(n, m, k)))O(min(n, m, k))Medium
378Kth Smallest Element in a Sorted MatrixC++PythonO(k * log(min(n, m, k)))O(min(n, m, k))MediumLintCode
407Trapping Rain Water IIC++PythonO(m * n * (logm + logn))O(m * n)HardLintCode

Tree

#TitleSolutionTimeSpaceDifficultyTagNote
94Binary Tree Inorder TraversalC++PythonO(n)O(1)MediumMorris Traversal
99Recover Binary Search TreeC++PythonO(n)O(1)HardMorris Traversal
144Binary Tree Preorder TraversalC++PythonO(n)O(1)MediumMorris Traversal
145Binary Tree Postorder TraversalC++PythonO(n)O(1)HardMorris Traversal
208Implement Trie (Prefix Tree)C++PythonO(n)O(1)MediumTrie
211Add and Search Word - Data structure designC++PythonO(min(n, h))O(min(n, h))MediumTrie, DFS
226Invert Binary TreeC++PythonO(n)O(h), O(w)Easy
297Serialize and Deserialize Binary TreeC++PythonO(n)O(h)HardLintCodeDFS
307Range Sum Query - MutableC++Pythonctor: O(n), update: O(logn), query: O(logn)O(n)MediumLintCodeDFS, Segment Tree, BIT
308Range Sum Query 2D - MutableC++Pythonctor: O(m * n), update: O(logm + logn), query: O(logm + logn)O(m * n)Hard📖DFS, Segment Tree, BIT
315Count of Smaller Numbers After SelfC++PythonO(nlogn)O(n)HardLintCodeBST, BIT, Divide and Conquer
652Find Duplicate SubtreesC++PythonO(n)O(n)MediumDFS, Hash
653Two Sum IV - Input is a BSTC++PythonO(n)O(h)EasyTwo Pointers
654Maximum Binary TreeC++PythonO(n)O(n)MediumLintCodeDescending Stack
655Print Binary TreeC++PythonO(n)O(h)Medium
662Maximum Width of Binary TreeC++PythonO(n)O(h)MediumDFS
663Equal Tree PartitionC++PythonO(n)O(n)Medium📖Hash
677Map Sum PairsC++PythonO(n)O(t)MediumTrie

Hash Table

#TitleSolutionTimeSpaceDifficultyTagNote
1Two SumC++PythonO(n)O(n)Easy
3Longest Substring Without Repeating CharactersC++PythonO(n)O(1)Medium
30Substring with Concatenation of All WordsC++PythonO(m * n * k)O(n * k)Hard
36Valid SudokuC++PythonO(9^2)O(9)Easy
49Group AnagramsC++PythonO(n * glogg)O(n)Medium
76Minimum Window SubstringC++PythonO(n)O(k)Hard
149Max Points on a LineC++PythonO(n^2)O(n)Hard
159Longest Substring with At Most Two Distinct CharactersC++PythonO(n)O(1)Hard📖
170Two Sum III - Data structure designC++PythonO(n)O(n)Easy📖
187Repeated DNA SequencesPythonO(n)O(n)Medium
202Happy NumberC++PythonO(k)O(k)Easy
204Count PrimesC++PythonO(n)O(n)Easy
205Isomorphic StringsC++PythonO(n)O(1)Easy
217Contains DuplicateC++PythonO(n)O(n)Easy
219Contains Duplicate IIC++PythonO(n)O(n)Easy
244Shortest Word Distance IIC++Pythonctor: O(n), lookup: O(a + b)O(n)Medium📖
246Strobogrammatic NumberC++PythonO(n)O(1)Easy📖
249Group Shifted StringsC++PythonO(nlogn)O(n)Easy📖
266Palindrome PermutationC++PythonO(n)O(1)Easy📖
288Unique Word AbbreviationC++Pythonctor: O(n), lookup: O(1)O(k)Easy📖
290Word PatternC++PythonO(n)O(c)Easyvariant of Isomorphic Strings
299Bulls and CowsC++PythonO(n)O(1)Easy
305Number of Islands IIC++PythonO(k)O(k)HardLintCode, 📖Union Find
314Binary Tree Vertical Order TraversalC++PythonO(n)O(n)Medium📖BFS
323Number of Connected Components in an Undirected GraphC++PythonO(n)O(n)Medium📖Union Find
325Maximum Size Subarray Sum Equals kC++PythonO(n)O(n)Medium📖
336Palindrome PairsC++PythonO(n * k^2)O(n * k)Hard
340Longest Substring with At Most K Distinct CharactersC++PythonO(n)O(1)Hard📖
356Line ReflectionC++PythonO(n)O(n)Medium📖Hash, Two Pointers
387First Unique Character in a StringC++PythonO(n)O(n)Easy
388Longest Absolute File PathC++PythonO(n)O(d)MediumStack
409Longest PalindromeC++PythonO(n)O(1)Easy
424Longest Repeating Character ReplacementC++PythonO(n)O(1)Medium
438Find All Anagrams in a StringC++PythonO(n)O(1)Easy
447Number of BoomerangsC++PythonO(n^2)O(n)Easy
4544Sum IIC++PythonO(n^2)O(n^2)Medium
473Matchsticks to SquareC++PythonO(n * s * 2^n)O(n * (2^n + s))Medium
554Brick WallC++PythonO(n)O(m)Medium

Math

#TitleSolutionTimeSpaceDifficultyTagNote
7Reverse IntegerC++PythonO(1)O(1)Easy
9Palindrome NumberC++PythonO(1)O(1)Easy
12Integer to RomanC++PythonO(n)O(1)Medium
13Roman to IntegerC++PythonO(n)O(1)Easy
29Divide Two IntegersC++PythonO(1)O(1)Medium
50Pow(x, n)C++PythonO(1)O(1)Medium
60Permutation SequenceC++PythonO(n^2)O(n)MediumCantor Ordering
65Valid NumberC++PythonO(n)O(1)HardAutomata
89Gray CodeC++PythonO(2^n)O(1)Medium
166Fraction to Recurring DecimalC++PythonO(logn)O(1)Medium
168Excel Sheet Column TitleC++PythonO(logn)O(1)Easy
171Excel Sheet Column NumberC++PythonO(n)O(1)Easy
172Factorial Trailing ZeroesC++PythonO(1)O(1)Easy
223Rectangle AreaC++PythonO(1)O(1)Easy
233Number of Digit OneC++PythonO(1)O(1)HardCTCI, LintCode
248Strobogrammatic Number IIIC++PythonO(5^(n/2))O(n)Hard📖
258Add DigitsC++PythonO(1)O(1)Easy
263Ugly NumberC++PythonO(1)O(1)Easy
292Nim GameC++PythonO(1)O(1)EasyLintCode
319Bulb SwitcherC++PythonO(1)O(1)Medium
326Power of ThreeC++PythonO(1)O(1)Easy
335Self CrossingC++PythonO(n)O(1)Hard
338Counting BitsC++PythonO(n)O(n)Medium
343Integer BreakC++PythonO(logn)O(1)MediumTricky, DP
365Water and Jug ProblemC++PythonO(logn)O(1)MediumEuclidean Algorithm
372Super PowC++PythonO(n)O(1)Medium
382Linked List Random NodeC++PythonO(n)O(1)MediumReservoir Sampling
386Lexicographical NumbersC++PythonO(n)O(1)Medium
390Elimination GameC++PythonO(logn)O(1)Medium
391Perfect RectangleC++PythonO(n)O(n)Hard
398Random Pick IndexC++PythonO(n)O(1)MediumReservoir Sampling
400Nth DigitC++PythonO(logn)O(1)Easy
413Arithmetic SlicesC++PythonO(n)O(1)Medium
423Reconstruct Original Digits from EnglishC++PythonO(n)O(1)MediumGCJ2016 - Round 1B
441Arranging CoinsC++PythonO(nlogn)O(1)EasyBinary Search
453Minimum Moves to Equal Array ElementsC++PythonO(n)O(1)Easy
458Poor PigsC++PythonO(n)O(1)Easy
469Convex PolygonC++PythonO(n)O(1)Medium📖
6514 Keys KeyboardC++PythonO(1)O(1)Medium📖Math, DP
660Remove 9C++PythonO(logn)O(1)Hard📖
672Bulb Switcher IIC++PythonO(1)O(1)Medium

Sort

#TitleSolutionTimeSpaceDifficultyTagNote
56Merge IntervalsC++PythonO(nlogn)O(1)Hard
57Insert IntervalC++PythonO(n)O(1)Hard
75Sort ColorsC++PythonO(n)O(1)MediumTri Partition
88Merge Sorted ArrayC++PythonO(n)O(1)Easy
147Insertion Sort ListC++PythonO(n^2)O(1)Medium
148Sort ListC++PythonO(nlogn)O(logn)Medium
164Maximum GapC++PythonO(n)O(n)HardTricky
179Largest NumberC++PythonO(nlogn)O(1)Medium
218The Skyline ProblemC++PythonO(nlogn)O(n)HardSort, BST
252Meeting RoomsC++PythonO(nlogn)O(n)Easy📖
253Meeting Rooms IIC++PythonO(nlogn)O(n)Medium📖
274H-IndexC++PythonO(n)O(n)MediumCounting Sort
280Wiggle SortC++PythonO(n)O(1)Medium📖
324Wiggle Sort IIC++PythonO(n) on averageO(1)Mediumvariant of Sort ColorsTri Partition
347Top K Frequent ElementsC++PythonO(n) on averageO(n)MediumQuick Select, Heap
406Queue Reconstruction by HeightC++PythonO(n * sqrt(n))O(n)Medium
451Sort Characters By FrequencyC++PythonO(n)O(n)Medium

Two Pointers

#TitleSolutionTimeSpaceDifficultyTagNote
19Remove Nth Node From End of ListC++PythonO(n)O(1)Easy
86Partition ListC++PythonO(n)O(1)Medium
141Linked List CycleC++PythonO(n)O(1)Easy
142Linked List Cycle IIC++PythonO(n)O(1)Medium
143Reorder ListC++PythonO(n)O(1)Medium
167Two Sum II - Input array is sortedC++PythonO(n)O(1)Medium
2593Sum SmallerC++PythonO(n^2)O(1)Medium📖, LintCode
283Move ZeroesC++PythonO(n)O(1)Easy
287Find the Duplicate NumberC++PythonO(n)O(1)HardBinary Search, Two Pointers
344Reverse StringC++PythonO(n)O(1)Easy
345Reverse Vowels of a StringC++PythonO(n)O(1)Easy
349Intersection of Two ArraysC++PythonO(m + n)O(min(m, n))EasyEPIHash, Binary Search
350Intersection of Two Arrays IIC++PythonO(m + n)O(1)EasyEPIHash, Binary Search
360Sort Transformed ArrayC++PythonO(n)O(1)Medium📖
457Circular Array LoopC++PythonO(n)O(1)Medium

Recursion

#TitleSolutionTimeSpaceDifficultyTagNote
95Unique Binary Search Trees IIC++PythonO(4^n / n^(3/2)O(4^n / n^(3/2)Medium
98Validate Binary Search TreeC++PythonO(n)O(1)Medium
100Same TreeC+PythonO(n)O(h)Easy
104Maximum Depth of Binary TreeC++PythonO(n)O(h)Easy
105Construct Binary Tree from Preorder and Inorder TraversalC++PythonO(n)O(n)Medium
106Construct Binary Tree from Inorder and Postorder TraversalC++PythonO(n)O(n)Medium
108Convert Sorted Array to Binary Search TreeC++PythonO(n)O(logn)Medium
109Convert Sorted List to Binary Search TreeC++PythonO(n)O(logn)Medium
110Balanced Binary TreePythonO(n)O(h)Easy
111Minimum Depth of Binary TreePythonO(n)O(h)Easy
114Flatten Binary Tree to Linked ListPythonO(n)O(h)Medium
116Populating Next Right Pointers in Each NodePythonO(n)O(1)Medium
124Binary Tree Maximum Path SumPythonO(n)O(h)Hard
129Sum Root to Leaf NumbersPythonO(n)O(h)Medium
156Binary Tree Upside DownPythonO(n)O(1)Medium📖
241Different Ways to Add ParenthesesC++PythonO(n * 4^n / n^(3/2))O(n * 4^n / n^(3/2))Medium
298Binary Tree Longest Consecutive SequenceC++PythonO(n)O(h)Medium📖
327Count of Range SumC++PythonO(nlogn)O(n)Hard
333Largest BST SubtreeC++PythonO(n)O(h)Medium📖
337House Robber IIIC++PythonO(n)O(h)Medium
395Longest Substring with At Least K Repeating CharactersC++PythonO(n)O(1)Medium
404Sum of Left LeavesC++PythonO(n)O(h)Easy
437Path Sum IIIC++PythonO(n)O(h)Easy
549Binary Tree Longest Consecutive Sequence IIC++PythonO(n)O(h)Medium📖
669Trim a Binary Search TreeC++PythonO(n)O(h)Easy
671Second Minimum Node In a Binary TreeC++PythonO(n)O(h)Easy

Binary Search

#TitleSolutionTimeSpaceDifficultyTagNote
4Median of Two Sorted ArraysC++PythonO(log(min(m, n)))O(1)Hard
33Search in Rotated Sorted ArrayC++PythonO(logn)O(1)Hard
34Search for a RangeC++PythonO(logn)O(1)Medium
35Search Insert PositionC++PythonO(logn)O(1)Medium
69Sqrt(x)C++PythonO(logn)O(1)Medium
74Search a 2D MatrixC++PythonO(logm + logn)O(1)Medium
81Search in Rotated Sorted Array IIC++PythonO(logn)O(1)Medium
153Find Minimum in Rotated Sorted ArrayC++PythonO(logn)O(1)Medium
154Find Minimum in Rotated Sorted Array IIC++PythonO(logn) ~ O(n)O(1)Hard
162Find Peak ElementC++PythonO(logn)O(1)Medium
222Count Complete Tree NodesC++PythonO((logn)^2)O(1)Medium
275H-Index IIC++PythonO(logn)O(1)MediumBinary Search
278First Bad VersionC++PythonO(logn)O(1)EasyLintCode
300Longest Increasing SubsequenceC++PythonO(nlogn)O(n)MediumCTCI, LintCodeBinary Search, DP
302Smallest Rectangle Enclosing Black PixelsC++PythonO(nlogn)O(1)Hard📖
354Russian Doll EnvelopesC++PythonO(nlogn)O(1)Hard
363Max Sum of Rectangle No Larger Than KC++PythonO(min(m, n)^2 * max(m, n) * logn(max(m, n)))O(max(m, n))Hard
367Valid Perfect SquareC++PythonO(logn)O(1)Medium
374Guess Number Higher or LowerC++PythonO(logn)O(1)Easy
410Split Array Largest SumC++PythonO(nlogs)O(1)Hard
436Find Right IntervalC++PythonO(nlogn)O(n)Medium
475HeatersC++PythonO((m + n) * logn)O(1)Easy
658Find K Closest ElementsC++PythonO(logn + k)O(1)Medium
668Kth Smallest Number in Multiplication TableC++PythonO(m * log(m * n))O(1)Hard

Binary Search Tree

#TitleSolutionTimeSpaceDifficultyTagNote
220Contains Duplicate IIIC++PythonO(nlogk)O(k)Medium
230Kth Smallest Element in a BSTC++PythonO(max(h, k))O(min(h, k))Medium
235Lowest Common Ancestor of a Binary Search TreeC++PythonO(h)O(1)EasyEPI
270Closest Binary Search Tree ValueC++PythonO(h)O(1)Easy📖
285Inorder Successor in BSTC++PythonO(h)O(1)Medium📖
352Data Stream as Disjoint IntervalsC++PythonO(logn)O(n)Hard
449Serialize and Deserialize BSTC++PythonO(n)O(h)Medium
450Delete Node in a BSTC++PythonO(h)O(h)Medium

Breadth-First Search

#TitleSolutionTimeSpaceDifficultyTagNote
102Binary Tree Level Order TraversalC++PythonO(n)O(n)Easy
107Binary Tree Level Order Traversal IIPythonO(n)O(n)Easy
103Binary Tree Zigzag Level Order TraversalPythonO(n)O(n)Medium
117Populating Next Right Pointers in Each Node IIPythonO(n)O(1)Hard
127Word LadderPythonO(n * d)O(d)Medium
130Surrounded RegionsC++PythonO(m * n)O(m + n)Medium
133Clone GraphPythonO(n)O(n)Medium
207Course SchedulePythonO(|V| + |E|)O(|E|)MediumTopological Sort
210Course Schedule IIPythonO(|V| + |E|)O(|E|)MediumTopological Sort
261Graph Valid TreeC++PythonO(|V| + |E|)O(|V| + |E|)Medium📖
269Alien DictionaryC++PythonO(n)O(1)Hard📖Topological Sort, BFS, DFS
286Walls and GatesC++PythonO(m * n)O(g)Medium📖
310Minimum Height TreesC++PythonO(n)O(n)Medium
317Shortest Distance from All BuildingsC++PythonO(k * m * n)O(m * n)Hard📖
433Minimum Genetic MutationC++PythonO(n * b)O(b)Medium
444Sequence ReconstructionC++PythonO(n * s)O(n)Medium📖Topological Sort
666Path Sum IVC++PythonO(n)O(w)Medium📖Topological Sort
675Cut Off Trees for Golf EventC++PythonO(t * m * n)O(m * n)HardA* Search Algorithm

Depth-First Search

#TitleSolutionTimeSpaceDifficultyTagNote
112Path SumPythonO(n)O(h)Easy
113Path Sum IIPythonO(n)O(h)Medium
199Binary Tree Right Side ViewPythonO(n)O(h)Medium
200Number of IslandsPythonO(m * n)O(m * n)Medium
236Lowest Common Ancestor of a Binary TreeC++PythonO(n)O(h)MediumEPI
247Strobogrammatic Number IIC++PythonO(n^2 * 5^(n/2))O(n)Medium📖
250Count Univalue SubtreesC++PythonO(n)O(h)Medium📖
257Binary Tree PathsC++PythonO(n * h)O(h)Easy
282Expression Add OperatorsC++PythonO(4^n)O(n)Hard
301Remove Invalid ParenthesesC++PythonO(C(n, c))O(c)Hard
329Longest Increasing Path in a MatrixC++PythonO(m * n)O(m * n)Hard
332Reconstruct ItineraryC++PythonO(t! / (n1! * n2! * ... nk!))O(t)Medium
339Nested List Weight SumC++PythonO(n)O(h)Easy📖
364Nested List Weight Sum IIC++PythonO(n)O(h)Medium📖
366Find Leaves of Binary TreeC++PythonO(n)O(h)Medium📖
399Evaluate DivisionC++PythonO(q * |V|!)O(e)Medium
417Pacific Atlantic Water FlowC++PythonO(m * n)O(m * n)Medium
440K-th Smallest in Lexicographical OrderC++PythonO(logn)O(logn)Hard
464Can I WinC++PythonO(n!)O(n)Medium

Backtracking

#TitleSolutionTimeSpaceDifficultyTagNote
17Letter Combinations of a Phone NumberPythonO(n * 4^n)O(n)Medium
22Generate ParenthesesPythonO(4^n / n^(3/2))O(n)Medium
37Sudoku SolverPythonO((9!)^9)O(1)Hard
39Combination SumPythonO(k * n^k)O(k)Medium
40Combination Sum IIPythonO(k * C(n, k))O(k)Medium
46PermutationsPythonO(n * n!)O(n)Medium
47Permutations IIPythonO(n * n!)O(n)Medium
51N-QueensPythonO(n!)O(n)Hard
52N-Queens-IIPythonO(n!)O(n)Hard
77CombinationsPythonO(n!)O(n)Medium
79Word SearchPythonO(m * n * l)O(l)Medium
93Restore IP AddressesPythonO(1)O(1)Medium
78SubsetsC++PythonO(n * 2^n)O(1)Medium
90Subsets IIC++PythonO(n * 2^n)O(1)Medium
126Word Ladder IIPythonO(n * d)O(d)Hard
131Palindrome PartitioningPythonO(n^2) ~ O(2^n)O(n^2)Medium
140Word Break IIC++PythonO(n * l^2 + n * r)O(n^2)Hard
212Word Search IIC++PythonO(m * n * l)O(l)HardLintCodeTrie, DFS
216Combination Sum IIIC++PythonO(k * C(n, k))O(k)Medium
254Factor CombinationsC++PythonO(nlogn)O(logn)Medium📖
267Palindrome Permutation IIC++PythonO(n * n!)O(n)Medium📖
291Word Pattern IIC++PythonO(n * C(n - 1, c - 1))O(n + c)Hard📖
294Flip Game IIC++PythonO(n + c^2)O(c)Medium📖DP, Hash
320Generalized AbbreviationC++PythonO(n * 2^n)O(n)Medium📖
425Word SquaresC++PythonO(n^2 * n!)O(n^2)Hard📖
676Implement Magic DictionaryC++PythonO(n)O(d)MediumTrie, DFS
67924 GameC++PythonO(1)O(1)HardDFS

Dynamic Programming

#TitleSolutionTimeSpaceDifficultyTagNote
10Regular Expression MatchingPythonO(m * n)O(n)Hard
53Maximum SubarrayPythonO(n)O(1)Medium
62Unique PathsPythonO(m * n)O(m + n)Medium
63Unique Paths IIPythonO(m * n)O(m + n)Medium
64Minimum Path SumPythonO(m * n)O(m + n)Medium
70Climbing StairsPythonO(n)O(1)Easy
72Edit DistancePythonO(m * n)O(m + n)Hard
87Scramble StringPythonO(n^4)O(n^3)Hard
91Decode WaysC++PythonO(n)O(1)Medium
96Unique Binary Search TreesPythonO(n)O(1)MediumMath
97Interleaving StringPythonO(m * n)O(m + n)Hard
115Distinct SubsequencesPythonO(n^2)O(n)Hard
120TrianglePythonO(m * n)O(n)Medium
123Best Time to Buy and Sell Stock IIIPythonO(n)O(1)Hard
132Palindrome Partitioning IIPythonO(n^2)O(n^2)Hard
139Word BreakC++PythonO(n * l^2)O(n)Medium
152Maximum Product SubarrayPythonO(n)O(1)Medium
174Dungeon GamePythonO(m * n)O(m + n)Hard
188Best Time to Buy and Sell Stock IVPythonO(k * n)O(k)Hard
198House RobberPythonO(n)O(1)Easy
213House Robber IIC++PythonO(n)O(1)Medium
221Maximal SquareC++PythonO(n^2)O(n)MediumEPI
256Paint HouseC++PythonO(n)O(1)Medium📖
265Paint House IIC++PythonO(n * k)O(k)Hard📖
276Paint FenceC++PythonO(n)O(1)Easy📖
279Perfect SquaresC++PythonO(n * sqrt(n))O(n)MediumHash
303Range Sum Query - ImmutableC++Pythonctor: O(n), lookup: O(1)O(n)Easy
304Range Sum Query 2D - ImmutableC++Pythonctor: O(m * n), lookup: O(1)O(m * n)Medium
309Best Time to Buy and Sell Stock with CooldownC++PythonO(n)O(1)Medium
312Burst BalloonsC++PythonO(n^3)O(n^2)Hard
322Coin ChangeC++PythonO(n * k)O(k)Medium
351Android Unlock PatternsC++PythonO(9^2 * 2^9)O(9 * 2^9)Medium📖Backtracking
357Count Numbers with Unique DigitsC++PythonO(n)O(1)MediumBacktracking, Math
361Bomb EnemyC++PythonO(m * n)O(m * n)Medium📖
368Largest Divisible SubsetC++PythonO(n^2)O(n)Medium
375Guess Number Higher or Lower IIC++PythonO(n^2)O(n^2)Medium
377Combination Sum IVC++PythonO(nlogn + n * t)O(t)Medium
403Frog JumpC++PythonO(n)O(n) ~ O(n^2)Hard
416Partition Equal Subset SumC++PythonO(n * s)O(s)Medium
418Sentence Screen FittingC++PythonO(r + n * c)O(n)Medium📖
446Arithmetic Slices II - SubsequenceC++PythonO(n^2)O(n * d)Hard
465Optimal Account BalancingC++PythonO(n * 2^n)O(n * 2^n)Hard📖
466Count The RepetitionsC++PythonO(s1 * min(s2, n1))O(s2)Hard
467Unique Substrings in Wraparound StringC++PythonO(n)O(1)Medium
471Encode String with Shortest LengthC++PythonO(n^3) on averageO(n^2)Medium📖
472Concatenated WordsC++PythonO(n * l^2)O(n * l)Medium
474Ones and ZeroesC++PythonO(s * m * n)O(m * n)Medium
6502 Keys KeyboardC++PythonO(sqrt(n))O(1)Medium
656Coin PathC++PythonO(n * B)O(n)Hard📖
664Strange PrinterC++PythonO(n^3)O(n^2)Hard
673Number of Longest Increasing SubsequenceC++PythonO(n^2)O(n)Medium

Greedy

#TitleSolutionTimeSpaceDifficultyTagNote
11Container With Most WaterC++PythonO(n)O(1)Medium
42Trapping Rain WaterC++PythonO(n)O(1)HardTricky
44Wildcard MatchingPythonO(m + n)O(1)HardTricky
45Jump Game IIPythonO(n)O(1)Hard
55Jump GamePythonO(n)O(1)Medium
122Best Time to Buy and Sell Stock IIPythonO(n)O(1)Medium
134Gas StationPythonO(n)O(1)Medium
135CandyC++PythonO(n)O(n)Hard
316Remove Duplicate LettersC++PythonO(n)O(k)HardAscending Stack
321Create Maximum NumberC++PythonO(k * (m + n + k)) ~ O(k * (m + n + k^2))O(m + n + k^2)Hardvariant of Delete DigitsGreedy, DP
330Patching ArrayC++PythonO(s + logn)O(1)Hard
376Wiggle SubsequenceC++PythonO(n)O(1)Medium
392Is SubsequenceC++PythonO(n)O(1)Medium
397Integer ReplacementC++PythonO(n)O(1)MediumMath
402Remove K DigitsC++PythonO(n)O(n)MediumLintCode
435Non-overlapping IntervalsC++PythonO(nlogn)O(1)Medium
452Minimum Number of Arrows to Burst BalloonsC++PythonO(nlogn)O(1)Medium
455Assign CookiesC++PythonO(nlogn)O(1)Easy
646Maximum Length of Pair ChainC++PythonO(nlogn)O(1)MediumScan Line
649Dota2 SenateC++PythonO(n)O(n)Medium
659Split Array into Consecutive SubsequencesC++PythonO(n)O(1)Medium

Design

#TitleSolutionTimeSpaceDifficultyTagNote
146LRU CacheC++PythonO(1)O(k)Hard
225Implement Stack using QueuesC++Pythonpush: O(n), pop: O(1), top: O(1)O(n)Easy
284Peeking IteratorC++PythonO(1)O(1)Medium
348Design Tic-Tac-ToeC++PythonO(1)O(n^2)Medium📖
353Design Snake GameC++PythonO(1)O(s)Medium📖Deque
355Design TwitterC++PythonO(klogu)O(t + f)MediumLintCodeHeap
359Logger Rate LimiterC++PythonO(1), amortizedO(k)Easy📖Deque
362Design Hit CounterC++PythonO(1), amortizedO(k)Medium📖Deque
379Design Phone DirectoryC++PythonO(1)O(n)Medium📖
380Insert Delete GetRandom O(1)C++PythonO(1)O(n)Hard
381Insert Delete GetRandom O(1) - Duplicates allowedC++PythonO(1)O(n)Hard
432All O`one Data StructureC++PythonO(1)O(n)Hard
460LFU CacheC++PythonO(1)O(k)Hard

SQL

#TitleSolutionTimeSpaceDifficultyTagNote
175Combine Two TablesMySQLO(m + n)O(m + n)Easy
176Second Highest SalaryMySQLO(n)O(1)Easy
177Nth Highest SalaryMySQLO(n^2)O(n)Medium
178Rank ScoresMySQLO(n^2)O(n)Medium
180Consecutive NumbersMySQLO(n)O(n)Medium
181Employees Earning More Than Their ManagersMySQLO(n^2)O(1)Easy
182Duplicate EmailsMySQLO(n^2)O(n)Easy
183Customers Who Never OrderMySQLO(n^2)O(1)Easy
184Department Highest SalaryMySQLO(n^2)O(n)Medium
185Department Top Three SalariesMySQLO(n^2)O(n)Hard
196Delete Duplicate EmailsMySQLO(n^2)O(n)Easy
197Rising TemperatureMySQLO(n^2)O(n)Easy
262Trips and UsersMySQLO((t * u) + tlogt)O(t)Hard

Shell Script

#TitleSolutionTimeSpaceDifficultyTagNote
192Word FrequencyShellO(n)O(k)Medium
193Valid Phone NumbersShellO(n)O(1)Easy
194Transpose FileShellO(n^2)O(n^2)Medium
195Tenth LineShellO(n)O(1)Easy

About

📝 Python / C++ 11 Solutions of LeetCode Questions

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { // Add copy buttons to all
 blocks
(function() {
function addCopyButtons() {
document.querySelectorAll('pre code').forEach(function(codeBlock) {
if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;
codeBlock.parentElement.setAttribute('data-copy-added', 'true');
var btn = document.createElement('button');
btn.textContent = 'Copy';
btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';
btn.onmouseover = function() { this.style.opacity = '1'; };
btn.onmouseout = function() { this.style.opacity = '0.7'; };
btn.onclick = function() {
navigator.clipboard.writeText(codeBlock.textContent).then(function() {
btn.textContent = 'Copied!';
setTimeout(function() { btn.textContent = 'Copy'; }, 1500);
});
};
codeBlock.parentElement.style.position = 'relative';
codeBlock.parentElement.appendChild(btn);
});
}
addCopyButtons();
// Re-run on dynamic content
var observer = new MutationObserver(addCopyButtons);
observer.observe(document.body, { childList: true, subtree: true });
})();
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
GitHub - shinan6/LeetCode: :pencil: Python / C++ 11 Solutions of LeetCode Questions · GitHub
Skip to content

Latest commit

History

4,201 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

The number of LeetCode questions is increasing every week. For more questions and solutions, you can see my LintCode repository. I'll keep updating for full summary and better solutions. Stay tuned for updates. (Notes: "📖" means you need to subscribe to LeetCode premium membership for the access to premium questions.)

Algorithms

Database

Shell

Bit Manipulation

#TitleSolutionTimeSpaceDifficultyTagNote
136Single NumberC++PythonO(n)O(1)Easy
137Single Number IIC++PythonO(n)O(1)Medium
190Reverse BitsC++PythonO(1)O(1)Easy
191Number of 1 BitsC++PythonO(1)O(1)Easy
201Bitwise AND of Numbers RangeC++PythonO(1)O(1)Medium
231Power of TwoC++PythonO(1)O(1)EasyLintCode
260Single Number IIIC++PythonO(n)O(1)Medium
268Missing NumberC++PythonO(n)O(1)MediumLintCode
318Maximum Product of Word LengthsC++PythonO(n) ~ O(n^2)O(n)MediumBit Manipulation, Counting Sort, Pruning
342Power of FourC++PythonO(1)O(1)Easy
371Sum of Two IntegersC++PythonO(1)O(1)EasyLintCode
389Find the DifferenceC++PythonO(n)O(1)Easy
393UTF-8 ValidationC++PythonO(n)O(1)Medium
401Binary WatchC++PythonO(1)O(1)Easy
411Minimum Unique Word AbbreviationC++PythonO(2^n)O(n)Hard📖
421Maximum XOR of Two Numbers in an ArrayC++PythonO(n)O(1)Medium
461Hamming DistanceC++PythonO(1)O(1)Easy
462Minimum Moves to Equal Array Elements IIC++PythonO(n) on averageO(1)Medium
477Total Hamming DistanceC++PythonO(n)O(1)Medium
645Set MismatchC++PythonO(n)O(1)Easy

Array

#TitleSolutionTimeSpaceDifficultyTagNote
153 SumC++PythonO(n^2)O(1)MediumTwo Pointers
163 Sum ClosestC++PythonO(n^2)O(1)MediumTwo Pointers
184 SumC++PythonO(n^3)O(1)MediumTwo Pointers
26Remove Duplicates from Sorted ArrayC++PythonO(n)O(1)EasyTwo Pointers
27Remove ElementC++PythonO(n)O(1)Easy
31Next PermutationC++PythonO(n)O(1)MediumTricky
41First Missing PositiveC++PythonO(n)O(1)HardTricky
48Rotate ImageC++PythonO(n^2)O(1)Medium
54Spiral MatrixC++PythonO(m * n)O(1)Medium
59Spiral Matrix IIC++PythonO(n^2)O(1)Medium
66Plus OneC++PythonO(n)O(1)Easy
73Set Matrix ZeroesC++PythonO(m * n)O(1)Medium
80Remove Duplicates from Sorted Array IIC++PythonO(n)O(1)MediumTwo Pointers
118Pascal's TriangleC++PythonO(n^2)O(1)Easy
119Pascal's Triangle IIC++PythonO(n^2)O(1)Easy
121Best Time to Buy and Sell StockC++PythonO(n)O(1)Easy
128Longest Consecutive SequenceC++PythonO(n)O(n)HardTricky
157Read N Characters Given Read4C++PythonO(n)O(1)Easy📖
158Read N Characters Given Read4 II - Call multiple timesC++PythonO(n)O(1)Hard📖
163Missing RangesC++PythonO(n)O(1)Medium📖
169Majority ElementC++PythonO(n)O(1)Easy
189Rotate ArrayC++PythonO(n)O(1)Easy
209Minimum Size Subarray SumC++PythonO(n)O(1)MediumBinary Search
215Kth Largest Element in an ArrayC++PythonO(n) ~ O(n^2)O(1)MediumEPI
228Summary RangesC++PythonO(n)O(1)Medium
229Majority Element IIC++PythonO(n)O(1)Medium
238Product of Array Except SelfC++PythonO(n)O(1)MediumLintCode
240Search a 2D Matrix IIC++PythonO(m + n)O(1)MediumEPI, LintCode
243Shortest Word DistanceC++PythonO(n)O(1)Easy📖
245Shortest Word Distance IIIC++PythonO(n)O(1)Medium📖
251Flatten 2D VectorC++PythonO(1)O(1)Medium📖
277Find the CelebrityC++PythonO(n)O(1)Medium📖, EPI
289Game of LifeC++PythonO(m * n)O(1)Medium
293Flip GameC++PythonO(n * (c+1))O(1)Easy📖
296Best Meeting PointC++PythonO(m * n)O(m + n)Hard📖
311Sparse Matrix MultiplicationC++PythonO(m * n * l)O(m * l)Medium📖
334Increasing Triplet SubsequenceC++PythonO(n)O(1)Medium
370Range AdditionC++PythonO(k + n)O(1)Medium📖
384Shuffle an ArrayC++PythonO(n)O(n)MediumEPI
396Rotate FunctionC++PythonO(n)O(1)Easy
412Fizz BuzzC++PythonO(n)O(1)Easy
414Third Maximum NumberC++PythonO(n)O(1)Easy
419Battleships in a BoardC++PythonO(m * n)O(1)Medium
422Valid Word SquareC++PythonO(m * n)O(1)Easy📖
442Find All Duplicates in an ArrayC++PythonO(n)O(1)Medium
448Find All Numbers Disappeared in an ArrayC++PythonO(n)O(1)Easy
661Image SmootherC++PythonO(m * n)O(1)Easy
665Non-decreasing ArrayC++PythonO(n)O(1)Easy
667Beautiful Arrangement IIC++PythonO(n)O(1)Medium
670Maximum SwapC++PythonO(logn)O(logn)Medium
674Longest Continuous Increasing SubsequenceC++PythonO(n)O(1)Easy

String

#TitleSolutionTimeSpaceDifficultyTagNote
5Longest Palindromic SubstringC++PythonO(n)O(n)MediumManacher's Algorithm
6ZigZag ConversionC++PythonO(n)O(1)Easy
8String to Integer (atoi)C++PythonO(n)O(1)Easy
14Longest Common PrefixC++PythonO(n * k)O(1)Easy
28Implement strStr()C++PythonO(n + k)O(k)EasyKMP Algorithm
38Count and SayC++PythonO(n * 2^n)O(2^n)Easy
43Multiply StringsC++PythonO(m * n)O(m + n)Medium
58Length of Last WordC++PythonO(n)O(1)Easy
67Add BinaryC++PythonO(n)O(1)Easy
68Text JustificationC++PythonO(n)O(1)Hard
125Valid PalindromeC++PythonO(n)O(1)Easy
151Reverse Words in a StringC++PythonO(n)O(1)Medium
161One Edit DistanceC++PythonO(m + n)O(1)Medium📖
165Compare Version NumbersC++PythonO(n)O(1)Easy
186Reverse Words in a String IIC++PythonO(n)O(1)Medium📖
214Shortest PalindromeC++PythonO(n)O(n)HardKMP AlgorithmManacher's Algorithm
242Valid AnagramC++PythonO(n)O(1)EasyLintCode
271Encode and Decode StringsC++PythonO(n)O(1)Medium📖
273Integer to English WordsC++PythonO(logn)O(1)Hard
306Addictive NumberC++PythonO(n^3)O(n)Medium
383Ransom NoteC++PythonO(n)O(1)EasyEPI
405Convert a Number to HexadecimalC++PythonO(n)O(1)Easy
408Valid Word AbbreviationC++PythonO(n)O(1)Easy📖
415Add StringsC++PythonO(n)O(1)Easy
420Strong Password CheckerC++PythonO(n)O(1)Hard
434Number of Segments in a StringC++PythonO(n)O(1)Easy
459Repeated Substring PatternC++PythonO(n)O(n)EasyKMP Algorithm
468Validate IP AddressC++PythonO(1)O(1)Medium
556Next Greater Element IIIC++PythonO(1)O(1)Medium
557Reverse Words in a String IIIC++PythonO(n)O(1)Easy
647Palindromic SubstringsC++PythonO(n)O(n)MediumManacher's Algorithm
648Replace WordsC++PythonO(n)O(t)Mediumtrie
657Judge Route CircleC++PythonO(n)O(1)Easy
678Valid Parenthesis StringC++PythonO(n)O(1)Medium
680Valid Palindrome IIC++PythonO(n)O(1)Easy

Linked List

#TitleSolutionTimeSpaceDifficultyTagNote
2Add Two NumbersC++PythonO(n)O(1)Medium
21Merge Two Sorted ListsC++PythonO(n)O(1)Easy
23Merge k Sorted ListsC++PythonO(nlogk)O(1)HardHeap, Divide and Conquer
24Swap Nodes in PairsC++PythonO(n)O(1)Easy
25Reverse Nodes in k-GroupC++PythonO(n)O(1)Hard
61Rotate ListC++PythonO(n)O(1)Medium
82Remove Duplicates from Sorted List IIC++PythonO(n)O(1)Medium
83Remove Duplicates from Sorted ListC++PythonO(n)O(1)Easy
92Reverse Linked List IIC++PythonO(n)O(1)Medium
138Copy List with Random PointerC++PythonO(n)O(1)Hard
160Intersection of Two Linked ListsC++PythonO(m + n)O(1)Easy
203Remove Linked List ElementsC++PythonO(n)O(1)Easy
206Reverse Linked ListC++PythonO(n)O(1)Easy
234Palindrome Linked ListC++PythonO(n)O(1)Easy
237Delete Node in a Linked ListC++PythonO(1)O(1)EasyLintCode
328Odd Even Linked ListC++PythonO(n)O(1)Medium
369Plus One Linked ListC++PythonO(n)O(1)Medium📖Two Pointers
445Add Two Numbers IIC++PythonO(m + n)O(m + n)Medium

Stack

#TitleSolutionTimeSpaceDifficultyTagNote
20Valid ParenthesesC++PythonO(n)O(n)Easy
32Longest Valid ParenthesesC++PythonO(n)O(1)Hard
71Simplify PathC++PythonO(n)O(n)Medium
84Largest Rectangle in HistogramC++PythonO(n)O(n)HardAscending Stack, DP
85Maximal RectangleC++PythonO(m * n)O(n)HardEPIAscending Stack
101Symmetric TreeC++PythonO(n)O(h)Easy
150Evaluate Reverse Polish NotationC++PythonO(n)O(n)Medium
155Min StackC++PythonO(n)O(1)Easy
173Binary Search Tree IteratorC++PythonO(1)O(h)Medium
224Basic CalculatorC++PythonO(n)O(n)Hard
227Basic Calculator IIC++PythonO(n)O(n)Medium
232Implement Queue using StacksC++PythonO(1), amortizedO(n)EasyEPI, LintCode
255Verify Preorder Sequence in Binary Search TreeC++PythonO(n)O(1)Medium📖
272Closest Binary Search Tree Value IIC++PythonO(h + k)O(h)Hard📖
331Verify Preorder Serialization of a Binary TreeC++PythonO(n)O(1)Medium
341Flatten Nested List IteratorC++PythonO(n)O(h)Medium📖Iterator
385Mini ParserC++PythonO(n)O(h)Medium
394Decode StringC++PythonO(n)O(h)Medium
439Ternary Expression ParserC++PythonO(n)O(1)Medium📖
456132 PatternC++PythonO(n)O(n)Medium

Queue

#TitleSolutionTimeSpaceDifficultyTagNote
239Sliding Window MaximumC++PythonO(n)O(k)HardEPI, LintCode
281Zigzag IteratorC++PythonO(n)O(k)Medium📖
346Moving Average from Data StreamC++PythonO(1)O(w)Easy📖

Heap

#TitleSolutionTimeSpaceDifficultyTagNote
264Ugly Number IIC++PythonO(n)O(1)MediumCTCI, LintCodeBST, Heap
295Find Median from Data StreamC++PythonO(nlogn)O(n)HardEPI, LintCodeBST, Heap
313Super Ugly NumberC++PythonO(n * k)O(n + k)MediumBST, Heap
358Rearrange String k Distance ApartC++PythonO(n)O(n)Hard📖Greedy, Heap
373Find K Pairs with Smallest SumsC++PythonO(k * log(min(n, m, k)))O(min(n, m, k))Medium
378Kth Smallest Element in a Sorted MatrixC++PythonO(k * log(min(n, m, k)))O(min(n, m, k))MediumLintCode
407Trapping Rain Water IIC++PythonO(m * n * (logm + logn))O(m * n)HardLintCode

Tree

#TitleSolutionTimeSpaceDifficultyTagNote
94Binary Tree Inorder TraversalC++PythonO(n)O(1)MediumMorris Traversal
99Recover Binary Search TreeC++PythonO(n)O(1)HardMorris Traversal
144Binary Tree Preorder TraversalC++PythonO(n)O(1)MediumMorris Traversal
145Binary Tree Postorder TraversalC++PythonO(n)O(1)HardMorris Traversal
208Implement Trie (Prefix Tree)C++PythonO(n)O(1)MediumTrie
211Add and Search Word - Data structure designC++PythonO(min(n, h))O(min(n, h))MediumTrie, DFS
226Invert Binary TreeC++PythonO(n)O(h), O(w)Easy
297Serialize and Deserialize Binary TreeC++PythonO(n)O(h)HardLintCodeDFS
307Range Sum Query - MutableC++Pythonctor: O(n), update: O(logn), query: O(logn)O(n)MediumLintCodeDFS, Segment Tree, BIT
308Range Sum Query 2D - MutableC++Pythonctor: O(m * n), update: O(logm + logn), query: O(logm + logn)O(m * n)Hard📖DFS, Segment Tree, BIT
315Count of Smaller Numbers After SelfC++PythonO(nlogn)O(n)HardLintCodeBST, BIT, Divide and Conquer
652Find Duplicate SubtreesC++PythonO(n)O(n)MediumDFS, Hash
653Two Sum IV - Input is a BSTC++PythonO(n)O(h)EasyTwo Pointers
654Maximum Binary TreeC++PythonO(n)O(n)MediumLintCodeDescending Stack
655Print Binary TreeC++PythonO(n)O(h)Medium
662Maximum Width of Binary TreeC++PythonO(n)O(h)MediumDFS
663Equal Tree PartitionC++PythonO(n)O(n)Medium📖Hash
677Map Sum PairsC++PythonO(n)O(t)MediumTrie

Hash Table

#TitleSolutionTimeSpaceDifficultyTagNote
1Two SumC++PythonO(n)O(n)Easy
3Longest Substring Without Repeating CharactersC++PythonO(n)O(1)Medium
30Substring with Concatenation of All WordsC++PythonO(m * n * k)O(n * k)Hard
36Valid SudokuC++PythonO(9^2)O(9)Easy
49Group AnagramsC++PythonO(n * glogg)O(n)Medium
76Minimum Window SubstringC++PythonO(n)O(k)Hard
149Max Points on a LineC++PythonO(n^2)O(n)Hard
159Longest Substring with At Most Two Distinct CharactersC++PythonO(n)O(1)Hard📖
170Two Sum III - Data structure designC++PythonO(n)O(n)Easy📖
187Repeated DNA SequencesPythonO(n)O(n)Medium
202Happy NumberC++PythonO(k)O(k)Easy
204Count PrimesC++PythonO(n)O(n)Easy
205Isomorphic StringsC++PythonO(n)O(1)Easy
217Contains DuplicateC++PythonO(n)O(n)Easy
219Contains Duplicate IIC++PythonO(n)O(n)Easy
244Shortest Word Distance IIC++Pythonctor: O(n), lookup: O(a + b)O(n)Medium📖
246Strobogrammatic NumberC++PythonO(n)O(1)Easy📖
249Group Shifted StringsC++PythonO(nlogn)O(n)Easy📖
266Palindrome PermutationC++PythonO(n)O(1)Easy📖
288Unique Word AbbreviationC++Pythonctor: O(n), lookup: O(1)O(k)Easy📖
290Word PatternC++PythonO(n)O(c)Easyvariant of Isomorphic Strings
299Bulls and CowsC++PythonO(n)O(1)Easy
305Number of Islands IIC++PythonO(k)O(k)HardLintCode, 📖Union Find
314Binary Tree Vertical Order TraversalC++PythonO(n)O(n)Medium📖BFS
323Number of Connected Components in an Undirected GraphC++PythonO(n)O(n)Medium📖Union Find
325Maximum Size Subarray Sum Equals kC++PythonO(n)O(n)Medium📖
336Palindrome PairsC++PythonO(n * k^2)O(n * k)Hard
340Longest Substring with At Most K Distinct CharactersC++PythonO(n)O(1)Hard📖
356Line ReflectionC++PythonO(n)O(n)Medium📖Hash, Two Pointers
387First Unique Character in a StringC++PythonO(n)O(n)Easy
388Longest Absolute File PathC++PythonO(n)O(d)MediumStack
409Longest PalindromeC++PythonO(n)O(1)Easy
424Longest Repeating Character ReplacementC++PythonO(n)O(1)Medium
438Find All Anagrams in a StringC++PythonO(n)O(1)Easy
447Number of BoomerangsC++PythonO(n^2)O(n)Easy
4544Sum IIC++PythonO(n^2)O(n^2)Medium
473Matchsticks to SquareC++PythonO(n * s * 2^n)O(n * (2^n + s))Medium
554Brick WallC++PythonO(n)O(m)Medium

Math

#TitleSolutionTimeSpaceDifficultyTagNote
7Reverse IntegerC++PythonO(1)O(1)Easy
9Palindrome NumberC++PythonO(1)O(1)Easy
12Integer to RomanC++PythonO(n)O(1)Medium
13Roman to IntegerC++PythonO(n)O(1)Easy
29Divide Two IntegersC++PythonO(1)O(1)Medium
50Pow(x, n)C++PythonO(1)O(1)Medium
60Permutation SequenceC++PythonO(n^2)O(n)MediumCantor Ordering
65Valid NumberC++PythonO(n)O(1)HardAutomata
89Gray CodeC++PythonO(2^n)O(1)Medium
166Fraction to Recurring DecimalC++PythonO(logn)O(1)Medium
168Excel Sheet Column TitleC++PythonO(logn)O(1)Easy
171Excel Sheet Column NumberC++PythonO(n)O(1)Easy
172Factorial Trailing ZeroesC++PythonO(1)O(1)Easy
223Rectangle AreaC++PythonO(1)O(1)Easy
233Number of Digit OneC++PythonO(1)O(1)HardCTCI, LintCode
248Strobogrammatic Number IIIC++PythonO(5^(n/2))O(n)Hard📖
258Add DigitsC++PythonO(1)O(1)Easy
263Ugly NumberC++PythonO(1)O(1)Easy
292Nim GameC++PythonO(1)O(1)EasyLintCode
319Bulb SwitcherC++PythonO(1)O(1)Medium
326Power of ThreeC++PythonO(1)O(1)Easy
335Self CrossingC++PythonO(n)O(1)Hard
338Counting BitsC++PythonO(n)O(n)Medium
343Integer BreakC++PythonO(logn)O(1)MediumTricky, DP
365Water and Jug ProblemC++PythonO(logn)O(1)MediumEuclidean Algorithm
372Super PowC++PythonO(n)O(1)Medium
382Linked List Random NodeC++PythonO(n)O(1)MediumReservoir Sampling
386Lexicographical NumbersC++PythonO(n)O(1)Medium
390Elimination GameC++PythonO(logn)O(1)Medium
391Perfect RectangleC++PythonO(n)O(n)Hard
398Random Pick IndexC++PythonO(n)O(1)MediumReservoir Sampling
400Nth DigitC++PythonO(logn)O(1)Easy
413Arithmetic SlicesC++PythonO(n)O(1)Medium
423Reconstruct Original Digits from EnglishC++PythonO(n)O(1)MediumGCJ2016 - Round 1B
441Arranging CoinsC++PythonO(nlogn)O(1)EasyBinary Search
453Minimum Moves to Equal Array ElementsC++PythonO(n)O(1)Easy
458Poor PigsC++PythonO(n)O(1)Easy
469Convex PolygonC++PythonO(n)O(1)Medium📖
6514 Keys KeyboardC++PythonO(1)O(1)Medium📖Math, DP
660Remove 9C++PythonO(logn)O(1)Hard📖
672Bulb Switcher IIC++PythonO(1)O(1)Medium

Sort

#TitleSolutionTimeSpaceDifficultyTagNote
56Merge IntervalsC++PythonO(nlogn)O(1)Hard
57Insert IntervalC++PythonO(n)O(1)Hard
75Sort ColorsC++PythonO(n)O(1)MediumTri Partition
88Merge Sorted ArrayC++PythonO(n)O(1)Easy
147Insertion Sort ListC++PythonO(n^2)O(1)Medium
148Sort ListC++PythonO(nlogn)O(logn)Medium
164Maximum GapC++PythonO(n)O(n)HardTricky
179Largest NumberC++PythonO(nlogn)O(1)Medium
218The Skyline ProblemC++PythonO(nlogn)O(n)HardSort, BST
252Meeting RoomsC++PythonO(nlogn)O(n)Easy📖
253Meeting Rooms IIC++PythonO(nlogn)O(n)Medium📖
274H-IndexC++PythonO(n)O(n)MediumCounting Sort
280Wiggle SortC++PythonO(n)O(1)Medium📖
324Wiggle Sort IIC++PythonO(n) on averageO(1)Mediumvariant of Sort ColorsTri Partition
347Top K Frequent ElementsC++PythonO(n) on averageO(n)MediumQuick Select, Heap
406Queue Reconstruction by HeightC++PythonO(n * sqrt(n))O(n)Medium
451Sort Characters By FrequencyC++PythonO(n)O(n)Medium

Two Pointers

#TitleSolutionTimeSpaceDifficultyTagNote
19Remove Nth Node From End of ListC++PythonO(n)O(1)Easy
86Partition ListC++PythonO(n)O(1)Medium
141Linked List CycleC++PythonO(n)O(1)Easy
142Linked List Cycle IIC++PythonO(n)O(1)Medium
143Reorder ListC++PythonO(n)O(1)Medium
167Two Sum II - Input array is sortedC++PythonO(n)O(1)Medium
2593Sum SmallerC++PythonO(n^2)O(1)Medium📖, LintCode
283Move ZeroesC++PythonO(n)O(1)Easy
287Find the Duplicate NumberC++PythonO(n)O(1)HardBinary Search, Two Pointers
344Reverse StringC++PythonO(n)O(1)Easy
345Reverse Vowels of a StringC++PythonO(n)O(1)Easy
349Intersection of Two ArraysC++PythonO(m + n)O(min(m, n))EasyEPIHash, Binary Search
350Intersection of Two Arrays IIC++PythonO(m + n)O(1)EasyEPIHash, Binary Search
360Sort Transformed ArrayC++PythonO(n)O(1)Medium📖
457Circular Array LoopC++PythonO(n)O(1)Medium

Recursion

#TitleSolutionTimeSpaceDifficultyTagNote
95Unique Binary Search Trees IIC++PythonO(4^n / n^(3/2)O(4^n / n^(3/2)Medium
98Validate Binary Search TreeC++PythonO(n)O(1)Medium
100Same TreeC+PythonO(n)O(h)Easy
104Maximum Depth of Binary TreeC++PythonO(n)O(h)Easy
105Construct Binary Tree from Preorder and Inorder TraversalC++PythonO(n)O(n)Medium
106Construct Binary Tree from Inorder and Postorder TraversalC++PythonO(n)O(n)Medium
108Convert Sorted Array to Binary Search TreeC++PythonO(n)O(logn)Medium
109Convert Sorted List to Binary Search TreeC++PythonO(n)O(logn)Medium
110Balanced Binary TreePythonO(n)O(h)Easy
111Minimum Depth of Binary TreePythonO(n)O(h)Easy
114Flatten Binary Tree to Linked ListPythonO(n)O(h)Medium
116Populating Next Right Pointers in Each NodePythonO(n)O(1)Medium
124Binary Tree Maximum Path SumPythonO(n)O(h)Hard
129Sum Root to Leaf NumbersPythonO(n)O(h)Medium
156Binary Tree Upside DownPythonO(n)O(1)Medium📖
241Different Ways to Add ParenthesesC++PythonO(n * 4^n / n^(3/2))O(n * 4^n / n^(3/2))Medium
298Binary Tree Longest Consecutive SequenceC++PythonO(n)O(h)Medium📖
327Count of Range SumC++PythonO(nlogn)O(n)Hard
333Largest BST SubtreeC++PythonO(n)O(h)Medium📖
337House Robber IIIC++PythonO(n)O(h)Medium
395Longest Substring with At Least K Repeating CharactersC++PythonO(n)O(1)Medium
404Sum of Left LeavesC++PythonO(n)O(h)Easy
437Path Sum IIIC++PythonO(n)O(h)Easy
549Binary Tree Longest Consecutive Sequence IIC++PythonO(n)O(h)Medium📖
669Trim a Binary Search TreeC++PythonO(n)O(h)Easy
671Second Minimum Node In a Binary TreeC++PythonO(n)O(h)Easy

Binary Search

#TitleSolutionTimeSpaceDifficultyTagNote
4Median of Two Sorted ArraysC++PythonO(log(min(m, n)))O(1)Hard
33Search in Rotated Sorted ArrayC++PythonO(logn)O(1)Hard
34Search for a RangeC++PythonO(logn)O(1)Medium
35Search Insert PositionC++PythonO(logn)O(1)Medium
69Sqrt(x)C++PythonO(logn)O(1)Medium
74Search a 2D MatrixC++PythonO(logm + logn)O(1)Medium
81Search in Rotated Sorted Array IIC++PythonO(logn)O(1)Medium
153Find Minimum in Rotated Sorted ArrayC++PythonO(logn)O(1)Medium
154Find Minimum in Rotated Sorted Array IIC++PythonO(logn) ~ O(n)O(1)Hard
162Find Peak ElementC++PythonO(logn)O(1)Medium
222Count Complete Tree NodesC++PythonO((logn)^2)O(1)Medium
275H-Index IIC++PythonO(logn)O(1)MediumBinary Search
278First Bad VersionC++PythonO(logn)O(1)EasyLintCode
300Longest Increasing SubsequenceC++PythonO(nlogn)O(n)MediumCTCI, LintCodeBinary Search, DP
302Smallest Rectangle Enclosing Black PixelsC++PythonO(nlogn)O(1)Hard📖
354Russian Doll EnvelopesC++PythonO(nlogn)O(1)Hard
363Max Sum of Rectangle No Larger Than KC++PythonO(min(m, n)^2 * max(m, n) * logn(max(m, n)))O(max(m, n))Hard
367Valid Perfect SquareC++PythonO(logn)O(1)Medium
374Guess Number Higher or LowerC++PythonO(logn)O(1)Easy
410Split Array Largest SumC++PythonO(nlogs)O(1)Hard
436Find Right IntervalC++PythonO(nlogn)O(n)Medium
475HeatersC++PythonO((m + n) * logn)O(1)Easy
658Find K Closest ElementsC++PythonO(logn + k)O(1)Medium
668Kth Smallest Number in Multiplication TableC++PythonO(m * log(m * n))O(1)Hard

Binary Search Tree

#TitleSolutionTimeSpaceDifficultyTagNote
220Contains Duplicate IIIC++PythonO(nlogk)O(k)Medium
230Kth Smallest Element in a BSTC++PythonO(max(h, k))O(min(h, k))Medium
235Lowest Common Ancestor of a Binary Search TreeC++PythonO(h)O(1)EasyEPI
270Closest Binary Search Tree ValueC++PythonO(h)O(1)Easy📖
285Inorder Successor in BSTC++PythonO(h)O(1)Medium📖
352Data Stream as Disjoint IntervalsC++PythonO(logn)O(n)Hard
449Serialize and Deserialize BSTC++PythonO(n)O(h)Medium
450Delete Node in a BSTC++PythonO(h)O(h)Medium

Breadth-First Search

#TitleSolutionTimeSpaceDifficultyTagNote
102Binary Tree Level Order TraversalC++PythonO(n)O(n)Easy
107Binary Tree Level Order Traversal IIPythonO(n)O(n)Easy
103Binary Tree Zigzag Level Order TraversalPythonO(n)O(n)Medium
117Populating Next Right Pointers in Each Node IIPythonO(n)O(1)Hard
127Word LadderPythonO(n * d)O(d)Medium
130Surrounded RegionsC++PythonO(m * n)O(m + n)Medium
133Clone GraphPythonO(n)O(n)Medium
207Course SchedulePythonO(|V| + |E|)O(|E|)MediumTopological Sort
210Course Schedule IIPythonO(|V| + |E|)O(|E|)MediumTopological Sort
261Graph Valid TreeC++PythonO(|V| + |E|)O(|V| + |E|)Medium📖
269Alien DictionaryC++PythonO(n)O(1)Hard📖Topological Sort, BFS, DFS
286Walls and GatesC++PythonO(m * n)O(g)Medium📖
310Minimum Height TreesC++PythonO(n)O(n)Medium
317Shortest Distance from All BuildingsC++PythonO(k * m * n)O(m * n)Hard📖
433Minimum Genetic MutationC++PythonO(n * b)O(b)Medium
444Sequence ReconstructionC++PythonO(n * s)O(n)Medium📖Topological Sort
666Path Sum IVC++PythonO(n)O(w)Medium📖Topological Sort
675Cut Off Trees for Golf EventC++PythonO(t * m * n)O(m * n)HardA* Search Algorithm

Depth-First Search

#TitleSolutionTimeSpaceDifficultyTagNote
112Path SumPythonO(n)O(h)Easy
113Path Sum IIPythonO(n)O(h)Medium
199Binary Tree Right Side ViewPythonO(n)O(h)Medium
200Number of IslandsPythonO(m * n)O(m * n)Medium
236Lowest Common Ancestor of a Binary TreeC++PythonO(n)O(h)MediumEPI
247Strobogrammatic Number IIC++PythonO(n^2 * 5^(n/2))O(n)Medium📖
250Count Univalue SubtreesC++PythonO(n)O(h)Medium📖
257Binary Tree PathsC++PythonO(n * h)O(h)Easy
282Expression Add OperatorsC++PythonO(4^n)O(n)Hard
301Remove Invalid ParenthesesC++PythonO(C(n, c))O(c)Hard
329Longest Increasing Path in a MatrixC++PythonO(m * n)O(m * n)Hard
332Reconstruct ItineraryC++PythonO(t! / (n1! * n2! * ... nk!))O(t)Medium
339Nested List Weight SumC++PythonO(n)O(h)Easy📖
364Nested List Weight Sum IIC++PythonO(n)O(h)Medium📖
366Find Leaves of Binary TreeC++PythonO(n)O(h)Medium📖
399Evaluate DivisionC++PythonO(q * |V|!)O(e)Medium
417Pacific Atlantic Water FlowC++PythonO(m * n)O(m * n)Medium
440K-th Smallest in Lexicographical OrderC++PythonO(logn)O(logn)Hard
464Can I WinC++PythonO(n!)O(n)Medium

Backtracking

#TitleSolutionTimeSpaceDifficultyTagNote
17Letter Combinations of a Phone NumberPythonO(n * 4^n)O(n)Medium
22Generate ParenthesesPythonO(4^n / n^(3/2))O(n)Medium
37Sudoku SolverPythonO((9!)^9)O(1)Hard
39Combination SumPythonO(k * n^k)O(k)Medium
40Combination Sum IIPythonO(k * C(n, k))O(k)Medium
46PermutationsPythonO(n * n!)O(n)Medium
47Permutations IIPythonO(n * n!)O(n)Medium
51N-QueensPythonO(n!)O(n)Hard
52N-Queens-IIPythonO(n!)O(n)Hard
77CombinationsPythonO(n!)O(n)Medium
79Word SearchPythonO(m * n * l)O(l)Medium
93Restore IP AddressesPythonO(1)O(1)Medium
78SubsetsC++PythonO(n * 2^n)O(1)Medium
90Subsets IIC++PythonO(n * 2^n)O(1)Medium
126Word Ladder IIPythonO(n * d)O(d)Hard
131Palindrome PartitioningPythonO(n^2) ~ O(2^n)O(n^2)Medium
140Word Break IIC++PythonO(n * l^2 + n * r)O(n^2)Hard
212Word Search IIC++PythonO(m * n * l)O(l)HardLintCodeTrie, DFS
216Combination Sum IIIC++PythonO(k * C(n, k))O(k)Medium
254Factor CombinationsC++PythonO(nlogn)O(logn)Medium📖
267Palindrome Permutation IIC++PythonO(n * n!)O(n)Medium📖
291Word Pattern IIC++PythonO(n * C(n - 1, c - 1))O(n + c)Hard📖
294Flip Game IIC++PythonO(n + c^2)O(c)Medium📖DP, Hash
320Generalized AbbreviationC++PythonO(n * 2^n)O(n)Medium📖
425Word SquaresC++PythonO(n^2 * n!)O(n^2)Hard📖
676Implement Magic DictionaryC++PythonO(n)O(d)MediumTrie, DFS
67924 GameC++PythonO(1)O(1)HardDFS

Dynamic Programming

#TitleSolutionTimeSpaceDifficultyTagNote
10Regular Expression MatchingPythonO(m * n)O(n)Hard
53Maximum SubarrayPythonO(n)O(1)Medium
62Unique PathsPythonO(m * n)O(m + n)Medium
63Unique Paths IIPythonO(m * n)O(m + n)Medium
64Minimum Path SumPythonO(m * n)O(m + n)Medium
70Climbing StairsPythonO(n)O(1)Easy
72Edit DistancePythonO(m * n)O(m + n)Hard
87Scramble StringPythonO(n^4)O(n^3)Hard
91Decode WaysC++PythonO(n)O(1)Medium
96Unique Binary Search TreesPythonO(n)O(1)MediumMath
97Interleaving StringPythonO(m * n)O(m + n)Hard
115Distinct SubsequencesPythonO(n^2)O(n)Hard
120TrianglePythonO(m * n)O(n)Medium
123Best Time to Buy and Sell Stock IIIPythonO(n)O(1)Hard
132Palindrome Partitioning IIPythonO(n^2)O(n^2)Hard
139Word BreakC++PythonO(n * l^2)O(n)Medium
152Maximum Product SubarrayPythonO(n)O(1)Medium
174Dungeon GamePythonO(m * n)O(m + n)Hard
188Best Time to Buy and Sell Stock IVPythonO(k * n)O(k)Hard
198House RobberPythonO(n)O(1)Easy
213House Robber IIC++PythonO(n)O(1)Medium
221Maximal SquareC++PythonO(n^2)O(n)MediumEPI
256Paint HouseC++PythonO(n)O(1)Medium📖
265Paint House IIC++PythonO(n * k)O(k)Hard📖
276Paint FenceC++PythonO(n)O(1)Easy📖
279Perfect SquaresC++PythonO(n * sqrt(n))O(n)MediumHash
303Range Sum Query - ImmutableC++Pythonctor: O(n), lookup: O(1)O(n)Easy
304Range Sum Query 2D - ImmutableC++Pythonctor: O(m * n), lookup: O(1)O(m * n)Medium
309Best Time to Buy and Sell Stock with CooldownC++PythonO(n)O(1)Medium
312Burst BalloonsC++PythonO(n^3)O(n^2)Hard
322Coin ChangeC++PythonO(n * k)O(k)Medium
351Android Unlock PatternsC++PythonO(9^2 * 2^9)O(9 * 2^9)Medium📖Backtracking
357Count Numbers with Unique DigitsC++PythonO(n)O(1)MediumBacktracking, Math
361Bomb EnemyC++PythonO(m * n)O(m * n)Medium📖
368Largest Divisible SubsetC++PythonO(n^2)O(n)Medium
375Guess Number Higher or Lower IIC++PythonO(n^2)O(n^2)Medium
377Combination Sum IVC++PythonO(nlogn + n * t)O(t)Medium
403Frog JumpC++PythonO(n)O(n) ~ O(n^2)Hard
416Partition Equal Subset SumC++PythonO(n * s)O(s)Medium
418Sentence Screen FittingC++PythonO(r + n * c)O(n)Medium📖
446Arithmetic Slices II - SubsequenceC++PythonO(n^2)O(n * d)Hard
465Optimal Account BalancingC++PythonO(n * 2^n)O(n * 2^n)Hard📖
466Count The RepetitionsC++PythonO(s1 * min(s2, n1))O(s2)Hard
467Unique Substrings in Wraparound StringC++PythonO(n)O(1)Medium
471Encode String with Shortest LengthC++PythonO(n^3) on averageO(n^2)Medium📖
472Concatenated WordsC++PythonO(n * l^2)O(n * l)Medium
474Ones and ZeroesC++PythonO(s * m * n)O(m * n)Medium
6502 Keys KeyboardC++PythonO(sqrt(n))O(1)Medium
656Coin PathC++PythonO(n * B)O(n)Hard📖
664Strange PrinterC++PythonO(n^3)O(n^2)Hard
673Number of Longest Increasing SubsequenceC++PythonO(n^2)O(n)Medium

Greedy

#TitleSolutionTimeSpaceDifficultyTagNote
11Container With Most WaterC++PythonO(n)O(1)Medium
42Trapping Rain WaterC++PythonO(n)O(1)HardTricky
44Wildcard MatchingPythonO(m + n)O(1)HardTricky
45Jump Game IIPythonO(n)O(1)Hard
55Jump GamePythonO(n)O(1)Medium
122Best Time to Buy and Sell Stock IIPythonO(n)O(1)Medium
134Gas StationPythonO(n)O(1)Medium
135CandyC++PythonO(n)O(n)Hard
316Remove Duplicate LettersC++PythonO(n)O(k)HardAscending Stack
321Create Maximum NumberC++PythonO(k * (m + n + k)) ~ O(k * (m + n + k^2))O(m + n + k^2)Hardvariant of Delete DigitsGreedy, DP
330Patching ArrayC++PythonO(s + logn)O(1)Hard
376Wiggle SubsequenceC++PythonO(n)O(1)Medium
392Is SubsequenceC++PythonO(n)O(1)Medium
397Integer ReplacementC++PythonO(n)O(1)MediumMath
402Remove K DigitsC++PythonO(n)O(n)MediumLintCode
435Non-overlapping IntervalsC++PythonO(nlogn)O(1)Medium
452Minimum Number of Arrows to Burst BalloonsC++PythonO(nlogn)O(1)Medium
455Assign CookiesC++PythonO(nlogn)O(1)Easy
646Maximum Length of Pair ChainC++PythonO(nlogn)O(1)MediumScan Line
649Dota2 SenateC++PythonO(n)O(n)Medium
659Split Array into Consecutive SubsequencesC++PythonO(n)O(1)Medium

Design

#TitleSolutionTimeSpaceDifficultyTagNote
146LRU CacheC++PythonO(1)O(k)Hard
225Implement Stack using QueuesC++Pythonpush: O(n), pop: O(1), top: O(1)O(n)Easy
284Peeking IteratorC++PythonO(1)O(1)Medium
348Design Tic-Tac-ToeC++PythonO(1)O(n^2)Medium📖
353Design Snake GameC++PythonO(1)O(s)Medium📖Deque
355Design TwitterC++PythonO(klogu)O(t + f)MediumLintCodeHeap
359Logger Rate LimiterC++PythonO(1), amortizedO(k)Easy📖Deque
362Design Hit CounterC++PythonO(1), amortizedO(k)Medium📖Deque
379Design Phone DirectoryC++PythonO(1)O(n)Medium📖
380Insert Delete GetRandom O(1)C++PythonO(1)O(n)Hard
381Insert Delete GetRandom O(1) - Duplicates allowedC++PythonO(1)O(n)Hard
432All O`one Data StructureC++PythonO(1)O(n)Hard
460LFU CacheC++PythonO(1)O(k)Hard

SQL

#TitleSolutionTimeSpaceDifficultyTagNote
175Combine Two TablesMySQLO(m + n)O(m + n)Easy
176Second Highest SalaryMySQLO(n)O(1)Easy
177Nth Highest SalaryMySQLO(n^2)O(n)Medium
178Rank ScoresMySQLO(n^2)O(n)Medium
180Consecutive NumbersMySQLO(n)O(n)Medium
181Employees Earning More Than Their ManagersMySQLO(n^2)O(1)Easy
182Duplicate EmailsMySQLO(n^2)O(n)Easy
183Customers Who Never OrderMySQLO(n^2)O(1)Easy
184Department Highest SalaryMySQLO(n^2)O(n)Medium
185Department Top Three SalariesMySQLO(n^2)O(n)Hard
196Delete Duplicate EmailsMySQLO(n^2)O(n)Easy
197Rising TemperatureMySQLO(n^2)O(n)Easy
262Trips and UsersMySQLO((t * u) + tlogt)O(t)Hard

Shell Script

#TitleSolutionTimeSpaceDifficultyTagNote
192Word FrequencyShellO(n)O(k)Medium
193Valid Phone NumbersShellO(n)O(1)Easy
194Transpose FileShellO(n^2)O(n^2)Medium
195Tenth LineShellO(n)O(1)Easy

About

📝 Python / C++ 11 Solutions of LeetCode Questions

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { // Force GitHub README to respect dark mode (function() { var style = document.createElement('style'); style.textContent = ' .markdown-body { color-scheme: dark light; } .markdown-body pre { background: #161b22 !important; } .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; } .markdown-body table th, .markdown-body table td { border-color: #30363d !important; } .markdown-body img { background: #0d1117; } .markdown-body blockquote { border-left-color: #8b949e; } .markdown-body hr { border-color: #30363d; } '; document.head.appendChild(style); })(); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' GitHub - shinan6/LeetCode: :pencil: Python / C++ 11 Solutions of LeetCode Questions · GitHub
Skip to content

Latest commit

History

4,201 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

The number of LeetCode questions is increasing every week. For more questions and solutions, you can see my LintCode repository. I'll keep updating for full summary and better solutions. Stay tuned for updates. (Notes: "📖" means you need to subscribe to LeetCode premium membership for the access to premium questions.)

Algorithms

Database

Shell

Bit Manipulation

#TitleSolutionTimeSpaceDifficultyTagNote
136Single NumberC++PythonO(n)O(1)Easy
137Single Number IIC++PythonO(n)O(1)Medium
190Reverse BitsC++PythonO(1)O(1)Easy
191Number of 1 BitsC++PythonO(1)O(1)Easy
201Bitwise AND of Numbers RangeC++PythonO(1)O(1)Medium
231Power of TwoC++PythonO(1)O(1)EasyLintCode
260Single Number IIIC++PythonO(n)O(1)Medium
268Missing NumberC++PythonO(n)O(1)MediumLintCode
318Maximum Product of Word LengthsC++PythonO(n) ~ O(n^2)O(n)MediumBit Manipulation, Counting Sort, Pruning
342Power of FourC++PythonO(1)O(1)Easy
371Sum of Two IntegersC++PythonO(1)O(1)EasyLintCode
389Find the DifferenceC++PythonO(n)O(1)Easy
393UTF-8 ValidationC++PythonO(n)O(1)Medium
401Binary WatchC++PythonO(1)O(1)Easy
411Minimum Unique Word AbbreviationC++PythonO(2^n)O(n)Hard📖
421Maximum XOR of Two Numbers in an ArrayC++PythonO(n)O(1)Medium
461Hamming DistanceC++PythonO(1)O(1)Easy
462Minimum Moves to Equal Array Elements IIC++PythonO(n) on averageO(1)Medium
477Total Hamming DistanceC++PythonO(n)O(1)Medium
645Set MismatchC++PythonO(n)O(1)Easy

Array

#TitleSolutionTimeSpaceDifficultyTagNote
153 SumC++PythonO(n^2)O(1)MediumTwo Pointers
163 Sum ClosestC++PythonO(n^2)O(1)MediumTwo Pointers
184 SumC++PythonO(n^3)O(1)MediumTwo Pointers
26Remove Duplicates from Sorted ArrayC++PythonO(n)O(1)EasyTwo Pointers
27Remove ElementC++PythonO(n)O(1)Easy
31Next PermutationC++PythonO(n)O(1)MediumTricky
41First Missing PositiveC++PythonO(n)O(1)HardTricky
48Rotate ImageC++PythonO(n^2)O(1)Medium
54Spiral MatrixC++PythonO(m * n)O(1)Medium
59Spiral Matrix IIC++PythonO(n^2)O(1)Medium
66Plus OneC++PythonO(n)O(1)Easy
73Set Matrix ZeroesC++PythonO(m * n)O(1)Medium
80Remove Duplicates from Sorted Array IIC++PythonO(n)O(1)MediumTwo Pointers
118Pascal's TriangleC++PythonO(n^2)O(1)Easy
119Pascal's Triangle IIC++PythonO(n^2)O(1)Easy
121Best Time to Buy and Sell StockC++PythonO(n)O(1)Easy
128Longest Consecutive SequenceC++PythonO(n)O(n)HardTricky
157Read N Characters Given Read4C++PythonO(n)O(1)Easy📖
158Read N Characters Given Read4 II - Call multiple timesC++PythonO(n)O(1)Hard📖
163Missing RangesC++PythonO(n)O(1)Medium📖
169Majority ElementC++PythonO(n)O(1)Easy
189Rotate ArrayC++PythonO(n)O(1)Easy
209Minimum Size Subarray SumC++PythonO(n)O(1)MediumBinary Search
215Kth Largest Element in an ArrayC++PythonO(n) ~ O(n^2)O(1)MediumEPI
228Summary RangesC++PythonO(n)O(1)Medium
229Majority Element IIC++PythonO(n)O(1)Medium
238Product of Array Except SelfC++PythonO(n)O(1)MediumLintCode
240Search a 2D Matrix IIC++PythonO(m + n)O(1)MediumEPI, LintCode
243Shortest Word DistanceC++PythonO(n)O(1)Easy📖
245Shortest Word Distance IIIC++PythonO(n)O(1)Medium📖
251Flatten 2D VectorC++PythonO(1)O(1)Medium📖
277Find the CelebrityC++PythonO(n)O(1)Medium📖, EPI
289Game of LifeC++PythonO(m * n)O(1)Medium
293Flip GameC++PythonO(n * (c+1))O(1)Easy📖
296Best Meeting PointC++PythonO(m * n)O(m + n)Hard📖
311Sparse Matrix MultiplicationC++PythonO(m * n * l)O(m * l)Medium📖
334Increasing Triplet SubsequenceC++PythonO(n)O(1)Medium
370Range AdditionC++PythonO(k + n)O(1)Medium📖
384Shuffle an ArrayC++PythonO(n)O(n)MediumEPI
396Rotate FunctionC++PythonO(n)O(1)Easy
412Fizz BuzzC++PythonO(n)O(1)Easy
414Third Maximum NumberC++PythonO(n)O(1)Easy
419Battleships in a BoardC++PythonO(m * n)O(1)Medium
422Valid Word SquareC++PythonO(m * n)O(1)Easy📖
442Find All Duplicates in an ArrayC++PythonO(n)O(1)Medium
448Find All Numbers Disappeared in an ArrayC++PythonO(n)O(1)Easy
661Image SmootherC++PythonO(m * n)O(1)Easy
665Non-decreasing ArrayC++PythonO(n)O(1)Easy
667Beautiful Arrangement IIC++PythonO(n)O(1)Medium
670Maximum SwapC++PythonO(logn)O(logn)Medium
674Longest Continuous Increasing SubsequenceC++PythonO(n)O(1)Easy

String

#TitleSolutionTimeSpaceDifficultyTagNote
5Longest Palindromic SubstringC++PythonO(n)O(n)MediumManacher's Algorithm
6ZigZag ConversionC++PythonO(n)O(1)Easy
8String to Integer (atoi)C++PythonO(n)O(1)Easy
14Longest Common PrefixC++PythonO(n * k)O(1)Easy
28Implement strStr()C++PythonO(n + k)O(k)EasyKMP Algorithm
38Count and SayC++PythonO(n * 2^n)O(2^n)Easy
43Multiply StringsC++PythonO(m * n)O(m + n)Medium
58Length of Last WordC++PythonO(n)O(1)Easy
67Add BinaryC++PythonO(n)O(1)Easy
68Text JustificationC++PythonO(n)O(1)Hard
125Valid PalindromeC++PythonO(n)O(1)Easy
151Reverse Words in a StringC++PythonO(n)O(1)Medium
161One Edit DistanceC++PythonO(m + n)O(1)Medium📖
165Compare Version NumbersC++PythonO(n)O(1)Easy
186Reverse Words in a String IIC++PythonO(n)O(1)Medium📖
214Shortest PalindromeC++PythonO(n)O(n)HardKMP AlgorithmManacher's Algorithm
242Valid AnagramC++PythonO(n)O(1)EasyLintCode
271Encode and Decode StringsC++PythonO(n)O(1)Medium📖
273Integer to English WordsC++PythonO(logn)O(1)Hard
306Addictive NumberC++PythonO(n^3)O(n)Medium
383Ransom NoteC++PythonO(n)O(1)EasyEPI
405Convert a Number to HexadecimalC++PythonO(n)O(1)Easy
408Valid Word AbbreviationC++PythonO(n)O(1)Easy📖
415Add StringsC++PythonO(n)O(1)Easy
420Strong Password CheckerC++PythonO(n)O(1)Hard
434Number of Segments in a StringC++PythonO(n)O(1)Easy
459Repeated Substring PatternC++PythonO(n)O(n)EasyKMP Algorithm
468Validate IP AddressC++PythonO(1)O(1)Medium
556Next Greater Element IIIC++PythonO(1)O(1)Medium
557Reverse Words in a String IIIC++PythonO(n)O(1)Easy
647Palindromic SubstringsC++PythonO(n)O(n)MediumManacher's Algorithm
648Replace WordsC++PythonO(n)O(t)Mediumtrie
657Judge Route CircleC++PythonO(n)O(1)Easy
678Valid Parenthesis StringC++PythonO(n)O(1)Medium
680Valid Palindrome IIC++PythonO(n)O(1)Easy

Linked List

#TitleSolutionTimeSpaceDifficultyTagNote
2Add Two NumbersC++PythonO(n)O(1)Medium
21Merge Two Sorted ListsC++PythonO(n)O(1)Easy
23Merge k Sorted ListsC++PythonO(nlogk)O(1)HardHeap, Divide and Conquer
24Swap Nodes in PairsC++PythonO(n)O(1)Easy
25Reverse Nodes in k-GroupC++PythonO(n)O(1)Hard
61Rotate ListC++PythonO(n)O(1)Medium
82Remove Duplicates from Sorted List IIC++PythonO(n)O(1)Medium
83Remove Duplicates from Sorted ListC++PythonO(n)O(1)Easy
92Reverse Linked List IIC++PythonO(n)O(1)Medium
138Copy List with Random PointerC++PythonO(n)O(1)Hard
160Intersection of Two Linked ListsC++PythonO(m + n)O(1)Easy
203Remove Linked List ElementsC++PythonO(n)O(1)Easy
206Reverse Linked ListC++PythonO(n)O(1)Easy
234Palindrome Linked ListC++PythonO(n)O(1)Easy
237Delete Node in a Linked ListC++PythonO(1)O(1)EasyLintCode
328Odd Even Linked ListC++PythonO(n)O(1)Medium
369Plus One Linked ListC++PythonO(n)O(1)Medium📖Two Pointers
445Add Two Numbers IIC++PythonO(m + n)O(m + n)Medium

Stack

#TitleSolutionTimeSpaceDifficultyTagNote
20Valid ParenthesesC++PythonO(n)O(n)Easy
32Longest Valid ParenthesesC++PythonO(n)O(1)Hard
71Simplify PathC++PythonO(n)O(n)Medium
84Largest Rectangle in HistogramC++PythonO(n)O(n)HardAscending Stack, DP
85Maximal RectangleC++PythonO(m * n)O(n)HardEPIAscending Stack
101Symmetric TreeC++PythonO(n)O(h)Easy
150Evaluate Reverse Polish NotationC++PythonO(n)O(n)Medium
155Min StackC++PythonO(n)O(1)Easy
173Binary Search Tree IteratorC++PythonO(1)O(h)Medium
224Basic CalculatorC++PythonO(n)O(n)Hard
227Basic Calculator IIC++PythonO(n)O(n)Medium
232Implement Queue using StacksC++PythonO(1), amortizedO(n)EasyEPI, LintCode
255Verify Preorder Sequence in Binary Search TreeC++PythonO(n)O(1)Medium📖
272Closest Binary Search Tree Value IIC++PythonO(h + k)O(h)Hard📖
331Verify Preorder Serialization of a Binary TreeC++PythonO(n)O(1)Medium
341Flatten Nested List IteratorC++PythonO(n)O(h)Medium📖Iterator
385Mini ParserC++PythonO(n)O(h)Medium
394Decode StringC++PythonO(n)O(h)Medium
439Ternary Expression ParserC++PythonO(n)O(1)Medium📖
456132 PatternC++PythonO(n)O(n)Medium

Queue

#TitleSolutionTimeSpaceDifficultyTagNote
239Sliding Window MaximumC++PythonO(n)O(k)HardEPI, LintCode
281Zigzag IteratorC++PythonO(n)O(k)Medium📖
346Moving Average from Data StreamC++PythonO(1)O(w)Easy📖

Heap

#TitleSolutionTimeSpaceDifficultyTagNote
264Ugly Number IIC++PythonO(n)O(1)MediumCTCI, LintCodeBST, Heap
295Find Median from Data StreamC++PythonO(nlogn)O(n)HardEPI, LintCodeBST, Heap
313Super Ugly NumberC++PythonO(n * k)O(n + k)MediumBST, Heap
358Rearrange String k Distance ApartC++PythonO(n)O(n)Hard📖Greedy, Heap
373Find K Pairs with Smallest SumsC++PythonO(k * log(min(n, m, k)))O(min(n, m, k))Medium
378Kth Smallest Element in a Sorted MatrixC++PythonO(k * log(min(n, m, k)))O(min(n, m, k))MediumLintCode
407Trapping Rain Water IIC++PythonO(m * n * (logm + logn))O(m * n)HardLintCode

Tree

#TitleSolutionTimeSpaceDifficultyTagNote
94Binary Tree Inorder TraversalC++PythonO(n)O(1)MediumMorris Traversal
99Recover Binary Search TreeC++PythonO(n)O(1)HardMorris Traversal
144Binary Tree Preorder TraversalC++PythonO(n)O(1)MediumMorris Traversal
145Binary Tree Postorder TraversalC++PythonO(n)O(1)HardMorris Traversal
208Implement Trie (Prefix Tree)C++PythonO(n)O(1)MediumTrie
211Add and Search Word - Data structure designC++PythonO(min(n, h))O(min(n, h))MediumTrie, DFS
226Invert Binary TreeC++PythonO(n)O(h), O(w)Easy
297Serialize and Deserialize Binary TreeC++PythonO(n)O(h)HardLintCodeDFS
307Range Sum Query - MutableC++Pythonctor: O(n), update: O(logn), query: O(logn)O(n)MediumLintCodeDFS, Segment Tree, BIT
308Range Sum Query 2D - MutableC++Pythonctor: O(m * n), update: O(logm + logn), query: O(logm + logn)O(m * n)Hard📖DFS, Segment Tree, BIT
315Count of Smaller Numbers After SelfC++PythonO(nlogn)O(n)HardLintCodeBST, BIT, Divide and Conquer
652Find Duplicate SubtreesC++PythonO(n)O(n)MediumDFS, Hash
653Two Sum IV - Input is a BSTC++PythonO(n)O(h)EasyTwo Pointers
654Maximum Binary TreeC++PythonO(n)O(n)MediumLintCodeDescending Stack
655Print Binary TreeC++PythonO(n)O(h)Medium
662Maximum Width of Binary TreeC++PythonO(n)O(h)MediumDFS
663Equal Tree PartitionC++PythonO(n)O(n)Medium📖Hash
677Map Sum PairsC++PythonO(n)O(t)MediumTrie

Hash Table

#TitleSolutionTimeSpaceDifficultyTagNote
1Two SumC++PythonO(n)O(n)Easy
3Longest Substring Without Repeating CharactersC++PythonO(n)O(1)Medium
30Substring with Concatenation of All WordsC++PythonO(m * n * k)O(n * k)Hard
36Valid SudokuC++PythonO(9^2)O(9)Easy
49Group AnagramsC++PythonO(n * glogg)O(n)Medium
76Minimum Window SubstringC++PythonO(n)O(k)Hard
149Max Points on a LineC++PythonO(n^2)O(n)Hard
159Longest Substring with At Most Two Distinct CharactersC++PythonO(n)O(1)Hard📖
170Two Sum III - Data structure designC++PythonO(n)O(n)Easy📖
187Repeated DNA SequencesPythonO(n)O(n)Medium
202Happy NumberC++PythonO(k)O(k)Easy
204Count PrimesC++PythonO(n)O(n)Easy
205Isomorphic StringsC++PythonO(n)O(1)Easy
217Contains DuplicateC++PythonO(n)O(n)Easy
219Contains Duplicate IIC++PythonO(n)O(n)Easy
244Shortest Word Distance IIC++Pythonctor: O(n), lookup: O(a + b)O(n)Medium📖
246Strobogrammatic NumberC++PythonO(n)O(1)Easy📖
249Group Shifted StringsC++PythonO(nlogn)O(n)Easy📖
266Palindrome PermutationC++PythonO(n)O(1)Easy📖
288Unique Word AbbreviationC++Pythonctor: O(n), lookup: O(1)O(k)Easy📖
290Word PatternC++PythonO(n)O(c)Easyvariant of Isomorphic Strings
299Bulls and CowsC++PythonO(n)O(1)Easy
305Number of Islands IIC++PythonO(k)O(k)HardLintCode, 📖Union Find
314Binary Tree Vertical Order TraversalC++PythonO(n)O(n)Medium📖BFS
323Number of Connected Components in an Undirected GraphC++PythonO(n)O(n)Medium📖Union Find
325Maximum Size Subarray Sum Equals kC++PythonO(n)O(n)Medium📖
336Palindrome PairsC++PythonO(n * k^2)O(n * k)Hard
340Longest Substring with At Most K Distinct CharactersC++PythonO(n)O(1)Hard📖
356Line ReflectionC++PythonO(n)O(n)Medium📖Hash, Two Pointers
387First Unique Character in a StringC++PythonO(n)O(n)Easy
388Longest Absolute File PathC++PythonO(n)O(d)MediumStack
409Longest PalindromeC++PythonO(n)O(1)Easy
424Longest Repeating Character ReplacementC++PythonO(n)O(1)Medium
438Find All Anagrams in a StringC++PythonO(n)O(1)Easy
447Number of BoomerangsC++PythonO(n^2)O(n)Easy
4544Sum IIC++PythonO(n^2)O(n^2)Medium
473Matchsticks to SquareC++PythonO(n * s * 2^n)O(n * (2^n + s))Medium
554Brick WallC++PythonO(n)O(m)Medium

Math

#TitleSolutionTimeSpaceDifficultyTagNote
7Reverse IntegerC++PythonO(1)O(1)Easy
9Palindrome NumberC++PythonO(1)O(1)Easy
12Integer to RomanC++PythonO(n)O(1)Medium
13Roman to IntegerC++PythonO(n)O(1)Easy
29Divide Two IntegersC++PythonO(1)O(1)Medium
50Pow(x, n)C++PythonO(1)O(1)Medium
60Permutation SequenceC++PythonO(n^2)O(n)MediumCantor Ordering
65Valid NumberC++PythonO(n)O(1)HardAutomata
89Gray CodeC++PythonO(2^n)O(1)Medium
166Fraction to Recurring DecimalC++PythonO(logn)O(1)Medium
168Excel Sheet Column TitleC++PythonO(logn)O(1)Easy
171Excel Sheet Column NumberC++PythonO(n)O(1)Easy
172Factorial Trailing ZeroesC++PythonO(1)O(1)Easy
223Rectangle AreaC++PythonO(1)O(1)Easy
233Number of Digit OneC++PythonO(1)O(1)HardCTCI, LintCode
248Strobogrammatic Number IIIC++PythonO(5^(n/2))O(n)Hard📖
258Add DigitsC++PythonO(1)O(1)Easy
263Ugly NumberC++PythonO(1)O(1)Easy
292Nim GameC++PythonO(1)O(1)EasyLintCode
319Bulb SwitcherC++PythonO(1)O(1)Medium
326Power of ThreeC++PythonO(1)O(1)Easy
335Self CrossingC++PythonO(n)O(1)Hard
338Counting BitsC++PythonO(n)O(n)Medium
343Integer BreakC++PythonO(logn)O(1)MediumTricky, DP
365Water and Jug ProblemC++PythonO(logn)O(1)MediumEuclidean Algorithm
372Super PowC++PythonO(n)O(1)Medium
382Linked List Random NodeC++PythonO(n)O(1)MediumReservoir Sampling
386Lexicographical NumbersC++PythonO(n)O(1)Medium
390Elimination GameC++PythonO(logn)O(1)Medium
391Perfect RectangleC++PythonO(n)O(n)Hard
398Random Pick IndexC++PythonO(n)O(1)MediumReservoir Sampling
400Nth DigitC++PythonO(logn)O(1)Easy
413Arithmetic SlicesC++PythonO(n)O(1)Medium
423Reconstruct Original Digits from EnglishC++PythonO(n)O(1)MediumGCJ2016 - Round 1B
441Arranging CoinsC++PythonO(nlogn)O(1)EasyBinary Search
453Minimum Moves to Equal Array ElementsC++PythonO(n)O(1)Easy
458Poor PigsC++PythonO(n)O(1)Easy
469Convex PolygonC++PythonO(n)O(1)Medium📖
6514 Keys KeyboardC++PythonO(1)O(1)Medium📖Math, DP
660Remove 9C++PythonO(logn)O(1)Hard📖
672Bulb Switcher IIC++PythonO(1)O(1)Medium

Sort

#TitleSolutionTimeSpaceDifficultyTagNote
56Merge IntervalsC++PythonO(nlogn)O(1)Hard
57Insert IntervalC++PythonO(n)O(1)Hard
75Sort ColorsC++PythonO(n)O(1)MediumTri Partition
88Merge Sorted ArrayC++PythonO(n)O(1)Easy
147Insertion Sort ListC++PythonO(n^2)O(1)Medium
148Sort ListC++PythonO(nlogn)O(logn)Medium
164Maximum GapC++PythonO(n)O(n)HardTricky
179Largest NumberC++PythonO(nlogn)O(1)Medium
218The Skyline ProblemC++PythonO(nlogn)O(n)HardSort, BST
252Meeting RoomsC++PythonO(nlogn)O(n)Easy📖
253Meeting Rooms IIC++PythonO(nlogn)O(n)Medium📖
274H-IndexC++PythonO(n)O(n)MediumCounting Sort
280Wiggle SortC++PythonO(n)O(1)Medium📖
324Wiggle Sort IIC++PythonO(n) on averageO(1)Mediumvariant of Sort ColorsTri Partition
347Top K Frequent ElementsC++PythonO(n) on averageO(n)MediumQuick Select, Heap
406Queue Reconstruction by HeightC++PythonO(n * sqrt(n))O(n)Medium
451Sort Characters By FrequencyC++PythonO(n)O(n)Medium

Two Pointers

#TitleSolutionTimeSpaceDifficultyTagNote
19Remove Nth Node From End of ListC++PythonO(n)O(1)Easy
86Partition ListC++PythonO(n)O(1)Medium
141Linked List CycleC++PythonO(n)O(1)Easy
142Linked List Cycle IIC++PythonO(n)O(1)Medium
143Reorder ListC++PythonO(n)O(1)Medium
167Two Sum II - Input array is sortedC++PythonO(n)O(1)Medium
2593Sum SmallerC++PythonO(n^2)O(1)Medium📖, LintCode
283Move ZeroesC++PythonO(n)O(1)Easy
287Find the Duplicate NumberC++PythonO(n)O(1)HardBinary Search, Two Pointers
344Reverse StringC++PythonO(n)O(1)Easy
345Reverse Vowels of a StringC++PythonO(n)O(1)Easy
349Intersection of Two ArraysC++PythonO(m + n)O(min(m, n))EasyEPIHash, Binary Search
350Intersection of Two Arrays IIC++PythonO(m + n)O(1)EasyEPIHash, Binary Search
360Sort Transformed ArrayC++PythonO(n)O(1)Medium📖
457Circular Array LoopC++PythonO(n)O(1)Medium

Recursion

#TitleSolutionTimeSpaceDifficultyTagNote
95Unique Binary Search Trees IIC++PythonO(4^n / n^(3/2)O(4^n / n^(3/2)Medium
98Validate Binary Search TreeC++PythonO(n)O(1)Medium
100Same TreeC+PythonO(n)O(h)Easy
104Maximum Depth of Binary TreeC++PythonO(n)O(h)Easy
105Construct Binary Tree from Preorder and Inorder TraversalC++PythonO(n)O(n)Medium
106Construct Binary Tree from Inorder and Postorder TraversalC++PythonO(n)O(n)Medium
108Convert Sorted Array to Binary Search TreeC++PythonO(n)O(logn)Medium
109Convert Sorted List to Binary Search TreeC++PythonO(n)O(logn)Medium
110Balanced Binary TreePythonO(n)O(h)Easy
111Minimum Depth of Binary TreePythonO(n)O(h)Easy
114Flatten Binary Tree to Linked ListPythonO(n)O(h)Medium
116Populating Next Right Pointers in Each NodePythonO(n)O(1)Medium
124Binary Tree Maximum Path SumPythonO(n)O(h)Hard
129Sum Root to Leaf NumbersPythonO(n)O(h)Medium
156Binary Tree Upside DownPythonO(n)O(1)Medium📖
241Different Ways to Add ParenthesesC++PythonO(n * 4^n / n^(3/2))O(n * 4^n / n^(3/2))Medium
298Binary Tree Longest Consecutive SequenceC++PythonO(n)O(h)Medium📖
327Count of Range SumC++PythonO(nlogn)O(n)Hard
333Largest BST SubtreeC++PythonO(n)O(h)Medium📖
337House Robber IIIC++PythonO(n)O(h)Medium
395Longest Substring with At Least K Repeating CharactersC++PythonO(n)O(1)Medium
404Sum of Left LeavesC++PythonO(n)O(h)Easy
437Path Sum IIIC++PythonO(n)O(h)Easy
549Binary Tree Longest Consecutive Sequence IIC++PythonO(n)O(h)Medium📖
669Trim a Binary Search TreeC++PythonO(n)O(h)Easy
671Second Minimum Node In a Binary TreeC++PythonO(n)O(h)Easy

Binary Search

#TitleSolutionTimeSpaceDifficultyTagNote
4Median of Two Sorted ArraysC++PythonO(log(min(m, n)))O(1)Hard
33Search in Rotated Sorted ArrayC++PythonO(logn)O(1)Hard
34Search for a RangeC++PythonO(logn)O(1)Medium
35Search Insert PositionC++PythonO(logn)O(1)Medium
69Sqrt(x)C++PythonO(logn)O(1)Medium
74Search a 2D MatrixC++PythonO(logm + logn)O(1)Medium
81Search in Rotated Sorted Array IIC++PythonO(logn)O(1)Medium
153Find Minimum in Rotated Sorted ArrayC++PythonO(logn)O(1)Medium
154Find Minimum in Rotated Sorted Array IIC++PythonO(logn) ~ O(n)O(1)Hard
162Find Peak ElementC++PythonO(logn)O(1)Medium
222Count Complete Tree NodesC++PythonO((logn)^2)O(1)Medium
275H-Index IIC++PythonO(logn)O(1)MediumBinary Search
278First Bad VersionC++PythonO(logn)O(1)EasyLintCode
300Longest Increasing SubsequenceC++PythonO(nlogn)O(n)MediumCTCI, LintCodeBinary Search, DP
302Smallest Rectangle Enclosing Black PixelsC++PythonO(nlogn)O(1)Hard📖
354Russian Doll EnvelopesC++PythonO(nlogn)O(1)Hard
363Max Sum of Rectangle No Larger Than KC++PythonO(min(m, n)^2 * max(m, n) * logn(max(m, n)))O(max(m, n))Hard
367Valid Perfect SquareC++PythonO(logn)O(1)Medium
374Guess Number Higher or LowerC++PythonO(logn)O(1)Easy
410Split Array Largest SumC++PythonO(nlogs)O(1)Hard
436Find Right IntervalC++PythonO(nlogn)O(n)Medium
475HeatersC++PythonO((m + n) * logn)O(1)Easy
658Find K Closest ElementsC++PythonO(logn + k)O(1)Medium
668Kth Smallest Number in Multiplication TableC++PythonO(m * log(m * n))O(1)Hard

Binary Search Tree

#TitleSolutionTimeSpaceDifficultyTagNote
220Contains Duplicate IIIC++PythonO(nlogk)O(k)Medium
230Kth Smallest Element in a BSTC++PythonO(max(h, k))O(min(h, k))Medium
235Lowest Common Ancestor of a Binary Search TreeC++PythonO(h)O(1)EasyEPI
270Closest Binary Search Tree ValueC++PythonO(h)O(1)Easy📖
285Inorder Successor in BSTC++PythonO(h)O(1)Medium📖
352Data Stream as Disjoint IntervalsC++PythonO(logn)O(n)Hard
449Serialize and Deserialize BSTC++PythonO(n)O(h)Medium
450Delete Node in a BSTC++PythonO(h)O(h)Medium

Breadth-First Search

#TitleSolutionTimeSpaceDifficultyTagNote
102Binary Tree Level Order TraversalC++PythonO(n)O(n)Easy
107Binary Tree Level Order Traversal IIPythonO(n)O(n)Easy
103Binary Tree Zigzag Level Order TraversalPythonO(n)O(n)Medium
117Populating Next Right Pointers in Each Node IIPythonO(n)O(1)Hard
127Word LadderPythonO(n * d)O(d)Medium
130Surrounded RegionsC++PythonO(m * n)O(m + n)Medium
133Clone GraphPythonO(n)O(n)Medium
207Course SchedulePythonO(|V| + |E|)O(|E|)MediumTopological Sort
210Course Schedule IIPythonO(|V| + |E|)O(|E|)MediumTopological Sort
261Graph Valid TreeC++PythonO(|V| + |E|)O(|V| + |E|)Medium📖
269Alien DictionaryC++PythonO(n)O(1)Hard📖Topological Sort, BFS, DFS
286Walls and GatesC++PythonO(m * n)O(g)Medium📖
310Minimum Height TreesC++PythonO(n)O(n)Medium
317Shortest Distance from All BuildingsC++PythonO(k * m * n)O(m * n)Hard📖
433Minimum Genetic MutationC++PythonO(n * b)O(b)Medium
444Sequence ReconstructionC++PythonO(n * s)O(n)Medium📖Topological Sort
666Path Sum IVC++PythonO(n)O(w)Medium📖Topological Sort
675Cut Off Trees for Golf EventC++PythonO(t * m * n)O(m * n)HardA* Search Algorithm

Depth-First Search

#TitleSolutionTimeSpaceDifficultyTagNote
112Path SumPythonO(n)O(h)Easy
113Path Sum IIPythonO(n)O(h)Medium
199Binary Tree Right Side ViewPythonO(n)O(h)Medium
200Number of IslandsPythonO(m * n)O(m * n)Medium
236Lowest Common Ancestor of a Binary TreeC++PythonO(n)O(h)MediumEPI
247Strobogrammatic Number IIC++PythonO(n^2 * 5^(n/2))O(n)Medium📖
250Count Univalue SubtreesC++PythonO(n)O(h)Medium📖
257Binary Tree PathsC++PythonO(n * h)O(h)Easy
282Expression Add OperatorsC++PythonO(4^n)O(n)Hard
301Remove Invalid ParenthesesC++PythonO(C(n, c))O(c)Hard
329Longest Increasing Path in a MatrixC++PythonO(m * n)O(m * n)Hard
332Reconstruct ItineraryC++PythonO(t! / (n1! * n2! * ... nk!))O(t)Medium
339Nested List Weight SumC++PythonO(n)O(h)Easy📖
364Nested List Weight Sum IIC++PythonO(n)O(h)Medium📖
366Find Leaves of Binary TreeC++PythonO(n)O(h)Medium📖
399Evaluate DivisionC++PythonO(q * |V|!)O(e)Medium
417Pacific Atlantic Water FlowC++PythonO(m * n)O(m * n)Medium
440K-th Smallest in Lexicographical OrderC++PythonO(logn)O(logn)Hard
464Can I WinC++PythonO(n!)O(n)Medium

Backtracking

#TitleSolutionTimeSpaceDifficultyTagNote
17Letter Combinations of a Phone NumberPythonO(n * 4^n)O(n)Medium
22Generate ParenthesesPythonO(4^n / n^(3/2))O(n)Medium
37Sudoku SolverPythonO((9!)^9)O(1)Hard
39Combination SumPythonO(k * n^k)O(k)Medium
40Combination Sum IIPythonO(k * C(n, k))O(k)Medium
46PermutationsPythonO(n * n!)O(n)Medium
47Permutations IIPythonO(n * n!)O(n)Medium
51N-QueensPythonO(n!)O(n)Hard
52N-Queens-IIPythonO(n!)O(n)Hard
77CombinationsPythonO(n!)O(n)Medium
79Word SearchPythonO(m * n * l)O(l)Medium
93Restore IP AddressesPythonO(1)O(1)Medium
78SubsetsC++PythonO(n * 2^n)O(1)Medium
90Subsets IIC++PythonO(n * 2^n)O(1)Medium
126Word Ladder IIPythonO(n * d)O(d)Hard
131Palindrome PartitioningPythonO(n^2) ~ O(2^n)O(n^2)Medium
140Word Break IIC++PythonO(n * l^2 + n * r)O(n^2)Hard
212Word Search IIC++PythonO(m * n * l)O(l)HardLintCodeTrie, DFS
216Combination Sum IIIC++PythonO(k * C(n, k))O(k)Medium
254Factor CombinationsC++PythonO(nlogn)O(logn)Medium📖
267Palindrome Permutation IIC++PythonO(n * n!)O(n)Medium📖
291Word Pattern IIC++PythonO(n * C(n - 1, c - 1))O(n + c)Hard📖
294Flip Game IIC++PythonO(n + c^2)O(c)Medium📖DP, Hash
320Generalized AbbreviationC++PythonO(n * 2^n)O(n)Medium📖
425Word SquaresC++PythonO(n^2 * n!)O(n^2)Hard📖
676Implement Magic DictionaryC++PythonO(n)O(d)MediumTrie, DFS
67924 GameC++PythonO(1)O(1)HardDFS

Dynamic Programming

#TitleSolutionTimeSpaceDifficultyTagNote
10Regular Expression MatchingPythonO(m * n)O(n)Hard
53Maximum SubarrayPythonO(n)O(1)Medium
62Unique PathsPythonO(m * n)O(m + n)Medium
63Unique Paths IIPythonO(m * n)O(m + n)Medium
64Minimum Path SumPythonO(m * n)O(m + n)Medium
70Climbing StairsPythonO(n)O(1)Easy
72Edit DistancePythonO(m * n)O(m + n)Hard
87Scramble StringPythonO(n^4)O(n^3)Hard
91Decode WaysC++PythonO(n)O(1)Medium
96Unique Binary Search TreesPythonO(n)O(1)MediumMath
97Interleaving StringPythonO(m * n)O(m + n)Hard
115Distinct SubsequencesPythonO(n^2)O(n)Hard
120TrianglePythonO(m * n)O(n)Medium
123Best Time to Buy and Sell Stock IIIPythonO(n)O(1)Hard
132Palindrome Partitioning IIPythonO(n^2)O(n^2)Hard
139Word BreakC++PythonO(n * l^2)O(n)Medium
152Maximum Product SubarrayPythonO(n)O(1)Medium
174Dungeon GamePythonO(m * n)O(m + n)Hard
188Best Time to Buy and Sell Stock IVPythonO(k * n)O(k)Hard
198House RobberPythonO(n)O(1)Easy
213House Robber IIC++PythonO(n)O(1)Medium
221Maximal SquareC++PythonO(n^2)O(n)MediumEPI
256Paint HouseC++PythonO(n)O(1)Medium📖
265Paint House IIC++PythonO(n * k)O(k)Hard📖
276Paint FenceC++PythonO(n)O(1)Easy📖
279Perfect SquaresC++PythonO(n * sqrt(n))O(n)MediumHash
303Range Sum Query - ImmutableC++Pythonctor: O(n), lookup: O(1)O(n)Easy
304Range Sum Query 2D - ImmutableC++Pythonctor: O(m * n), lookup: O(1)O(m * n)Medium
309Best Time to Buy and Sell Stock with CooldownC++PythonO(n)O(1)Medium
312Burst BalloonsC++PythonO(n^3)O(n^2)Hard
322Coin ChangeC++PythonO(n * k)O(k)Medium
351Android Unlock PatternsC++PythonO(9^2 * 2^9)O(9 * 2^9)Medium📖Backtracking
357Count Numbers with Unique DigitsC++PythonO(n)O(1)MediumBacktracking, Math
361Bomb EnemyC++PythonO(m * n)O(m * n)Medium📖
368Largest Divisible SubsetC++PythonO(n^2)O(n)Medium
375Guess Number Higher or Lower IIC++PythonO(n^2)O(n^2)Medium
377Combination Sum IVC++PythonO(nlogn + n * t)O(t)Medium
403Frog JumpC++PythonO(n)O(n) ~ O(n^2)Hard
416Partition Equal Subset SumC++PythonO(n * s)O(s)Medium
418Sentence Screen FittingC++PythonO(r + n * c)O(n)Medium📖
446Arithmetic Slices II - SubsequenceC++PythonO(n^2)O(n * d)Hard
465Optimal Account BalancingC++PythonO(n * 2^n)O(n * 2^n)Hard📖
466Count The RepetitionsC++PythonO(s1 * min(s2, n1))O(s2)Hard
467Unique Substrings in Wraparound StringC++PythonO(n)O(1)Medium
471Encode String with Shortest LengthC++PythonO(n^3) on averageO(n^2)Medium📖
472Concatenated WordsC++PythonO(n * l^2)O(n * l)Medium
474Ones and ZeroesC++PythonO(s * m * n)O(m * n)Medium
6502 Keys KeyboardC++PythonO(sqrt(n))O(1)Medium
656Coin PathC++PythonO(n * B)O(n)Hard📖
664Strange PrinterC++PythonO(n^3)O(n^2)Hard
673Number of Longest Increasing SubsequenceC++PythonO(n^2)O(n)Medium

Greedy

#TitleSolutionTimeSpaceDifficultyTagNote
11Container With Most WaterC++PythonO(n)O(1)Medium
42Trapping Rain WaterC++PythonO(n)O(1)HardTricky
44Wildcard MatchingPythonO(m + n)O(1)HardTricky
45Jump Game IIPythonO(n)O(1)Hard
55Jump GamePythonO(n)O(1)Medium
122Best Time to Buy and Sell Stock IIPythonO(n)O(1)Medium
134Gas StationPythonO(n)O(1)Medium
135CandyC++PythonO(n)O(n)Hard
316Remove Duplicate LettersC++PythonO(n)O(k)HardAscending Stack
321Create Maximum NumberC++PythonO(k * (m + n + k)) ~ O(k * (m + n + k^2))O(m + n + k^2)Hardvariant of Delete DigitsGreedy, DP
330Patching ArrayC++PythonO(s + logn)O(1)Hard
376Wiggle SubsequenceC++PythonO(n)O(1)Medium
392Is SubsequenceC++PythonO(n)O(1)Medium
397Integer ReplacementC++PythonO(n)O(1)MediumMath
402Remove K DigitsC++PythonO(n)O(n)MediumLintCode
435Non-overlapping IntervalsC++PythonO(nlogn)O(1)Medium
452Minimum Number of Arrows to Burst BalloonsC++PythonO(nlogn)O(1)Medium
455Assign CookiesC++PythonO(nlogn)O(1)Easy
646Maximum Length of Pair ChainC++PythonO(nlogn)O(1)MediumScan Line
649Dota2 SenateC++PythonO(n)O(n)Medium
659Split Array into Consecutive SubsequencesC++PythonO(n)O(1)Medium

Design

#TitleSolutionTimeSpaceDifficultyTagNote
146LRU CacheC++PythonO(1)O(k)Hard
225Implement Stack using QueuesC++Pythonpush: O(n), pop: O(1), top: O(1)O(n)Easy
284Peeking IteratorC++PythonO(1)O(1)Medium
348Design Tic-Tac-ToeC++PythonO(1)O(n^2)Medium📖
353Design Snake GameC++PythonO(1)O(s)Medium📖Deque
355Design TwitterC++PythonO(klogu)O(t + f)MediumLintCodeHeap
359Logger Rate LimiterC++PythonO(1), amortizedO(k)Easy📖Deque
362Design Hit CounterC++PythonO(1), amortizedO(k)Medium📖Deque
379Design Phone DirectoryC++PythonO(1)O(n)Medium📖
380Insert Delete GetRandom O(1)C++PythonO(1)O(n)Hard
381Insert Delete GetRandom O(1) - Duplicates allowedC++PythonO(1)O(n)Hard
432All O`one Data StructureC++PythonO(1)O(n)Hard
460LFU CacheC++PythonO(1)O(k)Hard

SQL

#TitleSolutionTimeSpaceDifficultyTagNote
175Combine Two TablesMySQLO(m + n)O(m + n)Easy
176Second Highest SalaryMySQLO(n)O(1)Easy
177Nth Highest SalaryMySQLO(n^2)O(n)Medium
178Rank ScoresMySQLO(n^2)O(n)Medium
180Consecutive NumbersMySQLO(n)O(n)Medium
181Employees Earning More Than Their ManagersMySQLO(n^2)O(1)Easy
182Duplicate EmailsMySQLO(n^2)O(n)Easy
183Customers Who Never OrderMySQLO(n^2)O(1)Easy
184Department Highest SalaryMySQLO(n^2)O(n)Medium
185Department Top Three SalariesMySQLO(n^2)O(n)Hard
196Delete Duplicate EmailsMySQLO(n^2)O(n)Easy
197Rising TemperatureMySQLO(n^2)O(n)Easy
262Trips and UsersMySQLO((t * u) + tlogt)O(t)Hard

Shell Script

#TitleSolutionTimeSpaceDifficultyTagNote
192Word FrequencyShellO(n)O(k)Medium
193Valid Phone NumbersShellO(n)O(1)Easy
194Transpose FileShellO(n^2)O(n^2)Medium
195Tenth LineShellO(n)O(1)Easy

About

📝 Python / C++ 11 Solutions of LeetCode Questions

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { // Highlight search terms from Google/DuckDuckGo/Bing referrer (function() { var ref = document.referrer; var terms = []; if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) { var url = new URL(ref); var q = url.searchParams.get('q') || url.searchParams.get('p'); if (q) { terms = q.split(/\s+/).filter(function(t) { return t.length > 2; }); } } if (terms.length === 0) return; var style = document.createElement('style'); style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }'; document.head.appendChild(style); function highlight(node) { if (node.nodeType === 3) { // text node var text = node.textContent; var found = false; terms.forEach(function(term) { var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\]\\]/g, '\\') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' GitHub - shinan6/LeetCode: :pencil: Python / C++ 11 Solutions of LeetCode Questions · GitHub
Skip to content

Latest commit

History

4,201 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

The number of LeetCode questions is increasing every week. For more questions and solutions, you can see my LintCode repository. I'll keep updating for full summary and better solutions. Stay tuned for updates. (Notes: "📖" means you need to subscribe to LeetCode premium membership for the access to premium questions.)

Algorithms

Database

Shell

Bit Manipulation

#TitleSolutionTimeSpaceDifficultyTagNote
136Single NumberC++PythonO(n)O(1)Easy
137Single Number IIC++PythonO(n)O(1)Medium
190Reverse BitsC++PythonO(1)O(1)Easy
191Number of 1 BitsC++PythonO(1)O(1)Easy
201Bitwise AND of Numbers RangeC++PythonO(1)O(1)Medium
231Power of TwoC++PythonO(1)O(1)EasyLintCode
260Single Number IIIC++PythonO(n)O(1)Medium
268Missing NumberC++PythonO(n)O(1)MediumLintCode
318Maximum Product of Word LengthsC++PythonO(n) ~ O(n^2)O(n)MediumBit Manipulation, Counting Sort, Pruning
342Power of FourC++PythonO(1)O(1)Easy
371Sum of Two IntegersC++PythonO(1)O(1)EasyLintCode
389Find the DifferenceC++PythonO(n)O(1)Easy
393UTF-8 ValidationC++PythonO(n)O(1)Medium
401Binary WatchC++PythonO(1)O(1)Easy
411Minimum Unique Word AbbreviationC++PythonO(2^n)O(n)Hard📖
421Maximum XOR of Two Numbers in an ArrayC++PythonO(n)O(1)Medium
461Hamming DistanceC++PythonO(1)O(1)Easy
462Minimum Moves to Equal Array Elements IIC++PythonO(n) on averageO(1)Medium
477Total Hamming DistanceC++PythonO(n)O(1)Medium
645Set MismatchC++PythonO(n)O(1)Easy

Array

#TitleSolutionTimeSpaceDifficultyTagNote
153 SumC++PythonO(n^2)O(1)MediumTwo Pointers
163 Sum ClosestC++PythonO(n^2)O(1)MediumTwo Pointers
184 SumC++PythonO(n^3)O(1)MediumTwo Pointers
26Remove Duplicates from Sorted ArrayC++PythonO(n)O(1)EasyTwo Pointers
27Remove ElementC++PythonO(n)O(1)Easy
31Next PermutationC++PythonO(n)O(1)MediumTricky
41First Missing PositiveC++PythonO(n)O(1)HardTricky
48Rotate ImageC++PythonO(n^2)O(1)Medium
54Spiral MatrixC++PythonO(m * n)O(1)Medium
59Spiral Matrix IIC++PythonO(n^2)O(1)Medium
66Plus OneC++PythonO(n)O(1)Easy
73Set Matrix ZeroesC++PythonO(m * n)O(1)Medium
80Remove Duplicates from Sorted Array IIC++PythonO(n)O(1)MediumTwo Pointers
118Pascal's TriangleC++PythonO(n^2)O(1)Easy
119Pascal's Triangle IIC++PythonO(n^2)O(1)Easy
121Best Time to Buy and Sell StockC++PythonO(n)O(1)Easy
128Longest Consecutive SequenceC++PythonO(n)O(n)HardTricky
157Read N Characters Given Read4C++PythonO(n)O(1)Easy📖
158Read N Characters Given Read4 II - Call multiple timesC++PythonO(n)O(1)Hard📖
163Missing RangesC++PythonO(n)O(1)Medium📖
169Majority ElementC++PythonO(n)O(1)Easy
189Rotate ArrayC++PythonO(n)O(1)Easy
209Minimum Size Subarray SumC++PythonO(n)O(1)MediumBinary Search
215Kth Largest Element in an ArrayC++PythonO(n) ~ O(n^2)O(1)MediumEPI
228Summary RangesC++PythonO(n)O(1)Medium
229Majority Element IIC++PythonO(n)O(1)Medium
238Product of Array Except SelfC++PythonO(n)O(1)MediumLintCode
240Search a 2D Matrix IIC++PythonO(m + n)O(1)MediumEPI, LintCode
243Shortest Word DistanceC++PythonO(n)O(1)Easy📖
245Shortest Word Distance IIIC++PythonO(n)O(1)Medium📖
251Flatten 2D VectorC++PythonO(1)O(1)Medium📖
277Find the CelebrityC++PythonO(n)O(1)Medium📖, EPI
289Game of LifeC++PythonO(m * n)O(1)Medium
293Flip GameC++PythonO(n * (c+1))O(1)Easy📖
296Best Meeting PointC++PythonO(m * n)O(m + n)Hard📖
311Sparse Matrix MultiplicationC++PythonO(m * n * l)O(m * l)Medium📖
334Increasing Triplet SubsequenceC++PythonO(n)O(1)Medium
370Range AdditionC++PythonO(k + n)O(1)Medium📖
384Shuffle an ArrayC++PythonO(n)O(n)MediumEPI
396Rotate FunctionC++PythonO(n)O(1)Easy
412Fizz BuzzC++PythonO(n)O(1)Easy
414Third Maximum NumberC++PythonO(n)O(1)Easy
419Battleships in a BoardC++PythonO(m * n)O(1)Medium
422Valid Word SquareC++PythonO(m * n)O(1)Easy📖
442Find All Duplicates in an ArrayC++PythonO(n)O(1)Medium
448Find All Numbers Disappeared in an ArrayC++PythonO(n)O(1)Easy
661Image SmootherC++PythonO(m * n)O(1)Easy
665Non-decreasing ArrayC++PythonO(n)O(1)Easy
667Beautiful Arrangement IIC++PythonO(n)O(1)Medium
670Maximum SwapC++PythonO(logn)O(logn)Medium
674Longest Continuous Increasing SubsequenceC++PythonO(n)O(1)Easy

String

#TitleSolutionTimeSpaceDifficultyTagNote
5Longest Palindromic SubstringC++PythonO(n)O(n)MediumManacher's Algorithm
6ZigZag ConversionC++PythonO(n)O(1)Easy
8String to Integer (atoi)C++PythonO(n)O(1)Easy
14Longest Common PrefixC++PythonO(n * k)O(1)Easy
28Implement strStr()C++PythonO(n + k)O(k)EasyKMP Algorithm
38Count and SayC++PythonO(n * 2^n)O(2^n)Easy
43Multiply StringsC++PythonO(m * n)O(m + n)Medium
58Length of Last WordC++PythonO(n)O(1)Easy
67Add BinaryC++PythonO(n)O(1)Easy
68Text JustificationC++PythonO(n)O(1)Hard
125Valid PalindromeC++PythonO(n)O(1)Easy
151Reverse Words in a StringC++PythonO(n)O(1)Medium
161One Edit DistanceC++PythonO(m + n)O(1)Medium📖
165Compare Version NumbersC++PythonO(n)O(1)Easy
186Reverse Words in a String IIC++PythonO(n)O(1)Medium📖
214Shortest PalindromeC++PythonO(n)O(n)HardKMP AlgorithmManacher's Algorithm
242Valid AnagramC++PythonO(n)O(1)EasyLintCode
271Encode and Decode StringsC++PythonO(n)O(1)Medium📖
273Integer to English WordsC++PythonO(logn)O(1)Hard
306Addictive NumberC++PythonO(n^3)O(n)Medium
383Ransom NoteC++PythonO(n)O(1)EasyEPI
405Convert a Number to HexadecimalC++PythonO(n)O(1)Easy
408Valid Word AbbreviationC++PythonO(n)O(1)Easy📖
415Add StringsC++PythonO(n)O(1)Easy
420Strong Password CheckerC++PythonO(n)O(1)Hard
434Number of Segments in a StringC++PythonO(n)O(1)Easy
459Repeated Substring PatternC++PythonO(n)O(n)EasyKMP Algorithm
468Validate IP AddressC++PythonO(1)O(1)Medium
556Next Greater Element IIIC++PythonO(1)O(1)Medium
557Reverse Words in a String IIIC++PythonO(n)O(1)Easy
647Palindromic SubstringsC++PythonO(n)O(n)MediumManacher's Algorithm
648Replace WordsC++PythonO(n)O(t)Mediumtrie
657Judge Route CircleC++PythonO(n)O(1)Easy
678Valid Parenthesis StringC++PythonO(n)O(1)Medium
680Valid Palindrome IIC++PythonO(n)O(1)Easy

Linked List

#TitleSolutionTimeSpaceDifficultyTagNote
2Add Two NumbersC++PythonO(n)O(1)Medium
21Merge Two Sorted ListsC++PythonO(n)O(1)Easy
23Merge k Sorted ListsC++PythonO(nlogk)O(1)HardHeap, Divide and Conquer
24Swap Nodes in PairsC++PythonO(n)O(1)Easy
25Reverse Nodes in k-GroupC++PythonO(n)O(1)Hard
61Rotate ListC++PythonO(n)O(1)Medium
82Remove Duplicates from Sorted List IIC++PythonO(n)O(1)Medium
83Remove Duplicates from Sorted ListC++PythonO(n)O(1)Easy
92Reverse Linked List IIC++PythonO(n)O(1)Medium
138Copy List with Random PointerC++PythonO(n)O(1)Hard
160Intersection of Two Linked ListsC++PythonO(m + n)O(1)Easy
203Remove Linked List ElementsC++PythonO(n)O(1)Easy
206Reverse Linked ListC++PythonO(n)O(1)Easy
234Palindrome Linked ListC++PythonO(n)O(1)Easy
237Delete Node in a Linked ListC++PythonO(1)O(1)EasyLintCode
328Odd Even Linked ListC++PythonO(n)O(1)Medium
369Plus One Linked ListC++PythonO(n)O(1)Medium📖Two Pointers
445Add Two Numbers IIC++PythonO(m + n)O(m + n)Medium

Stack

#TitleSolutionTimeSpaceDifficultyTagNote
20Valid ParenthesesC++PythonO(n)O(n)Easy
32Longest Valid ParenthesesC++PythonO(n)O(1)Hard
71Simplify PathC++PythonO(n)O(n)Medium
84Largest Rectangle in HistogramC++PythonO(n)O(n)HardAscending Stack, DP
85Maximal RectangleC++PythonO(m * n)O(n)HardEPIAscending Stack
101Symmetric TreeC++PythonO(n)O(h)Easy
150Evaluate Reverse Polish NotationC++PythonO(n)O(n)Medium
155Min StackC++PythonO(n)O(1)Easy
173Binary Search Tree IteratorC++PythonO(1)O(h)Medium
224Basic CalculatorC++PythonO(n)O(n)Hard
227Basic Calculator IIC++PythonO(n)O(n)Medium
232Implement Queue using StacksC++PythonO(1), amortizedO(n)EasyEPI, LintCode
255Verify Preorder Sequence in Binary Search TreeC++PythonO(n)O(1)Medium📖
272Closest Binary Search Tree Value IIC++PythonO(h + k)O(h)Hard📖
331Verify Preorder Serialization of a Binary TreeC++PythonO(n)O(1)Medium
341Flatten Nested List IteratorC++PythonO(n)O(h)Medium📖Iterator
385Mini ParserC++PythonO(n)O(h)Medium
394Decode StringC++PythonO(n)O(h)Medium
439Ternary Expression ParserC++PythonO(n)O(1)Medium📖
456132 PatternC++PythonO(n)O(n)Medium

Queue

#TitleSolutionTimeSpaceDifficultyTagNote
239Sliding Window MaximumC++PythonO(n)O(k)HardEPI, LintCode
281Zigzag IteratorC++PythonO(n)O(k)Medium📖
346Moving Average from Data StreamC++PythonO(1)O(w)Easy📖

Heap

#TitleSolutionTimeSpaceDifficultyTagNote
264Ugly Number IIC++PythonO(n)O(1)MediumCTCI, LintCodeBST, Heap
295Find Median from Data StreamC++PythonO(nlogn)O(n)HardEPI, LintCodeBST, Heap
313Super Ugly NumberC++PythonO(n * k)O(n + k)MediumBST, Heap
358Rearrange String k Distance ApartC++PythonO(n)O(n)Hard📖Greedy, Heap
373Find K Pairs with Smallest SumsC++PythonO(k * log(min(n, m, k)))O(min(n, m, k))Medium
378Kth Smallest Element in a Sorted MatrixC++PythonO(k * log(min(n, m, k)))O(min(n, m, k))MediumLintCode
407Trapping Rain Water IIC++PythonO(m * n * (logm + logn))O(m * n)HardLintCode

Tree

#TitleSolutionTimeSpaceDifficultyTagNote
94Binary Tree Inorder TraversalC++PythonO(n)O(1)MediumMorris Traversal
99Recover Binary Search TreeC++PythonO(n)O(1)HardMorris Traversal
144Binary Tree Preorder TraversalC++PythonO(n)O(1)MediumMorris Traversal
145Binary Tree Postorder TraversalC++PythonO(n)O(1)HardMorris Traversal
208Implement Trie (Prefix Tree)C++PythonO(n)O(1)MediumTrie
211Add and Search Word - Data structure designC++PythonO(min(n, h))O(min(n, h))MediumTrie, DFS
226Invert Binary TreeC++PythonO(n)O(h), O(w)Easy
297Serialize and Deserialize Binary TreeC++PythonO(n)O(h)HardLintCodeDFS
307Range Sum Query - MutableC++Pythonctor: O(n), update: O(logn), query: O(logn)O(n)MediumLintCodeDFS, Segment Tree, BIT
308Range Sum Query 2D - MutableC++Pythonctor: O(m * n), update: O(logm + logn), query: O(logm + logn)O(m * n)Hard📖DFS, Segment Tree, BIT
315Count of Smaller Numbers After SelfC++PythonO(nlogn)O(n)HardLintCodeBST, BIT, Divide and Conquer
652Find Duplicate SubtreesC++PythonO(n)O(n)MediumDFS, Hash
653Two Sum IV - Input is a BSTC++PythonO(n)O(h)EasyTwo Pointers
654Maximum Binary TreeC++PythonO(n)O(n)MediumLintCodeDescending Stack
655Print Binary TreeC++PythonO(n)O(h)Medium
662Maximum Width of Binary TreeC++PythonO(n)O(h)MediumDFS
663Equal Tree PartitionC++PythonO(n)O(n)Medium📖Hash
677Map Sum PairsC++PythonO(n)O(t)MediumTrie

Hash Table

#TitleSolutionTimeSpaceDifficultyTagNote
1Two SumC++PythonO(n)O(n)Easy
3Longest Substring Without Repeating CharactersC++PythonO(n)O(1)Medium
30Substring with Concatenation of All WordsC++PythonO(m * n * k)O(n * k)Hard
36Valid SudokuC++PythonO(9^2)O(9)Easy
49Group AnagramsC++PythonO(n * glogg)O(n)Medium
76Minimum Window SubstringC++PythonO(n)O(k)Hard
149Max Points on a LineC++PythonO(n^2)O(n)Hard
159Longest Substring with At Most Two Distinct CharactersC++PythonO(n)O(1)Hard📖
170Two Sum III - Data structure designC++PythonO(n)O(n)Easy📖
187Repeated DNA SequencesPythonO(n)O(n)Medium
202Happy NumberC++PythonO(k)O(k)Easy
204Count PrimesC++PythonO(n)O(n)Easy
205Isomorphic StringsC++PythonO(n)O(1)Easy
217Contains DuplicateC++PythonO(n)O(n)Easy
219Contains Duplicate IIC++PythonO(n)O(n)Easy
244Shortest Word Distance IIC++Pythonctor: O(n), lookup: O(a + b)O(n)Medium📖
246Strobogrammatic NumberC++PythonO(n)O(1)Easy📖
249Group Shifted StringsC++PythonO(nlogn)O(n)Easy📖
266Palindrome PermutationC++PythonO(n)O(1)Easy📖
288Unique Word AbbreviationC++Pythonctor: O(n), lookup: O(1)O(k)Easy📖
290Word PatternC++PythonO(n)O(c)Easyvariant of Isomorphic Strings
299Bulls and CowsC++PythonO(n)O(1)Easy
305Number of Islands IIC++PythonO(k)O(k)HardLintCode, 📖Union Find
314Binary Tree Vertical Order TraversalC++PythonO(n)O(n)Medium📖BFS
323Number of Connected Components in an Undirected GraphC++PythonO(n)O(n)Medium📖Union Find
325Maximum Size Subarray Sum Equals kC++PythonO(n)O(n)Medium📖
336Palindrome PairsC++PythonO(n * k^2)O(n * k)Hard
340Longest Substring with At Most K Distinct CharactersC++PythonO(n)O(1)Hard📖
356Line ReflectionC++PythonO(n)O(n)Medium📖Hash, Two Pointers
387First Unique Character in a StringC++PythonO(n)O(n)Easy
388Longest Absolute File PathC++PythonO(n)O(d)MediumStack
409Longest PalindromeC++PythonO(n)O(1)Easy
424Longest Repeating Character ReplacementC++PythonO(n)O(1)Medium
438Find All Anagrams in a StringC++PythonO(n)O(1)Easy
447Number of BoomerangsC++PythonO(n^2)O(n)Easy
4544Sum IIC++PythonO(n^2)O(n^2)Medium
473Matchsticks to SquareC++PythonO(n * s * 2^n)O(n * (2^n + s))Medium
554Brick WallC++PythonO(n)O(m)Medium

Math

#TitleSolutionTimeSpaceDifficultyTagNote
7Reverse IntegerC++PythonO(1)O(1)Easy
9Palindrome NumberC++PythonO(1)O(1)Easy
12Integer to RomanC++PythonO(n)O(1)Medium
13Roman to IntegerC++PythonO(n)O(1)Easy
29Divide Two IntegersC++PythonO(1)O(1)Medium
50Pow(x, n)C++PythonO(1)O(1)Medium
60Permutation SequenceC++PythonO(n^2)O(n)MediumCantor Ordering
65Valid NumberC++PythonO(n)O(1)HardAutomata
89Gray CodeC++PythonO(2^n)O(1)Medium
166Fraction to Recurring DecimalC++PythonO(logn)O(1)Medium
168Excel Sheet Column TitleC++PythonO(logn)O(1)Easy
171Excel Sheet Column NumberC++PythonO(n)O(1)Easy
172Factorial Trailing ZeroesC++PythonO(1)O(1)Easy
223Rectangle AreaC++PythonO(1)O(1)Easy
233Number of Digit OneC++PythonO(1)O(1)HardCTCI, LintCode
248Strobogrammatic Number IIIC++PythonO(5^(n/2))O(n)Hard📖
258Add DigitsC++PythonO(1)O(1)Easy
263Ugly NumberC++PythonO(1)O(1)Easy
292Nim GameC++PythonO(1)O(1)EasyLintCode
319Bulb SwitcherC++PythonO(1)O(1)Medium
326Power of ThreeC++PythonO(1)O(1)Easy
335Self CrossingC++PythonO(n)O(1)Hard
338Counting BitsC++PythonO(n)O(n)Medium
343Integer BreakC++PythonO(logn)O(1)MediumTricky, DP
365Water and Jug ProblemC++PythonO(logn)O(1)MediumEuclidean Algorithm
372Super PowC++PythonO(n)O(1)Medium
382Linked List Random NodeC++PythonO(n)O(1)MediumReservoir Sampling
386Lexicographical NumbersC++PythonO(n)O(1)Medium
390Elimination GameC++PythonO(logn)O(1)Medium
391Perfect RectangleC++PythonO(n)O(n)Hard
398Random Pick IndexC++PythonO(n)O(1)MediumReservoir Sampling
400Nth DigitC++PythonO(logn)O(1)Easy
413Arithmetic SlicesC++PythonO(n)O(1)Medium
423Reconstruct Original Digits from EnglishC++PythonO(n)O(1)MediumGCJ2016 - Round 1B
441Arranging CoinsC++PythonO(nlogn)O(1)EasyBinary Search
453Minimum Moves to Equal Array ElementsC++PythonO(n)O(1)Easy
458Poor PigsC++PythonO(n)O(1)Easy
469Convex PolygonC++PythonO(n)O(1)Medium📖
6514 Keys KeyboardC++PythonO(1)O(1)Medium📖Math, DP
660Remove 9C++PythonO(logn)O(1)Hard📖
672Bulb Switcher IIC++PythonO(1)O(1)Medium

Sort

#TitleSolutionTimeSpaceDifficultyTagNote
56Merge IntervalsC++PythonO(nlogn)O(1)Hard
57Insert IntervalC++PythonO(n)O(1)Hard
75Sort ColorsC++PythonO(n)O(1)MediumTri Partition
88Merge Sorted ArrayC++PythonO(n)O(1)Easy
147Insertion Sort ListC++PythonO(n^2)O(1)Medium
148Sort ListC++PythonO(nlogn)O(logn)Medium
164Maximum GapC++PythonO(n)O(n)HardTricky
179Largest NumberC++PythonO(nlogn)O(1)Medium
218The Skyline ProblemC++PythonO(nlogn)O(n)HardSort, BST
252Meeting RoomsC++PythonO(nlogn)O(n)Easy📖
253Meeting Rooms IIC++PythonO(nlogn)O(n)Medium📖
274H-IndexC++PythonO(n)O(n)MediumCounting Sort
280Wiggle SortC++PythonO(n)O(1)Medium📖
324Wiggle Sort IIC++PythonO(n) on averageO(1)Mediumvariant of Sort ColorsTri Partition
347Top K Frequent ElementsC++PythonO(n) on averageO(n)MediumQuick Select, Heap
406Queue Reconstruction by HeightC++PythonO(n * sqrt(n))O(n)Medium
451Sort Characters By FrequencyC++PythonO(n)O(n)Medium

Two Pointers

#TitleSolutionTimeSpaceDifficultyTagNote
19Remove Nth Node From End of ListC++PythonO(n)O(1)Easy
86Partition ListC++PythonO(n)O(1)Medium
141Linked List CycleC++PythonO(n)O(1)Easy
142Linked List Cycle IIC++PythonO(n)O(1)Medium
143Reorder ListC++PythonO(n)O(1)Medium
167Two Sum II - Input array is sortedC++PythonO(n)O(1)Medium
2593Sum SmallerC++PythonO(n^2)O(1)Medium📖, LintCode
283Move ZeroesC++PythonO(n)O(1)Easy
287Find the Duplicate NumberC++PythonO(n)O(1)HardBinary Search, Two Pointers
344Reverse StringC++PythonO(n)O(1)Easy
345Reverse Vowels of a StringC++PythonO(n)O(1)Easy
349Intersection of Two ArraysC++PythonO(m + n)O(min(m, n))EasyEPIHash, Binary Search
350Intersection of Two Arrays IIC++PythonO(m + n)O(1)EasyEPIHash, Binary Search
360Sort Transformed ArrayC++PythonO(n)O(1)Medium📖
457Circular Array LoopC++PythonO(n)O(1)Medium

Recursion

#TitleSolutionTimeSpaceDifficultyTagNote
95Unique Binary Search Trees IIC++PythonO(4^n / n^(3/2)O(4^n / n^(3/2)Medium
98Validate Binary Search TreeC++PythonO(n)O(1)Medium
100Same TreeC+PythonO(n)O(h)Easy
104Maximum Depth of Binary TreeC++PythonO(n)O(h)Easy
105Construct Binary Tree from Preorder and Inorder TraversalC++PythonO(n)O(n)Medium
106Construct Binary Tree from Inorder and Postorder TraversalC++PythonO(n)O(n)Medium
108Convert Sorted Array to Binary Search TreeC++PythonO(n)O(logn)Medium
109Convert Sorted List to Binary Search TreeC++PythonO(n)O(logn)Medium
110Balanced Binary TreePythonO(n)O(h)Easy
111Minimum Depth of Binary TreePythonO(n)O(h)Easy
114Flatten Binary Tree to Linked ListPythonO(n)O(h)Medium
116Populating Next Right Pointers in Each NodePythonO(n)O(1)Medium
124Binary Tree Maximum Path SumPythonO(n)O(h)Hard
129Sum Root to Leaf NumbersPythonO(n)O(h)Medium
156Binary Tree Upside DownPythonO(n)O(1)Medium📖
241Different Ways to Add ParenthesesC++PythonO(n * 4^n / n^(3/2))O(n * 4^n / n^(3/2))Medium
298Binary Tree Longest Consecutive SequenceC++PythonO(n)O(h)Medium📖
327Count of Range SumC++PythonO(nlogn)O(n)Hard
333Largest BST SubtreeC++PythonO(n)O(h)Medium📖
337House Robber IIIC++PythonO(n)O(h)Medium
395Longest Substring with At Least K Repeating CharactersC++PythonO(n)O(1)Medium
404Sum of Left LeavesC++PythonO(n)O(h)Easy
437Path Sum IIIC++PythonO(n)O(h)Easy
549Binary Tree Longest Consecutive Sequence IIC++PythonO(n)O(h)Medium📖
669Trim a Binary Search TreeC++PythonO(n)O(h)Easy
671Second Minimum Node In a Binary TreeC++PythonO(n)O(h)Easy

Binary Search

#TitleSolutionTimeSpaceDifficultyTagNote
4Median of Two Sorted ArraysC++PythonO(log(min(m, n)))O(1)Hard
33Search in Rotated Sorted ArrayC++PythonO(logn)O(1)Hard
34Search for a RangeC++PythonO(logn)O(1)Medium
35Search Insert PositionC++PythonO(logn)O(1)Medium
69Sqrt(x)C++PythonO(logn)O(1)Medium
74Search a 2D MatrixC++PythonO(logm + logn)O(1)Medium
81Search in Rotated Sorted Array IIC++PythonO(logn)O(1)Medium
153Find Minimum in Rotated Sorted ArrayC++PythonO(logn)O(1)Medium
154Find Minimum in Rotated Sorted Array IIC++PythonO(logn) ~ O(n)O(1)Hard
162Find Peak ElementC++PythonO(logn)O(1)Medium
222Count Complete Tree NodesC++PythonO((logn)^2)O(1)Medium
275H-Index IIC++PythonO(logn)O(1)MediumBinary Search
278First Bad VersionC++PythonO(logn)O(1)EasyLintCode
300Longest Increasing SubsequenceC++PythonO(nlogn)O(n)MediumCTCI, LintCodeBinary Search, DP
302Smallest Rectangle Enclosing Black PixelsC++PythonO(nlogn)O(1)Hard📖
354Russian Doll EnvelopesC++PythonO(nlogn)O(1)Hard
363Max Sum of Rectangle No Larger Than KC++PythonO(min(m, n)^2 * max(m, n) * logn(max(m, n)))O(max(m, n))Hard
367Valid Perfect SquareC++PythonO(logn)O(1)Medium
374Guess Number Higher or LowerC++PythonO(logn)O(1)Easy
410Split Array Largest SumC++PythonO(nlogs)O(1)Hard
436Find Right IntervalC++PythonO(nlogn)O(n)Medium
475HeatersC++PythonO((m + n) * logn)O(1)Easy
658Find K Closest ElementsC++PythonO(logn + k)O(1)Medium
668Kth Smallest Number in Multiplication TableC++PythonO(m * log(m * n))O(1)Hard

Binary Search Tree

#TitleSolutionTimeSpaceDifficultyTagNote
220Contains Duplicate IIIC++PythonO(nlogk)O(k)Medium
230Kth Smallest Element in a BSTC++PythonO(max(h, k))O(min(h, k))Medium
235Lowest Common Ancestor of a Binary Search TreeC++PythonO(h)O(1)EasyEPI
270Closest Binary Search Tree ValueC++PythonO(h)O(1)Easy📖
285Inorder Successor in BSTC++PythonO(h)O(1)Medium📖
352Data Stream as Disjoint IntervalsC++PythonO(logn)O(n)Hard
449Serialize and Deserialize BSTC++PythonO(n)O(h)Medium
450Delete Node in a BSTC++PythonO(h)O(h)Medium

Breadth-First Search

#TitleSolutionTimeSpaceDifficultyTagNote
102Binary Tree Level Order TraversalC++PythonO(n)O(n)Easy
107Binary Tree Level Order Traversal IIPythonO(n)O(n)Easy
103Binary Tree Zigzag Level Order TraversalPythonO(n)O(n)Medium
117Populating Next Right Pointers in Each Node IIPythonO(n)O(1)Hard
127Word LadderPythonO(n * d)O(d)Medium
130Surrounded RegionsC++PythonO(m * n)O(m + n)Medium
133Clone GraphPythonO(n)O(n)Medium
207Course SchedulePythonO(|V| + |E|)O(|E|)MediumTopological Sort
210Course Schedule IIPythonO(|V| + |E|)O(|E|)MediumTopological Sort
261Graph Valid TreeC++PythonO(|V| + |E|)O(|V| + |E|)Medium📖
269Alien DictionaryC++PythonO(n)O(1)Hard📖Topological Sort, BFS, DFS
286Walls and GatesC++PythonO(m * n)O(g)Medium📖
310Minimum Height TreesC++PythonO(n)O(n)Medium
317Shortest Distance from All BuildingsC++PythonO(k * m * n)O(m * n)Hard📖
433Minimum Genetic MutationC++PythonO(n * b)O(b)Medium
444Sequence ReconstructionC++PythonO(n * s)O(n)Medium📖Topological Sort
666Path Sum IVC++PythonO(n)O(w)Medium📖Topological Sort
675Cut Off Trees for Golf EventC++PythonO(t * m * n)O(m * n)HardA* Search Algorithm

Depth-First Search

#TitleSolutionTimeSpaceDifficultyTagNote
112Path SumPythonO(n)O(h)Easy
113Path Sum IIPythonO(n)O(h)Medium
199Binary Tree Right Side ViewPythonO(n)O(h)Medium
200Number of IslandsPythonO(m * n)O(m * n)Medium
236Lowest Common Ancestor of a Binary TreeC++PythonO(n)O(h)MediumEPI
247Strobogrammatic Number IIC++PythonO(n^2 * 5^(n/2))O(n)Medium📖
250Count Univalue SubtreesC++PythonO(n)O(h)Medium📖
257Binary Tree PathsC++PythonO(n * h)O(h)Easy
282Expression Add OperatorsC++PythonO(4^n)O(n)Hard
301Remove Invalid ParenthesesC++PythonO(C(n, c))O(c)Hard
329Longest Increasing Path in a MatrixC++PythonO(m * n)O(m * n)Hard
332Reconstruct ItineraryC++PythonO(t! / (n1! * n2! * ... nk!))O(t)Medium
339Nested List Weight SumC++PythonO(n)O(h)Easy📖
364Nested List Weight Sum IIC++PythonO(n)O(h)Medium📖
366Find Leaves of Binary TreeC++PythonO(n)O(h)Medium📖
399Evaluate DivisionC++PythonO(q * |V|!)O(e)Medium
417Pacific Atlantic Water FlowC++PythonO(m * n)O(m * n)Medium
440K-th Smallest in Lexicographical OrderC++PythonO(logn)O(logn)Hard
464Can I WinC++PythonO(n!)O(n)Medium

Backtracking

#TitleSolutionTimeSpaceDifficultyTagNote
17Letter Combinations of a Phone NumberPythonO(n * 4^n)O(n)Medium
22Generate ParenthesesPythonO(4^n / n^(3/2))O(n)Medium
37Sudoku SolverPythonO((9!)^9)O(1)Hard
39Combination SumPythonO(k * n^k)O(k)Medium
40Combination Sum IIPythonO(k * C(n, k))O(k)Medium
46PermutationsPythonO(n * n!)O(n)Medium
47Permutations IIPythonO(n * n!)O(n)Medium
51N-QueensPythonO(n!)O(n)Hard
52N-Queens-IIPythonO(n!)O(n)Hard
77CombinationsPythonO(n!)O(n)Medium
79Word SearchPythonO(m * n * l)O(l)Medium
93Restore IP AddressesPythonO(1)O(1)Medium
78SubsetsC++PythonO(n * 2^n)O(1)Medium
90Subsets IIC++PythonO(n * 2^n)O(1)Medium
126Word Ladder IIPythonO(n * d)O(d)Hard
131Palindrome PartitioningPythonO(n^2) ~ O(2^n)O(n^2)Medium
140Word Break IIC++PythonO(n * l^2 + n * r)O(n^2)Hard
212Word Search IIC++PythonO(m * n * l)O(l)HardLintCodeTrie, DFS
216Combination Sum IIIC++PythonO(k * C(n, k))O(k)Medium
254Factor CombinationsC++PythonO(nlogn)O(logn)Medium📖
267Palindrome Permutation IIC++PythonO(n * n!)O(n)Medium📖
291Word Pattern IIC++PythonO(n * C(n - 1, c - 1))O(n + c)Hard📖
294Flip Game IIC++PythonO(n + c^2)O(c)Medium📖DP, Hash
320Generalized AbbreviationC++PythonO(n * 2^n)O(n)Medium📖
425Word SquaresC++PythonO(n^2 * n!)O(n^2)Hard📖
676Implement Magic DictionaryC++PythonO(n)O(d)MediumTrie, DFS
67924 GameC++PythonO(1)O(1)HardDFS

Dynamic Programming

#TitleSolutionTimeSpaceDifficultyTagNote
10Regular Expression MatchingPythonO(m * n)O(n)Hard
53Maximum SubarrayPythonO(n)O(1)Medium
62Unique PathsPythonO(m * n)O(m + n)Medium
63Unique Paths IIPythonO(m * n)O(m + n)Medium
64Minimum Path SumPythonO(m * n)O(m + n)Medium
70Climbing StairsPythonO(n)O(1)Easy
72Edit DistancePythonO(m * n)O(m + n)Hard
87Scramble StringPythonO(n^4)O(n^3)Hard
91Decode WaysC++PythonO(n)O(1)Medium
96Unique Binary Search TreesPythonO(n)O(1)MediumMath
97Interleaving StringPythonO(m * n)O(m + n)Hard
115Distinct SubsequencesPythonO(n^2)O(n)Hard
120TrianglePythonO(m * n)O(n)Medium
123Best Time to Buy and Sell Stock IIIPythonO(n)O(1)Hard
132Palindrome Partitioning IIPythonO(n^2)O(n^2)Hard
139Word BreakC++PythonO(n * l^2)O(n)Medium
152Maximum Product SubarrayPythonO(n)O(1)Medium
174Dungeon GamePythonO(m * n)O(m + n)Hard
188Best Time to Buy and Sell Stock IVPythonO(k * n)O(k)Hard
198House RobberPythonO(n)O(1)Easy
213House Robber IIC++PythonO(n)O(1)Medium
221Maximal SquareC++PythonO(n^2)O(n)MediumEPI
256Paint HouseC++PythonO(n)O(1)Medium📖
265Paint House IIC++PythonO(n * k)O(k)Hard📖
276Paint FenceC++PythonO(n)O(1)Easy📖
279Perfect SquaresC++PythonO(n * sqrt(n))O(n)MediumHash
303Range Sum Query - ImmutableC++Pythonctor: O(n), lookup: O(1)O(n)Easy
304Range Sum Query 2D - ImmutableC++Pythonctor: O(m * n), lookup: O(1)O(m * n)Medium
309Best Time to Buy and Sell Stock with CooldownC++PythonO(n)O(1)Medium
312Burst BalloonsC++PythonO(n^3)O(n^2)Hard
322Coin ChangeC++PythonO(n * k)O(k)Medium
351Android Unlock PatternsC++PythonO(9^2 * 2^9)O(9 * 2^9)Medium📖Backtracking
357Count Numbers with Unique DigitsC++PythonO(n)O(1)MediumBacktracking, Math
361Bomb EnemyC++PythonO(m * n)O(m * n)Medium📖
368Largest Divisible SubsetC++PythonO(n^2)O(n)Medium
375Guess Number Higher or Lower IIC++PythonO(n^2)O(n^2)Medium
377Combination Sum IVC++PythonO(nlogn + n * t)O(t)Medium
403Frog JumpC++PythonO(n)O(n) ~ O(n^2)Hard
416Partition Equal Subset SumC++PythonO(n * s)O(s)Medium
418Sentence Screen FittingC++PythonO(r + n * c)O(n)Medium📖
446Arithmetic Slices II - SubsequenceC++PythonO(n^2)O(n * d)Hard
465Optimal Account BalancingC++PythonO(n * 2^n)O(n * 2^n)Hard📖
466Count The RepetitionsC++PythonO(s1 * min(s2, n1))O(s2)Hard
467Unique Substrings in Wraparound StringC++PythonO(n)O(1)Medium
471Encode String with Shortest LengthC++PythonO(n^3) on averageO(n^2)Medium📖
472Concatenated WordsC++PythonO(n * l^2)O(n * l)Medium
474Ones and ZeroesC++PythonO(s * m * n)O(m * n)Medium
6502 Keys KeyboardC++PythonO(sqrt(n))O(1)Medium
656Coin PathC++PythonO(n * B)O(n)Hard📖
664Strange PrinterC++PythonO(n^3)O(n^2)Hard
673Number of Longest Increasing SubsequenceC++PythonO(n^2)O(n)Medium

Greedy

#TitleSolutionTimeSpaceDifficultyTagNote
11Container With Most WaterC++PythonO(n)O(1)Medium
42Trapping Rain WaterC++PythonO(n)O(1)HardTricky
44Wildcard MatchingPythonO(m + n)O(1)HardTricky
45Jump Game IIPythonO(n)O(1)Hard
55Jump GamePythonO(n)O(1)Medium
122Best Time to Buy and Sell Stock IIPythonO(n)O(1)Medium
134Gas StationPythonO(n)O(1)Medium
135CandyC++PythonO(n)O(n)Hard
316Remove Duplicate LettersC++PythonO(n)O(k)HardAscending Stack
321Create Maximum NumberC++PythonO(k * (m + n + k)) ~ O(k * (m + n + k^2))O(m + n + k^2)Hardvariant of Delete DigitsGreedy, DP
330Patching ArrayC++PythonO(s + logn)O(1)Hard
376Wiggle SubsequenceC++PythonO(n)O(1)Medium
392Is SubsequenceC++PythonO(n)O(1)Medium
397Integer ReplacementC++PythonO(n)O(1)MediumMath
402Remove K DigitsC++PythonO(n)O(n)MediumLintCode
435Non-overlapping IntervalsC++PythonO(nlogn)O(1)Medium
452Minimum Number of Arrows to Burst BalloonsC++PythonO(nlogn)O(1)Medium
455Assign CookiesC++PythonO(nlogn)O(1)Easy
646Maximum Length of Pair ChainC++PythonO(nlogn)O(1)MediumScan Line
649Dota2 SenateC++PythonO(n)O(n)Medium
659Split Array into Consecutive SubsequencesC++PythonO(n)O(1)Medium

Design

#TitleSolutionTimeSpaceDifficultyTagNote
146LRU CacheC++PythonO(1)O(k)Hard
225Implement Stack using QueuesC++Pythonpush: O(n), pop: O(1), top: O(1)O(n)Easy
284Peeking IteratorC++PythonO(1)O(1)Medium
348Design Tic-Tac-ToeC++PythonO(1)O(n^2)Medium📖
353Design Snake GameC++PythonO(1)O(s)Medium📖Deque
355Design TwitterC++PythonO(klogu)O(t + f)MediumLintCodeHeap
359Logger Rate LimiterC++PythonO(1), amortizedO(k)Easy📖Deque
362Design Hit CounterC++PythonO(1), amortizedO(k)Medium📖Deque
379Design Phone DirectoryC++PythonO(1)O(n)Medium📖
380Insert Delete GetRandom O(1)C++PythonO(1)O(n)Hard
381Insert Delete GetRandom O(1) - Duplicates allowedC++PythonO(1)O(n)Hard
432All O`one Data StructureC++PythonO(1)O(n)Hard
460LFU CacheC++PythonO(1)O(k)Hard

SQL

#TitleSolutionTimeSpaceDifficultyTagNote
175Combine Two TablesMySQLO(m + n)O(m + n)Easy
176Second Highest SalaryMySQLO(n)O(1)Easy
177Nth Highest SalaryMySQLO(n^2)O(n)Medium
178Rank ScoresMySQLO(n^2)O(n)Medium
180Consecutive NumbersMySQLO(n)O(n)Medium
181Employees Earning More Than Their ManagersMySQLO(n^2)O(1)Easy
182Duplicate EmailsMySQLO(n^2)O(n)Easy
183Customers Who Never OrderMySQLO(n^2)O(1)Easy
184Department Highest SalaryMySQLO(n^2)O(n)Medium
185Department Top Three SalariesMySQLO(n^2)O(n)Hard
196Delete Duplicate EmailsMySQLO(n^2)O(n)Easy
197Rising TemperatureMySQLO(n^2)O(n)Easy
262Trips and UsersMySQLO((t * u) + tlogt)O(t)Hard

Shell Script

#TitleSolutionTimeSpaceDifficultyTagNote
192Word FrequencyShellO(n)O(k)Medium
193Valid Phone NumbersShellO(n)O(1)Easy
194Transpose FileShellO(n^2)O(n^2)Medium
195Tenth LineShellO(n)O(1)Easy

About

📝 Python / C++ 11 Solutions of LeetCode Questions

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + ' GitHub - shinan6/LeetCode: :pencil: Python / C++ 11 Solutions of LeetCode Questions · GitHub
Skip to content

Latest commit

History

4,201 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

The number of LeetCode questions is increasing every week. For more questions and solutions, you can see my LintCode repository. I'll keep updating for full summary and better solutions. Stay tuned for updates. (Notes: "📖" means you need to subscribe to LeetCode premium membership for the access to premium questions.)

Algorithms

Database

Shell

Bit Manipulation

#TitleSolutionTimeSpaceDifficultyTagNote
136Single NumberC++PythonO(n)O(1)Easy
137Single Number IIC++PythonO(n)O(1)Medium
190Reverse BitsC++PythonO(1)O(1)Easy
191Number of 1 BitsC++PythonO(1)O(1)Easy
201Bitwise AND of Numbers RangeC++PythonO(1)O(1)Medium
231Power of TwoC++PythonO(1)O(1)EasyLintCode
260Single Number IIIC++PythonO(n)O(1)Medium
268Missing NumberC++PythonO(n)O(1)MediumLintCode
318Maximum Product of Word LengthsC++PythonO(n) ~ O(n^2)O(n)MediumBit Manipulation, Counting Sort, Pruning
342Power of FourC++PythonO(1)O(1)Easy
371Sum of Two IntegersC++PythonO(1)O(1)EasyLintCode
389Find the DifferenceC++PythonO(n)O(1)Easy
393UTF-8 ValidationC++PythonO(n)O(1)Medium
401Binary WatchC++PythonO(1)O(1)Easy
411Minimum Unique Word AbbreviationC++PythonO(2^n)O(n)Hard📖
421Maximum XOR of Two Numbers in an ArrayC++PythonO(n)O(1)Medium
461Hamming DistanceC++PythonO(1)O(1)Easy
462Minimum Moves to Equal Array Elements IIC++PythonO(n) on averageO(1)Medium
477Total Hamming DistanceC++PythonO(n)O(1)Medium
645Set MismatchC++PythonO(n)O(1)Easy

Array

#TitleSolutionTimeSpaceDifficultyTagNote
153 SumC++PythonO(n^2)O(1)MediumTwo Pointers
163 Sum ClosestC++PythonO(n^2)O(1)MediumTwo Pointers
184 SumC++PythonO(n^3)O(1)MediumTwo Pointers
26Remove Duplicates from Sorted ArrayC++PythonO(n)O(1)EasyTwo Pointers
27Remove ElementC++PythonO(n)O(1)Easy
31Next PermutationC++PythonO(n)O(1)MediumTricky
41First Missing PositiveC++PythonO(n)O(1)HardTricky
48Rotate ImageC++PythonO(n^2)O(1)Medium
54Spiral MatrixC++PythonO(m * n)O(1)Medium
59Spiral Matrix IIC++PythonO(n^2)O(1)Medium
66Plus OneC++PythonO(n)O(1)Easy
73Set Matrix ZeroesC++PythonO(m * n)O(1)Medium
80Remove Duplicates from Sorted Array IIC++PythonO(n)O(1)MediumTwo Pointers
118Pascal's TriangleC++PythonO(n^2)O(1)Easy
119Pascal's Triangle IIC++PythonO(n^2)O(1)Easy
121Best Time to Buy and Sell StockC++PythonO(n)O(1)Easy
128Longest Consecutive SequenceC++PythonO(n)O(n)HardTricky
157Read N Characters Given Read4C++PythonO(n)O(1)Easy📖
158Read N Characters Given Read4 II - Call multiple timesC++PythonO(n)O(1)Hard📖
163Missing RangesC++PythonO(n)O(1)Medium📖
169Majority ElementC++PythonO(n)O(1)Easy
189Rotate ArrayC++PythonO(n)O(1)Easy
209Minimum Size Subarray SumC++PythonO(n)O(1)MediumBinary Search
215Kth Largest Element in an ArrayC++PythonO(n) ~ O(n^2)O(1)MediumEPI
228Summary RangesC++PythonO(n)O(1)Medium
229Majority Element IIC++PythonO(n)O(1)Medium
238Product of Array Except SelfC++PythonO(n)O(1)MediumLintCode
240Search a 2D Matrix IIC++PythonO(m + n)O(1)MediumEPI, LintCode
243Shortest Word DistanceC++PythonO(n)O(1)Easy📖
245Shortest Word Distance IIIC++PythonO(n)O(1)Medium📖
251Flatten 2D VectorC++PythonO(1)O(1)Medium📖
277Find the CelebrityC++PythonO(n)O(1)Medium📖, EPI
289Game of LifeC++PythonO(m * n)O(1)Medium
293Flip GameC++PythonO(n * (c+1))O(1)Easy📖
296Best Meeting PointC++PythonO(m * n)O(m + n)Hard📖
311Sparse Matrix MultiplicationC++PythonO(m * n * l)O(m * l)Medium📖
334Increasing Triplet SubsequenceC++PythonO(n)O(1)Medium
370Range AdditionC++PythonO(k + n)O(1)Medium📖
384Shuffle an ArrayC++PythonO(n)O(n)MediumEPI
396Rotate FunctionC++PythonO(n)O(1)Easy
412Fizz BuzzC++PythonO(n)O(1)Easy
414Third Maximum NumberC++PythonO(n)O(1)Easy
419Battleships in a BoardC++PythonO(m * n)O(1)Medium
422Valid Word SquareC++PythonO(m * n)O(1)Easy📖
442Find All Duplicates in an ArrayC++PythonO(n)O(1)Medium
448Find All Numbers Disappeared in an ArrayC++PythonO(n)O(1)Easy
661Image SmootherC++PythonO(m * n)O(1)Easy
665Non-decreasing ArrayC++PythonO(n)O(1)Easy
667Beautiful Arrangement IIC++PythonO(n)O(1)Medium
670Maximum SwapC++PythonO(logn)O(logn)Medium
674Longest Continuous Increasing SubsequenceC++PythonO(n)O(1)Easy

String

#TitleSolutionTimeSpaceDifficultyTagNote
5Longest Palindromic SubstringC++PythonO(n)O(n)MediumManacher's Algorithm
6ZigZag ConversionC++PythonO(n)O(1)Easy
8String to Integer (atoi)C++PythonO(n)O(1)Easy
14Longest Common PrefixC++PythonO(n * k)O(1)Easy
28Implement strStr()C++PythonO(n + k)O(k)EasyKMP Algorithm
38Count and SayC++PythonO(n * 2^n)O(2^n)Easy
43Multiply StringsC++PythonO(m * n)O(m + n)Medium
58Length of Last WordC++PythonO(n)O(1)Easy
67Add BinaryC++PythonO(n)O(1)Easy
68Text JustificationC++PythonO(n)O(1)Hard
125Valid PalindromeC++PythonO(n)O(1)Easy
151Reverse Words in a StringC++PythonO(n)O(1)Medium
161One Edit DistanceC++PythonO(m + n)O(1)Medium📖
165Compare Version NumbersC++PythonO(n)O(1)Easy
186Reverse Words in a String IIC++PythonO(n)O(1)Medium📖
214Shortest PalindromeC++PythonO(n)O(n)HardKMP AlgorithmManacher's Algorithm
242Valid AnagramC++PythonO(n)O(1)EasyLintCode
271Encode and Decode StringsC++PythonO(n)O(1)Medium📖
273Integer to English WordsC++PythonO(logn)O(1)Hard
306Addictive NumberC++PythonO(n^3)O(n)Medium
383Ransom NoteC++PythonO(n)O(1)EasyEPI
405Convert a Number to HexadecimalC++PythonO(n)O(1)Easy
408Valid Word AbbreviationC++PythonO(n)O(1)Easy📖
415Add StringsC++PythonO(n)O(1)Easy
420Strong Password CheckerC++PythonO(n)O(1)Hard
434Number of Segments in a StringC++PythonO(n)O(1)Easy
459Repeated Substring PatternC++PythonO(n)O(n)EasyKMP Algorithm
468Validate IP AddressC++PythonO(1)O(1)Medium
556Next Greater Element IIIC++PythonO(1)O(1)Medium
557Reverse Words in a String IIIC++PythonO(n)O(1)Easy
647Palindromic SubstringsC++PythonO(n)O(n)MediumManacher's Algorithm
648Replace WordsC++PythonO(n)O(t)Mediumtrie
657Judge Route CircleC++PythonO(n)O(1)Easy
678Valid Parenthesis StringC++PythonO(n)O(1)Medium
680Valid Palindrome IIC++PythonO(n)O(1)Easy

Linked List

#TitleSolutionTimeSpaceDifficultyTagNote
2Add Two NumbersC++PythonO(n)O(1)Medium
21Merge Two Sorted ListsC++PythonO(n)O(1)Easy
23Merge k Sorted ListsC++PythonO(nlogk)O(1)HardHeap, Divide and Conquer
24Swap Nodes in PairsC++PythonO(n)O(1)Easy
25Reverse Nodes in k-GroupC++PythonO(n)O(1)Hard
61Rotate ListC++PythonO(n)O(1)Medium
82Remove Duplicates from Sorted List IIC++PythonO(n)O(1)Medium
83Remove Duplicates from Sorted ListC++PythonO(n)O(1)Easy
92Reverse Linked List IIC++PythonO(n)O(1)Medium
138Copy List with Random PointerC++PythonO(n)O(1)Hard
160Intersection of Two Linked ListsC++PythonO(m + n)O(1)Easy
203Remove Linked List ElementsC++PythonO(n)O(1)Easy
206Reverse Linked ListC++PythonO(n)O(1)Easy
234Palindrome Linked ListC++PythonO(n)O(1)Easy
237Delete Node in a Linked ListC++PythonO(1)O(1)EasyLintCode
328Odd Even Linked ListC++PythonO(n)O(1)Medium
369Plus One Linked ListC++PythonO(n)O(1)Medium📖Two Pointers
445Add Two Numbers IIC++PythonO(m + n)O(m + n)Medium

Stack

#TitleSolutionTimeSpaceDifficultyTagNote
20Valid ParenthesesC++PythonO(n)O(n)Easy
32Longest Valid ParenthesesC++PythonO(n)O(1)Hard
71Simplify PathC++PythonO(n)O(n)Medium
84Largest Rectangle in HistogramC++PythonO(n)O(n)HardAscending Stack, DP
85Maximal RectangleC++PythonO(m * n)O(n)HardEPIAscending Stack
101Symmetric TreeC++PythonO(n)O(h)Easy
150Evaluate Reverse Polish NotationC++PythonO(n)O(n)Medium
155Min StackC++PythonO(n)O(1)Easy
173Binary Search Tree IteratorC++PythonO(1)O(h)Medium
224Basic CalculatorC++PythonO(n)O(n)Hard
227Basic Calculator IIC++PythonO(n)O(n)Medium
232Implement Queue using StacksC++PythonO(1), amortizedO(n)EasyEPI, LintCode
255Verify Preorder Sequence in Binary Search TreeC++PythonO(n)O(1)Medium📖
272Closest Binary Search Tree Value IIC++PythonO(h + k)O(h)Hard📖
331Verify Preorder Serialization of a Binary TreeC++PythonO(n)O(1)Medium
341Flatten Nested List IteratorC++PythonO(n)O(h)Medium📖Iterator
385Mini ParserC++PythonO(n)O(h)Medium
394Decode StringC++PythonO(n)O(h)Medium
439Ternary Expression ParserC++PythonO(n)O(1)Medium📖
456132 PatternC++PythonO(n)O(n)Medium

Queue

#TitleSolutionTimeSpaceDifficultyTagNote
239Sliding Window MaximumC++PythonO(n)O(k)HardEPI, LintCode
281Zigzag IteratorC++PythonO(n)O(k)Medium📖
346Moving Average from Data StreamC++PythonO(1)O(w)Easy📖

Heap

#TitleSolutionTimeSpaceDifficultyTagNote
264Ugly Number IIC++PythonO(n)O(1)MediumCTCI, LintCodeBST, Heap
295Find Median from Data StreamC++PythonO(nlogn)O(n)HardEPI, LintCodeBST, Heap
313Super Ugly NumberC++PythonO(n * k)O(n + k)MediumBST, Heap
358Rearrange String k Distance ApartC++PythonO(n)O(n)Hard📖Greedy, Heap
373Find K Pairs with Smallest SumsC++PythonO(k * log(min(n, m, k)))O(min(n, m, k))Medium
378Kth Smallest Element in a Sorted MatrixC++PythonO(k * log(min(n, m, k)))O(min(n, m, k))MediumLintCode
407Trapping Rain Water IIC++PythonO(m * n * (logm + logn))O(m * n)HardLintCode

Tree

#TitleSolutionTimeSpaceDifficultyTagNote
94Binary Tree Inorder TraversalC++PythonO(n)O(1)MediumMorris Traversal
99Recover Binary Search TreeC++PythonO(n)O(1)HardMorris Traversal
144Binary Tree Preorder TraversalC++PythonO(n)O(1)MediumMorris Traversal
145Binary Tree Postorder TraversalC++PythonO(n)O(1)HardMorris Traversal
208Implement Trie (Prefix Tree)C++PythonO(n)O(1)MediumTrie
211Add and Search Word - Data structure designC++PythonO(min(n, h))O(min(n, h))MediumTrie, DFS
226Invert Binary TreeC++PythonO(n)O(h), O(w)Easy
297Serialize and Deserialize Binary TreeC++PythonO(n)O(h)HardLintCodeDFS
307Range Sum Query - MutableC++Pythonctor: O(n), update: O(logn), query: O(logn)O(n)MediumLintCodeDFS, Segment Tree, BIT
308Range Sum Query 2D - MutableC++Pythonctor: O(m * n), update: O(logm + logn), query: O(logm + logn)O(m * n)Hard📖DFS, Segment Tree, BIT
315Count of Smaller Numbers After SelfC++PythonO(nlogn)O(n)HardLintCodeBST, BIT, Divide and Conquer
652Find Duplicate SubtreesC++PythonO(n)O(n)MediumDFS, Hash
653Two Sum IV - Input is a BSTC++PythonO(n)O(h)EasyTwo Pointers
654Maximum Binary TreeC++PythonO(n)O(n)MediumLintCodeDescending Stack
655Print Binary TreeC++PythonO(n)O(h)Medium
662Maximum Width of Binary TreeC++PythonO(n)O(h)MediumDFS
663Equal Tree PartitionC++PythonO(n)O(n)Medium📖Hash
677Map Sum PairsC++PythonO(n)O(t)MediumTrie

Hash Table

#TitleSolutionTimeSpaceDifficultyTagNote
1Two SumC++PythonO(n)O(n)Easy
3Longest Substring Without Repeating CharactersC++PythonO(n)O(1)Medium
30Substring with Concatenation of All WordsC++PythonO(m * n * k)O(n * k)Hard
36Valid SudokuC++PythonO(9^2)O(9)Easy
49Group AnagramsC++PythonO(n * glogg)O(n)Medium
76Minimum Window SubstringC++PythonO(n)O(k)Hard
149Max Points on a LineC++PythonO(n^2)O(n)Hard
159Longest Substring with At Most Two Distinct CharactersC++PythonO(n)O(1)Hard📖
170Two Sum III - Data structure designC++PythonO(n)O(n)Easy📖
187Repeated DNA SequencesPythonO(n)O(n)Medium
202Happy NumberC++PythonO(k)O(k)Easy
204Count PrimesC++PythonO(n)O(n)Easy
205Isomorphic StringsC++PythonO(n)O(1)Easy
217Contains DuplicateC++PythonO(n)O(n)Easy
219Contains Duplicate IIC++PythonO(n)O(n)Easy
244Shortest Word Distance IIC++Pythonctor: O(n), lookup: O(a + b)O(n)Medium📖
246Strobogrammatic NumberC++PythonO(n)O(1)Easy📖
249Group Shifted StringsC++PythonO(nlogn)O(n)Easy📖
266Palindrome PermutationC++PythonO(n)O(1)Easy📖
288Unique Word AbbreviationC++Pythonctor: O(n), lookup: O(1)O(k)Easy📖
290Word PatternC++PythonO(n)O(c)Easyvariant of Isomorphic Strings
299Bulls and CowsC++PythonO(n)O(1)Easy
305Number of Islands IIC++PythonO(k)O(k)HardLintCode, 📖Union Find
314Binary Tree Vertical Order TraversalC++PythonO(n)O(n)Medium📖BFS
323Number of Connected Components in an Undirected GraphC++PythonO(n)O(n)Medium📖Union Find
325Maximum Size Subarray Sum Equals kC++PythonO(n)O(n)Medium📖
336Palindrome PairsC++PythonO(n * k^2)O(n * k)Hard
340Longest Substring with At Most K Distinct CharactersC++PythonO(n)O(1)Hard📖
356Line ReflectionC++PythonO(n)O(n)Medium📖Hash, Two Pointers
387First Unique Character in a StringC++PythonO(n)O(n)Easy
388Longest Absolute File PathC++PythonO(n)O(d)MediumStack
409Longest PalindromeC++PythonO(n)O(1)Easy
424Longest Repeating Character ReplacementC++PythonO(n)O(1)Medium
438Find All Anagrams in a StringC++PythonO(n)O(1)Easy
447Number of BoomerangsC++PythonO(n^2)O(n)Easy
4544Sum IIC++PythonO(n^2)O(n^2)Medium
473Matchsticks to SquareC++PythonO(n * s * 2^n)O(n * (2^n + s))Medium
554Brick WallC++PythonO(n)O(m)Medium

Math

#TitleSolutionTimeSpaceDifficultyTagNote
7Reverse IntegerC++PythonO(1)O(1)Easy
9Palindrome NumberC++PythonO(1)O(1)Easy
12Integer to RomanC++PythonO(n)O(1)Medium
13Roman to IntegerC++PythonO(n)O(1)Easy
29Divide Two IntegersC++PythonO(1)O(1)Medium
50Pow(x, n)C++PythonO(1)O(1)Medium
60Permutation SequenceC++PythonO(n^2)O(n)MediumCantor Ordering
65Valid NumberC++PythonO(n)O(1)HardAutomata
89Gray CodeC++PythonO(2^n)O(1)Medium
166Fraction to Recurring DecimalC++PythonO(logn)O(1)Medium
168Excel Sheet Column TitleC++PythonO(logn)O(1)Easy
171Excel Sheet Column NumberC++PythonO(n)O(1)Easy
172Factorial Trailing ZeroesC++PythonO(1)O(1)Easy
223Rectangle AreaC++PythonO(1)O(1)Easy
233Number of Digit OneC++PythonO(1)O(1)HardCTCI, LintCode
248Strobogrammatic Number IIIC++PythonO(5^(n/2))O(n)Hard📖
258Add DigitsC++PythonO(1)O(1)Easy
263Ugly NumberC++PythonO(1)O(1)Easy
292Nim GameC++PythonO(1)O(1)EasyLintCode
319Bulb SwitcherC++PythonO(1)O(1)Medium
326Power of ThreeC++PythonO(1)O(1)Easy
335Self CrossingC++PythonO(n)O(1)Hard
338Counting BitsC++PythonO(n)O(n)Medium
343Integer BreakC++PythonO(logn)O(1)MediumTricky, DP
365Water and Jug ProblemC++PythonO(logn)O(1)MediumEuclidean Algorithm
372Super PowC++PythonO(n)O(1)Medium
382Linked List Random NodeC++PythonO(n)O(1)MediumReservoir Sampling
386Lexicographical NumbersC++PythonO(n)O(1)Medium
390Elimination GameC++PythonO(logn)O(1)Medium
391Perfect RectangleC++PythonO(n)O(n)Hard
398Random Pick IndexC++PythonO(n)O(1)MediumReservoir Sampling
400Nth DigitC++PythonO(logn)O(1)Easy
413Arithmetic SlicesC++PythonO(n)O(1)Medium
423Reconstruct Original Digits from EnglishC++PythonO(n)O(1)MediumGCJ2016 - Round 1B
441Arranging CoinsC++PythonO(nlogn)O(1)EasyBinary Search
453Minimum Moves to Equal Array ElementsC++PythonO(n)O(1)Easy
458Poor PigsC++PythonO(n)O(1)Easy
469Convex PolygonC++PythonO(n)O(1)Medium📖
6514 Keys KeyboardC++PythonO(1)O(1)Medium📖Math, DP
660Remove 9C++PythonO(logn)O(1)Hard📖
672Bulb Switcher IIC++PythonO(1)O(1)Medium

Sort

#TitleSolutionTimeSpaceDifficultyTagNote
56Merge IntervalsC++PythonO(nlogn)O(1)Hard
57Insert IntervalC++PythonO(n)O(1)Hard
75Sort ColorsC++PythonO(n)O(1)MediumTri Partition
88Merge Sorted ArrayC++PythonO(n)O(1)Easy
147Insertion Sort ListC++PythonO(n^2)O(1)Medium
148Sort ListC++PythonO(nlogn)O(logn)Medium
164Maximum GapC++PythonO(n)O(n)HardTricky
179Largest NumberC++PythonO(nlogn)O(1)Medium
218The Skyline ProblemC++PythonO(nlogn)O(n)HardSort, BST
252Meeting RoomsC++PythonO(nlogn)O(n)Easy📖
253Meeting Rooms IIC++PythonO(nlogn)O(n)Medium📖
274H-IndexC++PythonO(n)O(n)MediumCounting Sort
280Wiggle SortC++PythonO(n)O(1)Medium📖
324Wiggle Sort IIC++PythonO(n) on averageO(1)Mediumvariant of Sort ColorsTri Partition
347Top K Frequent ElementsC++PythonO(n) on averageO(n)MediumQuick Select, Heap
406Queue Reconstruction by HeightC++PythonO(n * sqrt(n))O(n)Medium
451Sort Characters By FrequencyC++PythonO(n)O(n)Medium

Two Pointers

#TitleSolutionTimeSpaceDifficultyTagNote
19Remove Nth Node From End of ListC++PythonO(n)O(1)Easy
86Partition ListC++PythonO(n)O(1)Medium
141Linked List CycleC++PythonO(n)O(1)Easy
142Linked List Cycle IIC++PythonO(n)O(1)Medium
143Reorder ListC++PythonO(n)O(1)Medium
167Two Sum II - Input array is sortedC++PythonO(n)O(1)Medium
2593Sum SmallerC++PythonO(n^2)O(1)Medium📖, LintCode
283Move ZeroesC++PythonO(n)O(1)Easy
287Find the Duplicate NumberC++PythonO(n)O(1)HardBinary Search, Two Pointers
344Reverse StringC++PythonO(n)O(1)Easy
345Reverse Vowels of a StringC++PythonO(n)O(1)Easy
349Intersection of Two ArraysC++PythonO(m + n)O(min(m, n))EasyEPIHash, Binary Search
350Intersection of Two Arrays IIC++PythonO(m + n)O(1)EasyEPIHash, Binary Search
360Sort Transformed ArrayC++PythonO(n)O(1)Medium📖
457Circular Array LoopC++PythonO(n)O(1)Medium

Recursion

#TitleSolutionTimeSpaceDifficultyTagNote
95Unique Binary Search Trees IIC++PythonO(4^n / n^(3/2)O(4^n / n^(3/2)Medium
98Validate Binary Search TreeC++PythonO(n)O(1)Medium
100Same TreeC+PythonO(n)O(h)Easy
104Maximum Depth of Binary TreeC++PythonO(n)O(h)Easy
105Construct Binary Tree from Preorder and Inorder TraversalC++PythonO(n)O(n)Medium
106Construct Binary Tree from Inorder and Postorder TraversalC++PythonO(n)O(n)Medium
108Convert Sorted Array to Binary Search TreeC++PythonO(n)O(logn)Medium
109Convert Sorted List to Binary Search TreeC++PythonO(n)O(logn)Medium
110Balanced Binary TreePythonO(n)O(h)Easy
111Minimum Depth of Binary TreePythonO(n)O(h)Easy
114Flatten Binary Tree to Linked ListPythonO(n)O(h)Medium
116Populating Next Right Pointers in Each NodePythonO(n)O(1)Medium
124Binary Tree Maximum Path SumPythonO(n)O(h)Hard
129Sum Root to Leaf NumbersPythonO(n)O(h)Medium
156Binary Tree Upside DownPythonO(n)O(1)Medium📖
241Different Ways to Add ParenthesesC++PythonO(n * 4^n / n^(3/2))O(n * 4^n / n^(3/2))Medium
298Binary Tree Longest Consecutive SequenceC++PythonO(n)O(h)Medium📖
327Count of Range SumC++PythonO(nlogn)O(n)Hard
333Largest BST SubtreeC++PythonO(n)O(h)Medium📖
337House Robber IIIC++PythonO(n)O(h)Medium
395Longest Substring with At Least K Repeating CharactersC++PythonO(n)O(1)Medium
404Sum of Left LeavesC++PythonO(n)O(h)Easy
437Path Sum IIIC++PythonO(n)O(h)Easy
549Binary Tree Longest Consecutive Sequence IIC++PythonO(n)O(h)Medium📖
669Trim a Binary Search TreeC++PythonO(n)O(h)Easy
671Second Minimum Node In a Binary TreeC++PythonO(n)O(h)Easy

Binary Search

#TitleSolutionTimeSpaceDifficultyTagNote
4Median of Two Sorted ArraysC++PythonO(log(min(m, n)))O(1)Hard
33Search in Rotated Sorted ArrayC++PythonO(logn)O(1)Hard
34Search for a RangeC++PythonO(logn)O(1)Medium
35Search Insert PositionC++PythonO(logn)O(1)Medium
69Sqrt(x)C++PythonO(logn)O(1)Medium
74Search a 2D MatrixC++PythonO(logm + logn)O(1)Medium
81Search in Rotated Sorted Array IIC++PythonO(logn)O(1)Medium
153Find Minimum in Rotated Sorted ArrayC++PythonO(logn)O(1)Medium
154Find Minimum in Rotated Sorted Array IIC++PythonO(logn) ~ O(n)O(1)Hard
162Find Peak ElementC++PythonO(logn)O(1)Medium
222Count Complete Tree NodesC++PythonO((logn)^2)O(1)Medium
275H-Index IIC++PythonO(logn)O(1)MediumBinary Search
278First Bad VersionC++PythonO(logn)O(1)EasyLintCode
300Longest Increasing SubsequenceC++PythonO(nlogn)O(n)MediumCTCI, LintCodeBinary Search, DP
302Smallest Rectangle Enclosing Black PixelsC++PythonO(nlogn)O(1)Hard📖
354Russian Doll EnvelopesC++PythonO(nlogn)O(1)Hard
363Max Sum of Rectangle No Larger Than KC++PythonO(min(m, n)^2 * max(m, n) * logn(max(m, n)))O(max(m, n))Hard
367Valid Perfect SquareC++PythonO(logn)O(1)Medium
374Guess Number Higher or LowerC++PythonO(logn)O(1)Easy
410Split Array Largest SumC++PythonO(nlogs)O(1)Hard
436Find Right IntervalC++PythonO(nlogn)O(n)Medium
475HeatersC++PythonO((m + n) * logn)O(1)Easy
658Find K Closest ElementsC++PythonO(logn + k)O(1)Medium
668Kth Smallest Number in Multiplication TableC++PythonO(m * log(m * n))O(1)Hard

Binary Search Tree

#TitleSolutionTimeSpaceDifficultyTagNote
220Contains Duplicate IIIC++PythonO(nlogk)O(k)Medium
230Kth Smallest Element in a BSTC++PythonO(max(h, k))O(min(h, k))Medium
235Lowest Common Ancestor of a Binary Search TreeC++PythonO(h)O(1)EasyEPI
270Closest Binary Search Tree ValueC++PythonO(h)O(1)Easy📖
285Inorder Successor in BSTC++PythonO(h)O(1)Medium📖
352Data Stream as Disjoint IntervalsC++PythonO(logn)O(n)Hard
449Serialize and Deserialize BSTC++PythonO(n)O(h)Medium
450Delete Node in a BSTC++PythonO(h)O(h)Medium

Breadth-First Search

#TitleSolutionTimeSpaceDifficultyTagNote
102Binary Tree Level Order TraversalC++PythonO(n)O(n)Easy
107Binary Tree Level Order Traversal IIPythonO(n)O(n)Easy
103Binary Tree Zigzag Level Order TraversalPythonO(n)O(n)Medium
117Populating Next Right Pointers in Each Node IIPythonO(n)O(1)Hard
127Word LadderPythonO(n * d)O(d)Medium
130Surrounded RegionsC++PythonO(m * n)O(m + n)Medium
133Clone GraphPythonO(n)O(n)Medium
207Course SchedulePythonO(|V| + |E|)O(|E|)MediumTopological Sort
210Course Schedule IIPythonO(|V| + |E|)O(|E|)MediumTopological Sort
261Graph Valid TreeC++PythonO(|V| + |E|)O(|V| + |E|)Medium📖
269Alien DictionaryC++PythonO(n)O(1)Hard📖Topological Sort, BFS, DFS
286Walls and GatesC++PythonO(m * n)O(g)Medium📖
310Minimum Height TreesC++PythonO(n)O(n)Medium
317Shortest Distance from All BuildingsC++PythonO(k * m * n)O(m * n)Hard📖
433Minimum Genetic MutationC++PythonO(n * b)O(b)Medium
444Sequence ReconstructionC++PythonO(n * s)O(n)Medium📖Topological Sort
666Path Sum IVC++PythonO(n)O(w)Medium📖Topological Sort
675Cut Off Trees for Golf EventC++PythonO(t * m * n)O(m * n)HardA* Search Algorithm

Depth-First Search

#TitleSolutionTimeSpaceDifficultyTagNote
112Path SumPythonO(n)O(h)Easy
113Path Sum IIPythonO(n)O(h)Medium
199Binary Tree Right Side ViewPythonO(n)O(h)Medium
200Number of IslandsPythonO(m * n)O(m * n)Medium
236Lowest Common Ancestor of a Binary TreeC++PythonO(n)O(h)MediumEPI
247Strobogrammatic Number IIC++PythonO(n^2 * 5^(n/2))O(n)Medium📖
250Count Univalue SubtreesC++PythonO(n)O(h)Medium📖
257Binary Tree PathsC++PythonO(n * h)O(h)Easy
282Expression Add OperatorsC++PythonO(4^n)O(n)Hard
301Remove Invalid ParenthesesC++PythonO(C(n, c))O(c)Hard
329Longest Increasing Path in a MatrixC++PythonO(m * n)O(m * n)Hard
332Reconstruct ItineraryC++PythonO(t! / (n1! * n2! * ... nk!))O(t)Medium
339Nested List Weight SumC++PythonO(n)O(h)Easy📖
364Nested List Weight Sum IIC++PythonO(n)O(h)Medium📖
366Find Leaves of Binary TreeC++PythonO(n)O(h)Medium📖
399Evaluate DivisionC++PythonO(q * |V|!)O(e)Medium
417Pacific Atlantic Water FlowC++PythonO(m * n)O(m * n)Medium
440K-th Smallest in Lexicographical OrderC++PythonO(logn)O(logn)Hard
464Can I WinC++PythonO(n!)O(n)Medium

Backtracking

#TitleSolutionTimeSpaceDifficultyTagNote
17Letter Combinations of a Phone NumberPythonO(n * 4^n)O(n)Medium
22Generate ParenthesesPythonO(4^n / n^(3/2))O(n)Medium
37Sudoku SolverPythonO((9!)^9)O(1)Hard
39Combination SumPythonO(k * n^k)O(k)Medium
40Combination Sum IIPythonO(k * C(n, k))O(k)Medium
46PermutationsPythonO(n * n!)O(n)Medium
47Permutations IIPythonO(n * n!)O(n)Medium
51N-QueensPythonO(n!)O(n)Hard
52N-Queens-IIPythonO(n!)O(n)Hard
77CombinationsPythonO(n!)O(n)Medium
79Word SearchPythonO(m * n * l)O(l)Medium
93Restore IP AddressesPythonO(1)O(1)Medium
78SubsetsC++PythonO(n * 2^n)O(1)Medium
90Subsets IIC++PythonO(n * 2^n)O(1)Medium
126Word Ladder IIPythonO(n * d)O(d)Hard
131Palindrome PartitioningPythonO(n^2) ~ O(2^n)O(n^2)Medium
140Word Break IIC++PythonO(n * l^2 + n * r)O(n^2)Hard
212Word Search IIC++PythonO(m * n * l)O(l)HardLintCodeTrie, DFS
216Combination Sum IIIC++PythonO(k * C(n, k))O(k)Medium
254Factor CombinationsC++PythonO(nlogn)O(logn)Medium📖
267Palindrome Permutation IIC++PythonO(n * n!)O(n)Medium📖
291Word Pattern IIC++PythonO(n * C(n - 1, c - 1))O(n + c)Hard📖
294Flip Game IIC++PythonO(n + c^2)O(c)Medium📖DP, Hash
320Generalized AbbreviationC++PythonO(n * 2^n)O(n)Medium📖
425Word SquaresC++PythonO(n^2 * n!)O(n^2)Hard📖
676Implement Magic DictionaryC++PythonO(n)O(d)MediumTrie, DFS
67924 GameC++PythonO(1)O(1)HardDFS

Dynamic Programming

#TitleSolutionTimeSpaceDifficultyTagNote
10Regular Expression MatchingPythonO(m * n)O(n)Hard
53Maximum SubarrayPythonO(n)O(1)Medium
62Unique PathsPythonO(m * n)O(m + n)Medium
63Unique Paths IIPythonO(m * n)O(m + n)Medium
64Minimum Path SumPythonO(m * n)O(m + n)Medium
70Climbing StairsPythonO(n)O(1)Easy
72Edit DistancePythonO(m * n)O(m + n)Hard
87Scramble StringPythonO(n^4)O(n^3)Hard
91Decode WaysC++PythonO(n)O(1)Medium
96Unique Binary Search TreesPythonO(n)O(1)MediumMath
97Interleaving StringPythonO(m * n)O(m + n)Hard
115Distinct SubsequencesPythonO(n^2)O(n)Hard
120TrianglePythonO(m * n)O(n)Medium
123Best Time to Buy and Sell Stock IIIPythonO(n)O(1)Hard
132Palindrome Partitioning IIPythonO(n^2)O(n^2)Hard
139Word BreakC++PythonO(n * l^2)O(n)Medium
152Maximum Product SubarrayPythonO(n)O(1)Medium
174Dungeon GamePythonO(m * n)O(m + n)Hard
188Best Time to Buy and Sell Stock IVPythonO(k * n)O(k)Hard
198House RobberPythonO(n)O(1)Easy
213House Robber IIC++PythonO(n)O(1)Medium
221Maximal SquareC++PythonO(n^2)O(n)MediumEPI
256Paint HouseC++PythonO(n)O(1)Medium📖
265Paint House IIC++PythonO(n * k)O(k)Hard📖
276Paint FenceC++PythonO(n)O(1)Easy📖
279Perfect SquaresC++PythonO(n * sqrt(n))O(n)MediumHash
303Range Sum Query - ImmutableC++Pythonctor: O(n), lookup: O(1)O(n)Easy
304Range Sum Query 2D - ImmutableC++Pythonctor: O(m * n), lookup: O(1)O(m * n)Medium
309Best Time to Buy and Sell Stock with CooldownC++PythonO(n)O(1)Medium
312Burst BalloonsC++PythonO(n^3)O(n^2)Hard
322Coin ChangeC++PythonO(n * k)O(k)Medium
351Android Unlock PatternsC++PythonO(9^2 * 2^9)O(9 * 2^9)Medium📖Backtracking
357Count Numbers with Unique DigitsC++PythonO(n)O(1)MediumBacktracking, Math
361Bomb EnemyC++PythonO(m * n)O(m * n)Medium📖
368Largest Divisible SubsetC++PythonO(n^2)O(n)Medium
375Guess Number Higher or Lower IIC++PythonO(n^2)O(n^2)Medium
377Combination Sum IVC++PythonO(nlogn + n * t)O(t)Medium
403Frog JumpC++PythonO(n)O(n) ~ O(n^2)Hard
416Partition Equal Subset SumC++PythonO(n * s)O(s)Medium
418Sentence Screen FittingC++PythonO(r + n * c)O(n)Medium📖
446Arithmetic Slices II - SubsequenceC++PythonO(n^2)O(n * d)Hard
465Optimal Account BalancingC++PythonO(n * 2^n)O(n * 2^n)Hard📖
466Count The RepetitionsC++PythonO(s1 * min(s2, n1))O(s2)Hard
467Unique Substrings in Wraparound StringC++PythonO(n)O(1)Medium
471Encode String with Shortest LengthC++PythonO(n^3) on averageO(n^2)Medium📖
472Concatenated WordsC++PythonO(n * l^2)O(n * l)Medium
474Ones and ZeroesC++PythonO(s * m * n)O(m * n)Medium
6502 Keys KeyboardC++PythonO(sqrt(n))O(1)Medium
656Coin PathC++PythonO(n * B)O(n)Hard📖
664Strange PrinterC++PythonO(n^3)O(n^2)Hard
673Number of Longest Increasing SubsequenceC++PythonO(n^2)O(n)Medium

Greedy

#TitleSolutionTimeSpaceDifficultyTagNote
11Container With Most WaterC++PythonO(n)O(1)Medium
42Trapping Rain WaterC++PythonO(n)O(1)HardTricky
44Wildcard MatchingPythonO(m + n)O(1)HardTricky
45Jump Game IIPythonO(n)O(1)Hard
55Jump GamePythonO(n)O(1)Medium
122Best Time to Buy and Sell Stock IIPythonO(n)O(1)Medium
134Gas StationPythonO(n)O(1)Medium
135CandyC++PythonO(n)O(n)Hard
316Remove Duplicate LettersC++PythonO(n)O(k)HardAscending Stack
321Create Maximum NumberC++PythonO(k * (m + n + k)) ~ O(k * (m + n + k^2))O(m + n + k^2)Hardvariant of Delete DigitsGreedy, DP
330Patching ArrayC++PythonO(s + logn)O(1)Hard
376Wiggle SubsequenceC++PythonO(n)O(1)Medium
392Is SubsequenceC++PythonO(n)O(1)Medium
397Integer ReplacementC++PythonO(n)O(1)MediumMath
402Remove K DigitsC++PythonO(n)O(n)MediumLintCode
435Non-overlapping IntervalsC++PythonO(nlogn)O(1)Medium
452Minimum Number of Arrows to Burst BalloonsC++PythonO(nlogn)O(1)Medium
455Assign CookiesC++PythonO(nlogn)O(1)Easy
646Maximum Length of Pair ChainC++PythonO(nlogn)O(1)MediumScan Line
649Dota2 SenateC++PythonO(n)O(n)Medium
659Split Array into Consecutive SubsequencesC++PythonO(n)O(1)Medium

Design

#TitleSolutionTimeSpaceDifficultyTagNote
146LRU CacheC++PythonO(1)O(k)Hard
225Implement Stack using QueuesC++Pythonpush: O(n), pop: O(1), top: O(1)O(n)Easy
284Peeking IteratorC++PythonO(1)O(1)Medium
348Design Tic-Tac-ToeC++PythonO(1)O(n^2)Medium📖
353Design Snake GameC++PythonO(1)O(s)Medium📖Deque
355Design TwitterC++PythonO(klogu)O(t + f)MediumLintCodeHeap
359Logger Rate LimiterC++PythonO(1), amortizedO(k)Easy📖Deque
362Design Hit CounterC++PythonO(1), amortizedO(k)Medium📖Deque
379Design Phone DirectoryC++PythonO(1)O(n)Medium📖
380Insert Delete GetRandom O(1)C++PythonO(1)O(n)Hard
381Insert Delete GetRandom O(1) - Duplicates allowedC++PythonO(1)O(n)Hard
432All O`one Data StructureC++PythonO(1)O(n)Hard
460LFU CacheC++PythonO(1)O(k)Hard

SQL

#TitleSolutionTimeSpaceDifficultyTagNote
175Combine Two TablesMySQLO(m + n)O(m + n)Easy
176Second Highest SalaryMySQLO(n)O(1)Easy
177Nth Highest SalaryMySQLO(n^2)O(n)Medium
178Rank ScoresMySQLO(n^2)O(n)Medium
180Consecutive NumbersMySQLO(n)O(n)Medium
181Employees Earning More Than Their ManagersMySQLO(n^2)O(1)Easy
182Duplicate EmailsMySQLO(n^2)O(n)Easy
183Customers Who Never OrderMySQLO(n^2)O(1)Easy
184Department Highest SalaryMySQLO(n^2)O(n)Medium
185Department Top Three SalariesMySQLO(n^2)O(n)Hard
196Delete Duplicate EmailsMySQLO(n^2)O(n)Easy
197Rising TemperatureMySQLO(n^2)O(n)Easy
262Trips and UsersMySQLO((t * u) + tlogt)O(t)Hard

Shell Script

#TitleSolutionTimeSpaceDifficultyTagNote
192Word FrequencyShellO(n)O(k)Medium
193Valid Phone NumbersShellO(n)O(1)Easy
194Transpose FileShellO(n^2)O(n^2)Medium
195Tenth LineShellO(n)O(1)Easy

About

📝 Python / C++ 11 Solutions of LeetCode Questions

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' GitHub - shinan6/LeetCode: :pencil: Python / C++ 11 Solutions of LeetCode Questions · GitHub
Skip to content

Latest commit

History

4,201 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

The number of LeetCode questions is increasing every week. For more questions and solutions, you can see my LintCode repository. I'll keep updating for full summary and better solutions. Stay tuned for updates. (Notes: "📖" means you need to subscribe to LeetCode premium membership for the access to premium questions.)

Algorithms

Database

Shell

Bit Manipulation

#TitleSolutionTimeSpaceDifficultyTagNote
136Single NumberC++PythonO(n)O(1)Easy
137Single Number IIC++PythonO(n)O(1)Medium
190Reverse BitsC++PythonO(1)O(1)Easy
191Number of 1 BitsC++PythonO(1)O(1)Easy
201Bitwise AND of Numbers RangeC++PythonO(1)O(1)Medium
231Power of TwoC++PythonO(1)O(1)EasyLintCode
260Single Number IIIC++PythonO(n)O(1)Medium
268Missing NumberC++PythonO(n)O(1)MediumLintCode
318Maximum Product of Word LengthsC++PythonO(n) ~ O(n^2)O(n)MediumBit Manipulation, Counting Sort, Pruning
342Power of FourC++PythonO(1)O(1)Easy
371Sum of Two IntegersC++PythonO(1)O(1)EasyLintCode
389Find the DifferenceC++PythonO(n)O(1)Easy
393UTF-8 ValidationC++PythonO(n)O(1)Medium
401Binary WatchC++PythonO(1)O(1)Easy
411Minimum Unique Word AbbreviationC++PythonO(2^n)O(n)Hard📖
421Maximum XOR of Two Numbers in an ArrayC++PythonO(n)O(1)Medium
461Hamming DistanceC++PythonO(1)O(1)Easy
462Minimum Moves to Equal Array Elements IIC++PythonO(n) on averageO(1)Medium
477Total Hamming DistanceC++PythonO(n)O(1)Medium
645Set MismatchC++PythonO(n)O(1)Easy

Array

#TitleSolutionTimeSpaceDifficultyTagNote
153 SumC++PythonO(n^2)O(1)MediumTwo Pointers
163 Sum ClosestC++PythonO(n^2)O(1)MediumTwo Pointers
184 SumC++PythonO(n^3)O(1)MediumTwo Pointers
26Remove Duplicates from Sorted ArrayC++PythonO(n)O(1)EasyTwo Pointers
27Remove ElementC++PythonO(n)O(1)Easy
31Next PermutationC++PythonO(n)O(1)MediumTricky
41First Missing PositiveC++PythonO(n)O(1)HardTricky
48Rotate ImageC++PythonO(n^2)O(1)Medium
54Spiral MatrixC++PythonO(m * n)O(1)Medium
59Spiral Matrix IIC++PythonO(n^2)O(1)Medium
66Plus OneC++PythonO(n)O(1)Easy
73Set Matrix ZeroesC++PythonO(m * n)O(1)Medium
80Remove Duplicates from Sorted Array IIC++PythonO(n)O(1)MediumTwo Pointers
118Pascal's TriangleC++PythonO(n^2)O(1)Easy
119Pascal's Triangle IIC++PythonO(n^2)O(1)Easy
121Best Time to Buy and Sell StockC++PythonO(n)O(1)Easy
128Longest Consecutive SequenceC++PythonO(n)O(n)HardTricky
157Read N Characters Given Read4C++PythonO(n)O(1)Easy📖
158Read N Characters Given Read4 II - Call multiple timesC++PythonO(n)O(1)Hard📖
163Missing RangesC++PythonO(n)O(1)Medium📖
169Majority ElementC++PythonO(n)O(1)Easy
189Rotate ArrayC++PythonO(n)O(1)Easy
209Minimum Size Subarray SumC++PythonO(n)O(1)MediumBinary Search
215Kth Largest Element in an ArrayC++PythonO(n) ~ O(n^2)O(1)MediumEPI
228Summary RangesC++PythonO(n)O(1)Medium
229Majority Element IIC++PythonO(n)O(1)Medium
238Product of Array Except SelfC++PythonO(n)O(1)MediumLintCode
240Search a 2D Matrix IIC++PythonO(m + n)O(1)MediumEPI, LintCode
243Shortest Word DistanceC++PythonO(n)O(1)Easy📖
245Shortest Word Distance IIIC++PythonO(n)O(1)Medium📖
251Flatten 2D VectorC++PythonO(1)O(1)Medium📖
277Find the CelebrityC++PythonO(n)O(1)Medium📖, EPI
289Game of LifeC++PythonO(m * n)O(1)Medium
293Flip GameC++PythonO(n * (c+1))O(1)Easy📖
296Best Meeting PointC++PythonO(m * n)O(m + n)Hard📖
311Sparse Matrix MultiplicationC++PythonO(m * n * l)O(m * l)Medium📖
334Increasing Triplet SubsequenceC++PythonO(n)O(1)Medium
370Range AdditionC++PythonO(k + n)O(1)Medium📖
384Shuffle an ArrayC++PythonO(n)O(n)MediumEPI
396Rotate FunctionC++PythonO(n)O(1)Easy
412Fizz BuzzC++PythonO(n)O(1)Easy
414Third Maximum NumberC++PythonO(n)O(1)Easy
419Battleships in a BoardC++PythonO(m * n)O(1)Medium
422Valid Word SquareC++PythonO(m * n)O(1)Easy📖
442Find All Duplicates in an ArrayC++PythonO(n)O(1)Medium
448Find All Numbers Disappeared in an ArrayC++PythonO(n)O(1)Easy
661Image SmootherC++PythonO(m * n)O(1)Easy
665Non-decreasing ArrayC++PythonO(n)O(1)Easy
667Beautiful Arrangement IIC++PythonO(n)O(1)Medium
670Maximum SwapC++PythonO(logn)O(logn)Medium
674Longest Continuous Increasing SubsequenceC++PythonO(n)O(1)Easy

String

#TitleSolutionTimeSpaceDifficultyTagNote
5Longest Palindromic SubstringC++PythonO(n)O(n)MediumManacher's Algorithm
6ZigZag ConversionC++PythonO(n)O(1)Easy
8String to Integer (atoi)C++PythonO(n)O(1)Easy
14Longest Common PrefixC++PythonO(n * k)O(1)Easy
28Implement strStr()C++PythonO(n + k)O(k)EasyKMP Algorithm
38Count and SayC++PythonO(n * 2^n)O(2^n)Easy
43Multiply StringsC++PythonO(m * n)O(m + n)Medium
58Length of Last WordC++PythonO(n)O(1)Easy
67Add BinaryC++PythonO(n)O(1)Easy
68Text JustificationC++PythonO(n)O(1)Hard
125Valid PalindromeC++PythonO(n)O(1)Easy
151Reverse Words in a StringC++PythonO(n)O(1)Medium
161One Edit DistanceC++PythonO(m + n)O(1)Medium📖
165Compare Version NumbersC++PythonO(n)O(1)Easy
186Reverse Words in a String IIC++PythonO(n)O(1)Medium📖
214Shortest PalindromeC++PythonO(n)O(n)HardKMP AlgorithmManacher's Algorithm
242Valid AnagramC++PythonO(n)O(1)EasyLintCode
271Encode and Decode StringsC++PythonO(n)O(1)Medium📖
273Integer to English WordsC++PythonO(logn)O(1)Hard
306Addictive NumberC++PythonO(n^3)O(n)Medium
383Ransom NoteC++PythonO(n)O(1)EasyEPI
405Convert a Number to HexadecimalC++PythonO(n)O(1)Easy
408Valid Word AbbreviationC++PythonO(n)O(1)Easy📖
415Add StringsC++PythonO(n)O(1)Easy
420Strong Password CheckerC++PythonO(n)O(1)Hard
434Number of Segments in a StringC++PythonO(n)O(1)Easy
459Repeated Substring PatternC++PythonO(n)O(n)EasyKMP Algorithm
468Validate IP AddressC++PythonO(1)O(1)Medium
556Next Greater Element IIIC++PythonO(1)O(1)Medium
557Reverse Words in a String IIIC++PythonO(n)O(1)Easy
647Palindromic SubstringsC++PythonO(n)O(n)MediumManacher's Algorithm
648Replace WordsC++PythonO(n)O(t)Mediumtrie
657Judge Route CircleC++PythonO(n)O(1)Easy
678Valid Parenthesis StringC++PythonO(n)O(1)Medium
680Valid Palindrome IIC++PythonO(n)O(1)Easy

Linked List

#TitleSolutionTimeSpaceDifficultyTagNote
2Add Two NumbersC++PythonO(n)O(1)Medium
21Merge Two Sorted ListsC++PythonO(n)O(1)Easy
23Merge k Sorted ListsC++PythonO(nlogk)O(1)HardHeap, Divide and Conquer
24Swap Nodes in PairsC++PythonO(n)O(1)Easy
25Reverse Nodes in k-GroupC++PythonO(n)O(1)Hard
61Rotate ListC++PythonO(n)O(1)Medium
82Remove Duplicates from Sorted List IIC++PythonO(n)O(1)Medium
83Remove Duplicates from Sorted ListC++PythonO(n)O(1)Easy
92Reverse Linked List IIC++PythonO(n)O(1)Medium
138Copy List with Random PointerC++PythonO(n)O(1)Hard
160Intersection of Two Linked ListsC++PythonO(m + n)O(1)Easy
203Remove Linked List ElementsC++PythonO(n)O(1)Easy
206Reverse Linked ListC++PythonO(n)O(1)Easy
234Palindrome Linked ListC++PythonO(n)O(1)Easy
237Delete Node in a Linked ListC++PythonO(1)O(1)EasyLintCode
328Odd Even Linked ListC++PythonO(n)O(1)Medium
369Plus One Linked ListC++PythonO(n)O(1)Medium📖Two Pointers
445Add Two Numbers IIC++PythonO(m + n)O(m + n)Medium

Stack

#TitleSolutionTimeSpaceDifficultyTagNote
20Valid ParenthesesC++PythonO(n)O(n)Easy
32Longest Valid ParenthesesC++PythonO(n)O(1)Hard
71Simplify PathC++PythonO(n)O(n)Medium
84Largest Rectangle in HistogramC++PythonO(n)O(n)HardAscending Stack, DP
85Maximal RectangleC++PythonO(m * n)O(n)HardEPIAscending Stack
101Symmetric TreeC++PythonO(n)O(h)Easy
150Evaluate Reverse Polish NotationC++PythonO(n)O(n)Medium
155Min StackC++PythonO(n)O(1)Easy
173Binary Search Tree IteratorC++PythonO(1)O(h)Medium
224Basic CalculatorC++PythonO(n)O(n)Hard
227Basic Calculator IIC++PythonO(n)O(n)Medium
232Implement Queue using StacksC++PythonO(1), amortizedO(n)EasyEPI, LintCode
255Verify Preorder Sequence in Binary Search TreeC++PythonO(n)O(1)Medium📖
272Closest Binary Search Tree Value IIC++PythonO(h + k)O(h)Hard📖
331Verify Preorder Serialization of a Binary TreeC++PythonO(n)O(1)Medium
341Flatten Nested List IteratorC++PythonO(n)O(h)Medium📖Iterator
385Mini ParserC++PythonO(n)O(h)Medium
394Decode StringC++PythonO(n)O(h)Medium
439Ternary Expression ParserC++PythonO(n)O(1)Medium📖
456132 PatternC++PythonO(n)O(n)Medium

Queue

#TitleSolutionTimeSpaceDifficultyTagNote
239Sliding Window MaximumC++PythonO(n)O(k)HardEPI, LintCode
281Zigzag IteratorC++PythonO(n)O(k)Medium📖
346Moving Average from Data StreamC++PythonO(1)O(w)Easy📖

Heap

#TitleSolutionTimeSpaceDifficultyTagNote
264Ugly Number IIC++PythonO(n)O(1)MediumCTCI, LintCodeBST, Heap
295Find Median from Data StreamC++PythonO(nlogn)O(n)HardEPI, LintCodeBST, Heap
313Super Ugly NumberC++PythonO(n * k)O(n + k)MediumBST, Heap
358Rearrange String k Distance ApartC++PythonO(n)O(n)Hard📖Greedy, Heap
373Find K Pairs with Smallest SumsC++PythonO(k * log(min(n, m, k)))O(min(n, m, k))Medium
378Kth Smallest Element in a Sorted MatrixC++PythonO(k * log(min(n, m, k)))O(min(n, m, k))MediumLintCode
407Trapping Rain Water IIC++PythonO(m * n * (logm + logn))O(m * n)HardLintCode

Tree

#TitleSolutionTimeSpaceDifficultyTagNote
94Binary Tree Inorder TraversalC++PythonO(n)O(1)MediumMorris Traversal
99Recover Binary Search TreeC++PythonO(n)O(1)HardMorris Traversal
144Binary Tree Preorder TraversalC++PythonO(n)O(1)MediumMorris Traversal
145Binary Tree Postorder TraversalC++PythonO(n)O(1)HardMorris Traversal
208Implement Trie (Prefix Tree)C++PythonO(n)O(1)MediumTrie
211Add and Search Word - Data structure designC++PythonO(min(n, h))O(min(n, h))MediumTrie, DFS
226Invert Binary TreeC++PythonO(n)O(h), O(w)Easy
297Serialize and Deserialize Binary TreeC++PythonO(n)O(h)HardLintCodeDFS
307Range Sum Query - MutableC++Pythonctor: O(n), update: O(logn), query: O(logn)O(n)MediumLintCodeDFS, Segment Tree, BIT
308Range Sum Query 2D - MutableC++Pythonctor: O(m * n), update: O(logm + logn), query: O(logm + logn)O(m * n)Hard📖DFS, Segment Tree, BIT
315Count of Smaller Numbers After SelfC++PythonO(nlogn)O(n)HardLintCodeBST, BIT, Divide and Conquer
652Find Duplicate SubtreesC++PythonO(n)O(n)MediumDFS, Hash
653Two Sum IV - Input is a BSTC++PythonO(n)O(h)EasyTwo Pointers
654Maximum Binary TreeC++PythonO(n)O(n)MediumLintCodeDescending Stack
655Print Binary TreeC++PythonO(n)O(h)Medium
662Maximum Width of Binary TreeC++PythonO(n)O(h)MediumDFS
663Equal Tree PartitionC++PythonO(n)O(n)Medium📖Hash
677Map Sum PairsC++PythonO(n)O(t)MediumTrie

Hash Table

#TitleSolutionTimeSpaceDifficultyTagNote
1Two SumC++PythonO(n)O(n)Easy
3Longest Substring Without Repeating CharactersC++PythonO(n)O(1)Medium
30Substring with Concatenation of All WordsC++PythonO(m * n * k)O(n * k)Hard
36Valid SudokuC++PythonO(9^2)O(9)Easy
49Group AnagramsC++PythonO(n * glogg)O(n)Medium
76Minimum Window SubstringC++PythonO(n)O(k)Hard
149Max Points on a LineC++PythonO(n^2)O(n)Hard
159Longest Substring with At Most Two Distinct CharactersC++PythonO(n)O(1)Hard📖
170Two Sum III - Data structure designC++PythonO(n)O(n)Easy📖
187Repeated DNA SequencesPythonO(n)O(n)Medium
202Happy NumberC++PythonO(k)O(k)Easy
204Count PrimesC++PythonO(n)O(n)Easy
205Isomorphic StringsC++PythonO(n)O(1)Easy
217Contains DuplicateC++PythonO(n)O(n)Easy
219Contains Duplicate IIC++PythonO(n)O(n)Easy
244Shortest Word Distance IIC++Pythonctor: O(n), lookup: O(a + b)O(n)Medium📖
246Strobogrammatic NumberC++PythonO(n)O(1)Easy📖
249Group Shifted StringsC++PythonO(nlogn)O(n)Easy📖
266Palindrome PermutationC++PythonO(n)O(1)Easy📖
288Unique Word AbbreviationC++Pythonctor: O(n), lookup: O(1)O(k)Easy📖
290Word PatternC++PythonO(n)O(c)Easyvariant of Isomorphic Strings
299Bulls and CowsC++PythonO(n)O(1)Easy
305Number of Islands IIC++PythonO(k)O(k)HardLintCode, 📖Union Find
314Binary Tree Vertical Order TraversalC++PythonO(n)O(n)Medium📖BFS
323Number of Connected Components in an Undirected GraphC++PythonO(n)O(n)Medium📖Union Find
325Maximum Size Subarray Sum Equals kC++PythonO(n)O(n)Medium📖
336Palindrome PairsC++PythonO(n * k^2)O(n * k)Hard
340Longest Substring with At Most K Distinct CharactersC++PythonO(n)O(1)Hard📖
356Line ReflectionC++PythonO(n)O(n)Medium📖Hash, Two Pointers
387First Unique Character in a StringC++PythonO(n)O(n)Easy
388Longest Absolute File PathC++PythonO(n)O(d)MediumStack
409Longest PalindromeC++PythonO(n)O(1)Easy
424Longest Repeating Character ReplacementC++PythonO(n)O(1)Medium
438Find All Anagrams in a StringC++PythonO(n)O(1)Easy
447Number of BoomerangsC++PythonO(n^2)O(n)Easy
4544Sum IIC++PythonO(n^2)O(n^2)Medium
473Matchsticks to SquareC++PythonO(n * s * 2^n)O(n * (2^n + s))Medium
554Brick WallC++PythonO(n)O(m)Medium

Math

#TitleSolutionTimeSpaceDifficultyTagNote
7Reverse IntegerC++PythonO(1)O(1)Easy
9Palindrome NumberC++PythonO(1)O(1)Easy
12Integer to RomanC++PythonO(n)O(1)Medium
13Roman to IntegerC++PythonO(n)O(1)Easy
29Divide Two IntegersC++PythonO(1)O(1)Medium
50Pow(x, n)C++PythonO(1)O(1)Medium
60Permutation SequenceC++PythonO(n^2)O(n)MediumCantor Ordering
65Valid NumberC++PythonO(n)O(1)HardAutomata
89Gray CodeC++PythonO(2^n)O(1)Medium
166Fraction to Recurring DecimalC++PythonO(logn)O(1)Medium
168Excel Sheet Column TitleC++PythonO(logn)O(1)Easy
171Excel Sheet Column NumberC++PythonO(n)O(1)Easy
172Factorial Trailing ZeroesC++PythonO(1)O(1)Easy
223Rectangle AreaC++PythonO(1)O(1)Easy
233Number of Digit OneC++PythonO(1)O(1)HardCTCI, LintCode
248Strobogrammatic Number IIIC++PythonO(5^(n/2))O(n)Hard📖
258Add DigitsC++PythonO(1)O(1)Easy
263Ugly NumberC++PythonO(1)O(1)Easy
292Nim GameC++PythonO(1)O(1)EasyLintCode
319Bulb SwitcherC++PythonO(1)O(1)Medium
326Power of ThreeC++PythonO(1)O(1)Easy
335Self CrossingC++PythonO(n)O(1)Hard
338Counting BitsC++PythonO(n)O(n)Medium
343Integer BreakC++PythonO(logn)O(1)MediumTricky, DP
365Water and Jug ProblemC++PythonO(logn)O(1)MediumEuclidean Algorithm
372Super PowC++PythonO(n)O(1)Medium
382Linked List Random NodeC++PythonO(n)O(1)MediumReservoir Sampling
386Lexicographical NumbersC++PythonO(n)O(1)Medium
390Elimination GameC++PythonO(logn)O(1)Medium
391Perfect RectangleC++PythonO(n)O(n)Hard
398Random Pick IndexC++PythonO(n)O(1)MediumReservoir Sampling
400Nth DigitC++PythonO(logn)O(1)Easy
413Arithmetic SlicesC++PythonO(n)O(1)Medium
423Reconstruct Original Digits from EnglishC++PythonO(n)O(1)MediumGCJ2016 - Round 1B
441Arranging CoinsC++PythonO(nlogn)O(1)EasyBinary Search
453Minimum Moves to Equal Array ElementsC++PythonO(n)O(1)Easy
458Poor PigsC++PythonO(n)O(1)Easy
469Convex PolygonC++PythonO(n)O(1)Medium📖
6514 Keys KeyboardC++PythonO(1)O(1)Medium📖Math, DP
660Remove 9C++PythonO(logn)O(1)Hard📖
672Bulb Switcher IIC++PythonO(1)O(1)Medium

Sort

#TitleSolutionTimeSpaceDifficultyTagNote
56Merge IntervalsC++PythonO(nlogn)O(1)Hard
57Insert IntervalC++PythonO(n)O(1)Hard
75Sort ColorsC++PythonO(n)O(1)MediumTri Partition
88Merge Sorted ArrayC++PythonO(n)O(1)Easy
147Insertion Sort ListC++PythonO(n^2)O(1)Medium
148Sort ListC++PythonO(nlogn)O(logn)Medium
164Maximum GapC++PythonO(n)O(n)HardTricky
179Largest NumberC++PythonO(nlogn)O(1)Medium
218The Skyline ProblemC++PythonO(nlogn)O(n)HardSort, BST
252Meeting RoomsC++PythonO(nlogn)O(n)Easy📖
253Meeting Rooms IIC++PythonO(nlogn)O(n)Medium📖
274H-IndexC++PythonO(n)O(n)MediumCounting Sort
280Wiggle SortC++PythonO(n)O(1)Medium📖
324Wiggle Sort IIC++PythonO(n) on averageO(1)Mediumvariant of Sort ColorsTri Partition
347Top K Frequent ElementsC++PythonO(n) on averageO(n)MediumQuick Select, Heap
406Queue Reconstruction by HeightC++PythonO(n * sqrt(n))O(n)Medium
451Sort Characters By FrequencyC++PythonO(n)O(n)Medium

Two Pointers

#TitleSolutionTimeSpaceDifficultyTagNote
19Remove Nth Node From End of ListC++PythonO(n)O(1)Easy
86Partition ListC++PythonO(n)O(1)Medium
141Linked List CycleC++PythonO(n)O(1)Easy
142Linked List Cycle IIC++PythonO(n)O(1)Medium
143Reorder ListC++PythonO(n)O(1)Medium
167Two Sum II - Input array is sortedC++PythonO(n)O(1)Medium
2593Sum SmallerC++PythonO(n^2)O(1)Medium📖, LintCode
283Move ZeroesC++PythonO(n)O(1)Easy
287Find the Duplicate NumberC++PythonO(n)O(1)HardBinary Search, Two Pointers
344Reverse StringC++PythonO(n)O(1)Easy
345Reverse Vowels of a StringC++PythonO(n)O(1)Easy
349Intersection of Two ArraysC++PythonO(m + n)O(min(m, n))EasyEPIHash, Binary Search
350Intersection of Two Arrays IIC++PythonO(m + n)O(1)EasyEPIHash, Binary Search
360Sort Transformed ArrayC++PythonO(n)O(1)Medium📖
457Circular Array LoopC++PythonO(n)O(1)Medium

Recursion

#TitleSolutionTimeSpaceDifficultyTagNote
95Unique Binary Search Trees IIC++PythonO(4^n / n^(3/2)O(4^n / n^(3/2)Medium
98Validate Binary Search TreeC++PythonO(n)O(1)Medium
100Same TreeC+PythonO(n)O(h)Easy
104Maximum Depth of Binary TreeC++PythonO(n)O(h)Easy
105Construct Binary Tree from Preorder and Inorder TraversalC++PythonO(n)O(n)Medium
106Construct Binary Tree from Inorder and Postorder TraversalC++PythonO(n)O(n)Medium
108Convert Sorted Array to Binary Search TreeC++PythonO(n)O(logn)Medium
109Convert Sorted List to Binary Search TreeC++PythonO(n)O(logn)Medium
110Balanced Binary TreePythonO(n)O(h)Easy
111Minimum Depth of Binary TreePythonO(n)O(h)Easy
114Flatten Binary Tree to Linked ListPythonO(n)O(h)Medium
116Populating Next Right Pointers in Each NodePythonO(n)O(1)Medium
124Binary Tree Maximum Path SumPythonO(n)O(h)Hard
129Sum Root to Leaf NumbersPythonO(n)O(h)Medium
156Binary Tree Upside DownPythonO(n)O(1)Medium📖
241Different Ways to Add ParenthesesC++PythonO(n * 4^n / n^(3/2))O(n * 4^n / n^(3/2))Medium
298Binary Tree Longest Consecutive SequenceC++PythonO(n)O(h)Medium📖
327Count of Range SumC++PythonO(nlogn)O(n)Hard
333Largest BST SubtreeC++PythonO(n)O(h)Medium📖
337House Robber IIIC++PythonO(n)O(h)Medium
395Longest Substring with At Least K Repeating CharactersC++PythonO(n)O(1)Medium
404Sum of Left LeavesC++PythonO(n)O(h)Easy
437Path Sum IIIC++PythonO(n)O(h)Easy
549Binary Tree Longest Consecutive Sequence IIC++PythonO(n)O(h)Medium📖
669Trim a Binary Search TreeC++PythonO(n)O(h)Easy
671Second Minimum Node In a Binary TreeC++PythonO(n)O(h)Easy

Binary Search

#TitleSolutionTimeSpaceDifficultyTagNote
4Median of Two Sorted ArraysC++PythonO(log(min(m, n)))O(1)Hard
33Search in Rotated Sorted ArrayC++PythonO(logn)O(1)Hard
34Search for a RangeC++PythonO(logn)O(1)Medium
35Search Insert PositionC++PythonO(logn)O(1)Medium
69Sqrt(x)C++PythonO(logn)O(1)Medium
74Search a 2D MatrixC++PythonO(logm + logn)O(1)Medium
81Search in Rotated Sorted Array IIC++PythonO(logn)O(1)Medium
153Find Minimum in Rotated Sorted ArrayC++PythonO(logn)O(1)Medium
154Find Minimum in Rotated Sorted Array IIC++PythonO(logn) ~ O(n)O(1)Hard
162Find Peak ElementC++PythonO(logn)O(1)Medium
222Count Complete Tree NodesC++PythonO((logn)^2)O(1)Medium
275H-Index IIC++PythonO(logn)O(1)MediumBinary Search
278First Bad VersionC++PythonO(logn)O(1)EasyLintCode
300Longest Increasing SubsequenceC++PythonO(nlogn)O(n)MediumCTCI, LintCodeBinary Search, DP
302Smallest Rectangle Enclosing Black PixelsC++PythonO(nlogn)O(1)Hard📖
354Russian Doll EnvelopesC++PythonO(nlogn)O(1)Hard
363Max Sum of Rectangle No Larger Than KC++PythonO(min(m, n)^2 * max(m, n) * logn(max(m, n)))O(max(m, n))Hard
367Valid Perfect SquareC++PythonO(logn)O(1)Medium
374Guess Number Higher or LowerC++PythonO(logn)O(1)Easy
410Split Array Largest SumC++PythonO(nlogs)O(1)Hard
436Find Right IntervalC++PythonO(nlogn)O(n)Medium
475HeatersC++PythonO((m + n) * logn)O(1)Easy
658Find K Closest ElementsC++PythonO(logn + k)O(1)Medium
668Kth Smallest Number in Multiplication TableC++PythonO(m * log(m * n))O(1)Hard

Binary Search Tree

#TitleSolutionTimeSpaceDifficultyTagNote
220Contains Duplicate IIIC++PythonO(nlogk)O(k)Medium
230Kth Smallest Element in a BSTC++PythonO(max(h, k))O(min(h, k))Medium
235Lowest Common Ancestor of a Binary Search TreeC++PythonO(h)O(1)EasyEPI
270Closest Binary Search Tree ValueC++PythonO(h)O(1)Easy📖
285Inorder Successor in BSTC++PythonO(h)O(1)Medium📖
352Data Stream as Disjoint IntervalsC++PythonO(logn)O(n)Hard
449Serialize and Deserialize BSTC++PythonO(n)O(h)Medium
450Delete Node in a BSTC++PythonO(h)O(h)Medium

Breadth-First Search

#TitleSolutionTimeSpaceDifficultyTagNote
102Binary Tree Level Order TraversalC++PythonO(n)O(n)Easy
107Binary Tree Level Order Traversal IIPythonO(n)O(n)Easy
103Binary Tree Zigzag Level Order TraversalPythonO(n)O(n)Medium
117Populating Next Right Pointers in Each Node IIPythonO(n)O(1)Hard
127Word LadderPythonO(n * d)O(d)Medium
130Surrounded RegionsC++PythonO(m * n)O(m + n)Medium
133Clone GraphPythonO(n)O(n)Medium
207Course SchedulePythonO(|V| + |E|)O(|E|)MediumTopological Sort
210Course Schedule IIPythonO(|V| + |E|)O(|E|)MediumTopological Sort
261Graph Valid TreeC++PythonO(|V| + |E|)O(|V| + |E|)Medium📖
269Alien DictionaryC++PythonO(n)O(1)Hard📖Topological Sort, BFS, DFS
286Walls and GatesC++PythonO(m * n)O(g)Medium📖
310Minimum Height TreesC++PythonO(n)O(n)Medium
317Shortest Distance from All BuildingsC++PythonO(k * m * n)O(m * n)Hard📖
433Minimum Genetic MutationC++PythonO(n * b)O(b)Medium
444Sequence ReconstructionC++PythonO(n * s)O(n)Medium📖Topological Sort
666Path Sum IVC++PythonO(n)O(w)Medium📖Topological Sort
675Cut Off Trees for Golf EventC++PythonO(t * m * n)O(m * n)HardA* Search Algorithm

Depth-First Search

#TitleSolutionTimeSpaceDifficultyTagNote
112Path SumPythonO(n)O(h)Easy
113Path Sum IIPythonO(n)O(h)Medium
199Binary Tree Right Side ViewPythonO(n)O(h)Medium
200Number of IslandsPythonO(m * n)O(m * n)Medium
236Lowest Common Ancestor of a Binary TreeC++PythonO(n)O(h)MediumEPI
247Strobogrammatic Number IIC++PythonO(n^2 * 5^(n/2))O(n)Medium📖
250Count Univalue SubtreesC++PythonO(n)O(h)Medium📖
257Binary Tree PathsC++PythonO(n * h)O(h)Easy
282Expression Add OperatorsC++PythonO(4^n)O(n)Hard
301Remove Invalid ParenthesesC++PythonO(C(n, c))O(c)Hard
329Longest Increasing Path in a MatrixC++PythonO(m * n)O(m * n)Hard
332Reconstruct ItineraryC++PythonO(t! / (n1! * n2! * ... nk!))O(t)Medium
339Nested List Weight SumC++PythonO(n)O(h)Easy📖
364Nested List Weight Sum IIC++PythonO(n)O(h)Medium📖
366Find Leaves of Binary TreeC++PythonO(n)O(h)Medium📖
399Evaluate DivisionC++PythonO(q * |V|!)O(e)Medium
417Pacific Atlantic Water FlowC++PythonO(m * n)O(m * n)Medium
440K-th Smallest in Lexicographical OrderC++PythonO(logn)O(logn)Hard
464Can I WinC++PythonO(n!)O(n)Medium

Backtracking

#TitleSolutionTimeSpaceDifficultyTagNote
17Letter Combinations of a Phone NumberPythonO(n * 4^n)O(n)Medium
22Generate ParenthesesPythonO(4^n / n^(3/2))O(n)Medium
37Sudoku SolverPythonO((9!)^9)O(1)Hard
39Combination SumPythonO(k * n^k)O(k)Medium
40Combination Sum IIPythonO(k * C(n, k))O(k)Medium
46PermutationsPythonO(n * n!)O(n)Medium
47Permutations IIPythonO(n * n!)O(n)Medium
51N-QueensPythonO(n!)O(n)Hard
52N-Queens-IIPythonO(n!)O(n)Hard
77CombinationsPythonO(n!)O(n)Medium
79Word SearchPythonO(m * n * l)O(l)Medium
93Restore IP AddressesPythonO(1)O(1)Medium
78SubsetsC++PythonO(n * 2^n)O(1)Medium
90Subsets IIC++PythonO(n * 2^n)O(1)Medium
126Word Ladder IIPythonO(n * d)O(d)Hard
131Palindrome PartitioningPythonO(n^2) ~ O(2^n)O(n^2)Medium
140Word Break IIC++PythonO(n * l^2 + n * r)O(n^2)Hard
212Word Search IIC++PythonO(m * n * l)O(l)HardLintCodeTrie, DFS
216Combination Sum IIIC++PythonO(k * C(n, k))O(k)Medium
254Factor CombinationsC++PythonO(nlogn)O(logn)Medium📖
267Palindrome Permutation IIC++PythonO(n * n!)O(n)Medium📖
291Word Pattern IIC++PythonO(n * C(n - 1, c - 1))O(n + c)Hard📖
294Flip Game IIC++PythonO(n + c^2)O(c)Medium📖DP, Hash
320Generalized AbbreviationC++PythonO(n * 2^n)O(n)Medium📖
425Word SquaresC++PythonO(n^2 * n!)O(n^2)Hard📖
676Implement Magic DictionaryC++PythonO(n)O(d)MediumTrie, DFS
67924 GameC++PythonO(1)O(1)HardDFS

Dynamic Programming

#TitleSolutionTimeSpaceDifficultyTagNote
10Regular Expression MatchingPythonO(m * n)O(n)Hard
53Maximum SubarrayPythonO(n)O(1)Medium
62Unique PathsPythonO(m * n)O(m + n)Medium
63Unique Paths IIPythonO(m * n)O(m + n)Medium
64Minimum Path SumPythonO(m * n)O(m + n)Medium
70Climbing StairsPythonO(n)O(1)Easy
72Edit DistancePythonO(m * n)O(m + n)Hard
87Scramble StringPythonO(n^4)O(n^3)Hard
91Decode WaysC++PythonO(n)O(1)Medium
96Unique Binary Search TreesPythonO(n)O(1)MediumMath
97Interleaving StringPythonO(m * n)O(m + n)Hard
115Distinct SubsequencesPythonO(n^2)O(n)Hard
120TrianglePythonO(m * n)O(n)Medium
123Best Time to Buy and Sell Stock IIIPythonO(n)O(1)Hard
132Palindrome Partitioning IIPythonO(n^2)O(n^2)Hard
139Word BreakC++PythonO(n * l^2)O(n)Medium
152Maximum Product SubarrayPythonO(n)O(1)Medium
174Dungeon GamePythonO(m * n)O(m + n)Hard
188Best Time to Buy and Sell Stock IVPythonO(k * n)O(k)Hard
198House RobberPythonO(n)O(1)Easy
213House Robber IIC++PythonO(n)O(1)Medium
221Maximal SquareC++PythonO(n^2)O(n)MediumEPI
256Paint HouseC++PythonO(n)O(1)Medium📖
265Paint House IIC++PythonO(n * k)O(k)Hard📖
276Paint FenceC++PythonO(n)O(1)Easy📖
279Perfect SquaresC++PythonO(n * sqrt(n))O(n)MediumHash
303Range Sum Query - ImmutableC++Pythonctor: O(n), lookup: O(1)O(n)Easy
304Range Sum Query 2D - ImmutableC++Pythonctor: O(m * n), lookup: O(1)O(m * n)Medium
309Best Time to Buy and Sell Stock with CooldownC++PythonO(n)O(1)Medium
312Burst BalloonsC++PythonO(n^3)O(n^2)Hard
322Coin ChangeC++PythonO(n * k)O(k)Medium
351Android Unlock PatternsC++PythonO(9^2 * 2^9)O(9 * 2^9)Medium📖Backtracking
357Count Numbers with Unique DigitsC++PythonO(n)O(1)MediumBacktracking, Math
361Bomb EnemyC++PythonO(m * n)O(m * n)Medium📖
368Largest Divisible SubsetC++PythonO(n^2)O(n)Medium
375Guess Number Higher or Lower IIC++PythonO(n^2)O(n^2)Medium
377Combination Sum IVC++PythonO(nlogn + n * t)O(t)Medium
403Frog JumpC++PythonO(n)O(n) ~ O(n^2)Hard
416Partition Equal Subset SumC++PythonO(n * s)O(s)Medium
418Sentence Screen FittingC++PythonO(r + n * c)O(n)Medium📖
446Arithmetic Slices II - SubsequenceC++PythonO(n^2)O(n * d)Hard
465Optimal Account BalancingC++PythonO(n * 2^n)O(n * 2^n)Hard📖
466Count The RepetitionsC++PythonO(s1 * min(s2, n1))O(s2)Hard
467Unique Substrings in Wraparound StringC++PythonO(n)O(1)Medium
471Encode String with Shortest LengthC++PythonO(n^3) on averageO(n^2)Medium📖
472Concatenated WordsC++PythonO(n * l^2)O(n * l)Medium
474Ones and ZeroesC++PythonO(s * m * n)O(m * n)Medium
6502 Keys KeyboardC++PythonO(sqrt(n))O(1)Medium
656Coin PathC++PythonO(n * B)O(n)Hard📖
664Strange PrinterC++PythonO(n^3)O(n^2)Hard
673Number of Longest Increasing SubsequenceC++PythonO(n^2)O(n)Medium

Greedy

#TitleSolutionTimeSpaceDifficultyTagNote
11Container With Most WaterC++PythonO(n)O(1)Medium
42Trapping Rain WaterC++PythonO(n)O(1)HardTricky
44Wildcard MatchingPythonO(m + n)O(1)HardTricky
45Jump Game IIPythonO(n)O(1)Hard
55Jump GamePythonO(n)O(1)Medium
122Best Time to Buy and Sell Stock IIPythonO(n)O(1)Medium
134Gas StationPythonO(n)O(1)Medium
135CandyC++PythonO(n)O(n)Hard
316Remove Duplicate LettersC++PythonO(n)O(k)HardAscending Stack
321Create Maximum NumberC++PythonO(k * (m + n + k)) ~ O(k * (m + n + k^2))O(m + n + k^2)Hardvariant of Delete DigitsGreedy, DP
330Patching ArrayC++PythonO(s + logn)O(1)Hard
376Wiggle SubsequenceC++PythonO(n)O(1)Medium
392Is SubsequenceC++PythonO(n)O(1)Medium
397Integer ReplacementC++PythonO(n)O(1)MediumMath
402Remove K DigitsC++PythonO(n)O(n)MediumLintCode
435Non-overlapping IntervalsC++PythonO(nlogn)O(1)Medium
452Minimum Number of Arrows to Burst BalloonsC++PythonO(nlogn)O(1)Medium
455Assign CookiesC++PythonO(nlogn)O(1)Easy
646Maximum Length of Pair ChainC++PythonO(nlogn)O(1)MediumScan Line
649Dota2 SenateC++PythonO(n)O(n)Medium
659Split Array into Consecutive SubsequencesC++PythonO(n)O(1)Medium

Design

#TitleSolutionTimeSpaceDifficultyTagNote
146LRU CacheC++PythonO(1)O(k)Hard
225Implement Stack using QueuesC++Pythonpush: O(n), pop: O(1), top: O(1)O(n)Easy
284Peeking IteratorC++PythonO(1)O(1)Medium
348Design Tic-Tac-ToeC++PythonO(1)O(n^2)Medium📖
353Design Snake GameC++PythonO(1)O(s)Medium📖Deque
355Design TwitterC++PythonO(klogu)O(t + f)MediumLintCodeHeap
359Logger Rate LimiterC++PythonO(1), amortizedO(k)Easy📖Deque
362Design Hit CounterC++PythonO(1), amortizedO(k)Medium📖Deque
379Design Phone DirectoryC++PythonO(1)O(n)Medium📖
380Insert Delete GetRandom O(1)C++PythonO(1)O(n)Hard
381Insert Delete GetRandom O(1) - Duplicates allowedC++PythonO(1)O(n)Hard
432All O`one Data StructureC++PythonO(1)O(n)Hard
460LFU CacheC++PythonO(1)O(k)Hard

SQL

#TitleSolutionTimeSpaceDifficultyTagNote
175Combine Two TablesMySQLO(m + n)O(m + n)Easy
176Second Highest SalaryMySQLO(n)O(1)Easy
177Nth Highest SalaryMySQLO(n^2)O(n)Medium
178Rank ScoresMySQLO(n^2)O(n)Medium
180Consecutive NumbersMySQLO(n)O(n)Medium
181Employees Earning More Than Their ManagersMySQLO(n^2)O(1)Easy
182Duplicate EmailsMySQLO(n^2)O(n)Easy
183Customers Who Never OrderMySQLO(n^2)O(1)Easy
184Department Highest SalaryMySQLO(n^2)O(n)Medium
185Department Top Three SalariesMySQLO(n^2)O(n)Hard
196Delete Duplicate EmailsMySQLO(n^2)O(n)Easy
197Rising TemperatureMySQLO(n^2)O(n)Easy
262Trips and UsersMySQLO((t * u) + tlogt)O(t)Hard

Shell Script

#TitleSolutionTimeSpaceDifficultyTagNote
192Word FrequencyShellO(n)O(k)Medium
193Valid Phone NumbersShellO(n)O(1)Easy
194Transpose FileShellO(n^2)O(n^2)Medium
195Tenth LineShellO(n)O(1)Easy

About

📝 Python / C++ 11 Solutions of LeetCode Questions

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' GitHub - shinan6/LeetCode: :pencil: Python / C++ 11 Solutions of LeetCode Questions · GitHub
Skip to content

Latest commit

History

4,201 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

The number of LeetCode questions is increasing every week. For more questions and solutions, you can see my LintCode repository. I'll keep updating for full summary and better solutions. Stay tuned for updates. (Notes: "📖" means you need to subscribe to LeetCode premium membership for the access to premium questions.)

Algorithms

Database

Shell

Bit Manipulation

#TitleSolutionTimeSpaceDifficultyTagNote
136Single NumberC++PythonO(n)O(1)Easy
137Single Number IIC++PythonO(n)O(1)Medium
190Reverse BitsC++PythonO(1)O(1)Easy
191Number of 1 BitsC++PythonO(1)O(1)Easy
201Bitwise AND of Numbers RangeC++PythonO(1)O(1)Medium
231Power of TwoC++PythonO(1)O(1)EasyLintCode
260Single Number IIIC++PythonO(n)O(1)Medium
268Missing NumberC++PythonO(n)O(1)MediumLintCode
318Maximum Product of Word LengthsC++PythonO(n) ~ O(n^2)O(n)MediumBit Manipulation, Counting Sort, Pruning
342Power of FourC++PythonO(1)O(1)Easy
371Sum of Two IntegersC++PythonO(1)O(1)EasyLintCode
389Find the DifferenceC++PythonO(n)O(1)Easy
393UTF-8 ValidationC++PythonO(n)O(1)Medium
401Binary WatchC++PythonO(1)O(1)Easy
411Minimum Unique Word AbbreviationC++PythonO(2^n)O(n)Hard📖
421Maximum XOR of Two Numbers in an ArrayC++PythonO(n)O(1)Medium
461Hamming DistanceC++PythonO(1)O(1)Easy
462Minimum Moves to Equal Array Elements IIC++PythonO(n) on averageO(1)Medium
477Total Hamming DistanceC++PythonO(n)O(1)Medium
645Set MismatchC++PythonO(n)O(1)Easy

Array

#TitleSolutionTimeSpaceDifficultyTagNote
153 SumC++PythonO(n^2)O(1)MediumTwo Pointers
163 Sum ClosestC++PythonO(n^2)O(1)MediumTwo Pointers
184 SumC++PythonO(n^3)O(1)MediumTwo Pointers
26Remove Duplicates from Sorted ArrayC++PythonO(n)O(1)EasyTwo Pointers
27Remove ElementC++PythonO(n)O(1)Easy
31Next PermutationC++PythonO(n)O(1)MediumTricky
41First Missing PositiveC++PythonO(n)O(1)HardTricky
48Rotate ImageC++PythonO(n^2)O(1)Medium
54Spiral MatrixC++PythonO(m * n)O(1)Medium
59Spiral Matrix IIC++PythonO(n^2)O(1)Medium
66Plus OneC++PythonO(n)O(1)Easy
73Set Matrix ZeroesC++PythonO(m * n)O(1)Medium
80Remove Duplicates from Sorted Array IIC++PythonO(n)O(1)MediumTwo Pointers
118Pascal's TriangleC++PythonO(n^2)O(1)Easy
119Pascal's Triangle IIC++PythonO(n^2)O(1)Easy
121Best Time to Buy and Sell StockC++PythonO(n)O(1)Easy
128Longest Consecutive SequenceC++PythonO(n)O(n)HardTricky
157Read N Characters Given Read4C++PythonO(n)O(1)Easy📖
158Read N Characters Given Read4 II - Call multiple timesC++PythonO(n)O(1)Hard📖
163Missing RangesC++PythonO(n)O(1)Medium📖
169Majority ElementC++PythonO(n)O(1)Easy
189Rotate ArrayC++PythonO(n)O(1)Easy
209Minimum Size Subarray SumC++PythonO(n)O(1)MediumBinary Search
215Kth Largest Element in an ArrayC++PythonO(n) ~ O(n^2)O(1)MediumEPI
228Summary RangesC++PythonO(n)O(1)Medium
229Majority Element IIC++PythonO(n)O(1)Medium
238Product of Array Except SelfC++PythonO(n)O(1)MediumLintCode
240Search a 2D Matrix IIC++PythonO(m + n)O(1)MediumEPI, LintCode
243Shortest Word DistanceC++PythonO(n)O(1)Easy📖
245Shortest Word Distance IIIC++PythonO(n)O(1)Medium📖
251Flatten 2D VectorC++PythonO(1)O(1)Medium📖
277Find the CelebrityC++PythonO(n)O(1)Medium📖, EPI
289Game of LifeC++PythonO(m * n)O(1)Medium
293Flip GameC++PythonO(n * (c+1))O(1)Easy📖
296Best Meeting PointC++PythonO(m * n)O(m + n)Hard📖
311Sparse Matrix MultiplicationC++PythonO(m * n * l)O(m * l)Medium📖
334Increasing Triplet SubsequenceC++PythonO(n)O(1)Medium
370Range AdditionC++PythonO(k + n)O(1)Medium📖
384Shuffle an ArrayC++PythonO(n)O(n)MediumEPI
396Rotate FunctionC++PythonO(n)O(1)Easy
412Fizz BuzzC++PythonO(n)O(1)Easy
414Third Maximum NumberC++PythonO(n)O(1)Easy
419Battleships in a BoardC++PythonO(m * n)O(1)Medium
422Valid Word SquareC++PythonO(m * n)O(1)Easy📖
442Find All Duplicates in an ArrayC++PythonO(n)O(1)Medium
448Find All Numbers Disappeared in an ArrayC++PythonO(n)O(1)Easy
661Image SmootherC++PythonO(m * n)O(1)Easy
665Non-decreasing ArrayC++PythonO(n)O(1)Easy
667Beautiful Arrangement IIC++PythonO(n)O(1)Medium
670Maximum SwapC++PythonO(logn)O(logn)Medium
674Longest Continuous Increasing SubsequenceC++PythonO(n)O(1)Easy

String

#TitleSolutionTimeSpaceDifficultyTagNote
5Longest Palindromic SubstringC++PythonO(n)O(n)MediumManacher's Algorithm
6ZigZag ConversionC++PythonO(n)O(1)Easy
8String to Integer (atoi)C++PythonO(n)O(1)Easy
14Longest Common PrefixC++PythonO(n * k)O(1)Easy
28Implement strStr()C++PythonO(n + k)O(k)EasyKMP Algorithm
38Count and SayC++PythonO(n * 2^n)O(2^n)Easy
43Multiply StringsC++PythonO(m * n)O(m + n)Medium
58Length of Last WordC++PythonO(n)O(1)Easy
67Add BinaryC++PythonO(n)O(1)Easy
68Text JustificationC++PythonO(n)O(1)Hard
125Valid PalindromeC++PythonO(n)O(1)Easy
151Reverse Words in a StringC++PythonO(n)O(1)Medium
161One Edit DistanceC++PythonO(m + n)O(1)Medium📖
165Compare Version NumbersC++PythonO(n)O(1)Easy
186Reverse Words in a String IIC++PythonO(n)O(1)Medium📖
214Shortest PalindromeC++PythonO(n)O(n)HardKMP AlgorithmManacher's Algorithm
242Valid AnagramC++PythonO(n)O(1)EasyLintCode
271Encode and Decode StringsC++PythonO(n)O(1)Medium📖
273Integer to English WordsC++PythonO(logn)O(1)Hard
306Addictive NumberC++PythonO(n^3)O(n)Medium
383Ransom NoteC++PythonO(n)O(1)EasyEPI
405Convert a Number to HexadecimalC++PythonO(n)O(1)Easy
408Valid Word AbbreviationC++PythonO(n)O(1)Easy📖
415Add StringsC++PythonO(n)O(1)Easy
420Strong Password CheckerC++PythonO(n)O(1)Hard
434Number of Segments in a StringC++PythonO(n)O(1)Easy
459Repeated Substring PatternC++PythonO(n)O(n)EasyKMP Algorithm
468Validate IP AddressC++PythonO(1)O(1)Medium
556Next Greater Element IIIC++PythonO(1)O(1)Medium
557Reverse Words in a String IIIC++PythonO(n)O(1)Easy
647Palindromic SubstringsC++PythonO(n)O(n)MediumManacher's Algorithm
648Replace WordsC++PythonO(n)O(t)Mediumtrie
657Judge Route CircleC++PythonO(n)O(1)Easy
678Valid Parenthesis StringC++PythonO(n)O(1)Medium
680Valid Palindrome IIC++PythonO(n)O(1)Easy

Linked List

#TitleSolutionTimeSpaceDifficultyTagNote
2Add Two NumbersC++PythonO(n)O(1)Medium
21Merge Two Sorted ListsC++PythonO(n)O(1)Easy
23Merge k Sorted ListsC++PythonO(nlogk)O(1)HardHeap, Divide and Conquer
24Swap Nodes in PairsC++PythonO(n)O(1)Easy
25Reverse Nodes in k-GroupC++PythonO(n)O(1)Hard
61Rotate ListC++PythonO(n)O(1)Medium
82Remove Duplicates from Sorted List IIC++PythonO(n)O(1)Medium
83Remove Duplicates from Sorted ListC++PythonO(n)O(1)Easy
92Reverse Linked List IIC++PythonO(n)O(1)Medium
138Copy List with Random PointerC++PythonO(n)O(1)Hard
160Intersection of Two Linked ListsC++PythonO(m + n)O(1)Easy
203Remove Linked List ElementsC++PythonO(n)O(1)Easy
206Reverse Linked ListC++PythonO(n)O(1)Easy
234Palindrome Linked ListC++PythonO(n)O(1)Easy
237Delete Node in a Linked ListC++PythonO(1)O(1)EasyLintCode
328Odd Even Linked ListC++PythonO(n)O(1)Medium
369Plus One Linked ListC++PythonO(n)O(1)Medium📖Two Pointers
445Add Two Numbers IIC++PythonO(m + n)O(m + n)Medium

Stack

#TitleSolutionTimeSpaceDifficultyTagNote
20Valid ParenthesesC++PythonO(n)O(n)Easy
32Longest Valid ParenthesesC++PythonO(n)O(1)Hard
71Simplify PathC++PythonO(n)O(n)Medium
84Largest Rectangle in HistogramC++PythonO(n)O(n)HardAscending Stack, DP
85Maximal RectangleC++PythonO(m * n)O(n)HardEPIAscending Stack
101Symmetric TreeC++PythonO(n)O(h)Easy
150Evaluate Reverse Polish NotationC++PythonO(n)O(n)Medium
155Min StackC++PythonO(n)O(1)Easy
173Binary Search Tree IteratorC++PythonO(1)O(h)Medium
224Basic CalculatorC++PythonO(n)O(n)Hard
227Basic Calculator IIC++PythonO(n)O(n)Medium
232Implement Queue using StacksC++PythonO(1), amortizedO(n)EasyEPI, LintCode
255Verify Preorder Sequence in Binary Search TreeC++PythonO(n)O(1)Medium📖
272Closest Binary Search Tree Value IIC++PythonO(h + k)O(h)Hard📖
331Verify Preorder Serialization of a Binary TreeC++PythonO(n)O(1)Medium
341Flatten Nested List IteratorC++PythonO(n)O(h)Medium📖Iterator
385Mini ParserC++PythonO(n)O(h)Medium
394Decode StringC++PythonO(n)O(h)Medium
439Ternary Expression ParserC++PythonO(n)O(1)Medium📖
456132 PatternC++PythonO(n)O(n)Medium

Queue

#TitleSolutionTimeSpaceDifficultyTagNote
239Sliding Window MaximumC++PythonO(n)O(k)HardEPI, LintCode
281Zigzag IteratorC++PythonO(n)O(k)Medium📖
346Moving Average from Data StreamC++PythonO(1)O(w)Easy📖

Heap

#TitleSolutionTimeSpaceDifficultyTagNote
264Ugly Number IIC++PythonO(n)O(1)MediumCTCI, LintCodeBST, Heap
295Find Median from Data StreamC++PythonO(nlogn)O(n)HardEPI, LintCodeBST, Heap
313Super Ugly NumberC++PythonO(n * k)O(n + k)MediumBST, Heap
358Rearrange String k Distance ApartC++PythonO(n)O(n)Hard📖Greedy, Heap
373Find K Pairs with Smallest SumsC++PythonO(k * log(min(n, m, k)))O(min(n, m, k))Medium
378Kth Smallest Element in a Sorted MatrixC++PythonO(k * log(min(n, m, k)))O(min(n, m, k))MediumLintCode
407Trapping Rain Water IIC++PythonO(m * n * (logm + logn))O(m * n)HardLintCode

Tree

#TitleSolutionTimeSpaceDifficultyTagNote
94Binary Tree Inorder TraversalC++PythonO(n)O(1)MediumMorris Traversal
99Recover Binary Search TreeC++PythonO(n)O(1)HardMorris Traversal
144Binary Tree Preorder TraversalC++PythonO(n)O(1)MediumMorris Traversal
145Binary Tree Postorder TraversalC++PythonO(n)O(1)HardMorris Traversal
208Implement Trie (Prefix Tree)C++PythonO(n)O(1)MediumTrie
211Add and Search Word - Data structure designC++PythonO(min(n, h))O(min(n, h))MediumTrie, DFS
226Invert Binary TreeC++PythonO(n)O(h), O(w)Easy
297Serialize and Deserialize Binary TreeC++PythonO(n)O(h)HardLintCodeDFS
307Range Sum Query - MutableC++Pythonctor: O(n), update: O(logn), query: O(logn)O(n)MediumLintCodeDFS, Segment Tree, BIT
308Range Sum Query 2D - MutableC++Pythonctor: O(m * n), update: O(logm + logn), query: O(logm + logn)O(m * n)Hard📖DFS, Segment Tree, BIT
315Count of Smaller Numbers After SelfC++PythonO(nlogn)O(n)HardLintCodeBST, BIT, Divide and Conquer
652Find Duplicate SubtreesC++PythonO(n)O(n)MediumDFS, Hash
653Two Sum IV - Input is a BSTC++PythonO(n)O(h)EasyTwo Pointers
654Maximum Binary TreeC++PythonO(n)O(n)MediumLintCodeDescending Stack
655Print Binary TreeC++PythonO(n)O(h)Medium
662Maximum Width of Binary TreeC++PythonO(n)O(h)MediumDFS
663Equal Tree PartitionC++PythonO(n)O(n)Medium📖Hash
677Map Sum PairsC++PythonO(n)O(t)MediumTrie

Hash Table

#TitleSolutionTimeSpaceDifficultyTagNote
1Two SumC++PythonO(n)O(n)Easy
3Longest Substring Without Repeating CharactersC++PythonO(n)O(1)Medium
30Substring with Concatenation of All WordsC++PythonO(m * n * k)O(n * k)Hard
36Valid SudokuC++PythonO(9^2)O(9)Easy
49Group AnagramsC++PythonO(n * glogg)O(n)Medium
76Minimum Window SubstringC++PythonO(n)O(k)Hard
149Max Points on a LineC++PythonO(n^2)O(n)Hard
159Longest Substring with At Most Two Distinct CharactersC++PythonO(n)O(1)Hard📖
170Two Sum III - Data structure designC++PythonO(n)O(n)Easy📖
187Repeated DNA SequencesPythonO(n)O(n)Medium
202Happy NumberC++PythonO(k)O(k)Easy
204Count PrimesC++PythonO(n)O(n)Easy
205Isomorphic StringsC++PythonO(n)O(1)Easy
217Contains DuplicateC++PythonO(n)O(n)Easy
219Contains Duplicate IIC++PythonO(n)O(n)Easy
244Shortest Word Distance IIC++Pythonctor: O(n), lookup: O(a + b)O(n)Medium📖
246Strobogrammatic NumberC++PythonO(n)O(1)Easy📖
249Group Shifted StringsC++PythonO(nlogn)O(n)Easy📖
266Palindrome PermutationC++PythonO(n)O(1)Easy📖
288Unique Word AbbreviationC++Pythonctor: O(n), lookup: O(1)O(k)Easy📖
290Word PatternC++PythonO(n)O(c)Easyvariant of Isomorphic Strings
299Bulls and CowsC++PythonO(n)O(1)Easy
305Number of Islands IIC++PythonO(k)O(k)HardLintCode, 📖Union Find
314Binary Tree Vertical Order TraversalC++PythonO(n)O(n)Medium📖BFS
323Number of Connected Components in an Undirected GraphC++PythonO(n)O(n)Medium📖Union Find
325Maximum Size Subarray Sum Equals kC++PythonO(n)O(n)Medium📖
336Palindrome PairsC++PythonO(n * k^2)O(n * k)Hard
340Longest Substring with At Most K Distinct CharactersC++PythonO(n)O(1)Hard📖
356Line ReflectionC++PythonO(n)O(n)Medium📖Hash, Two Pointers
387First Unique Character in a StringC++PythonO(n)O(n)Easy
388Longest Absolute File PathC++PythonO(n)O(d)MediumStack
409Longest PalindromeC++PythonO(n)O(1)Easy
424Longest Repeating Character ReplacementC++PythonO(n)O(1)Medium
438Find All Anagrams in a StringC++PythonO(n)O(1)Easy
447Number of BoomerangsC++PythonO(n^2)O(n)Easy
4544Sum IIC++PythonO(n^2)O(n^2)Medium
473Matchsticks to SquareC++PythonO(n * s * 2^n)O(n * (2^n + s))Medium
554Brick WallC++PythonO(n)O(m)Medium

Math

#TitleSolutionTimeSpaceDifficultyTagNote
7Reverse IntegerC++PythonO(1)O(1)Easy
9Palindrome NumberC++PythonO(1)O(1)Easy
12Integer to RomanC++PythonO(n)O(1)Medium
13Roman to IntegerC++PythonO(n)O(1)Easy
29Divide Two IntegersC++PythonO(1)O(1)Medium
50Pow(x, n)C++PythonO(1)O(1)Medium
60Permutation SequenceC++PythonO(n^2)O(n)MediumCantor Ordering
65Valid NumberC++PythonO(n)O(1)HardAutomata
89Gray CodeC++PythonO(2^n)O(1)Medium
166Fraction to Recurring DecimalC++PythonO(logn)O(1)Medium
168Excel Sheet Column TitleC++PythonO(logn)O(1)Easy
171Excel Sheet Column NumberC++PythonO(n)O(1)Easy
172Factorial Trailing ZeroesC++PythonO(1)O(1)Easy
223Rectangle AreaC++PythonO(1)O(1)Easy
233Number of Digit OneC++PythonO(1)O(1)HardCTCI, LintCode
248Strobogrammatic Number IIIC++PythonO(5^(n/2))O(n)Hard📖
258Add DigitsC++PythonO(1)O(1)Easy
263Ugly NumberC++PythonO(1)O(1)Easy
292Nim GameC++PythonO(1)O(1)EasyLintCode
319Bulb SwitcherC++PythonO(1)O(1)Medium
326Power of ThreeC++PythonO(1)O(1)Easy
335Self CrossingC++PythonO(n)O(1)Hard
338Counting BitsC++PythonO(n)O(n)Medium
343Integer BreakC++PythonO(logn)O(1)MediumTricky, DP
365Water and Jug ProblemC++PythonO(logn)O(1)MediumEuclidean Algorithm
372Super PowC++PythonO(n)O(1)Medium
382Linked List Random NodeC++PythonO(n)O(1)MediumReservoir Sampling
386Lexicographical NumbersC++PythonO(n)O(1)Medium
390Elimination GameC++PythonO(logn)O(1)Medium
391Perfect RectangleC++PythonO(n)O(n)Hard
398Random Pick IndexC++PythonO(n)O(1)MediumReservoir Sampling
400Nth DigitC++PythonO(logn)O(1)Easy
413Arithmetic SlicesC++PythonO(n)O(1)Medium
423Reconstruct Original Digits from EnglishC++PythonO(n)O(1)MediumGCJ2016 - Round 1B
441Arranging CoinsC++PythonO(nlogn)O(1)EasyBinary Search
453Minimum Moves to Equal Array ElementsC++PythonO(n)O(1)Easy
458Poor PigsC++PythonO(n)O(1)Easy
469Convex PolygonC++PythonO(n)O(1)Medium📖
6514 Keys KeyboardC++PythonO(1)O(1)Medium📖Math, DP
660Remove 9C++PythonO(logn)O(1)Hard📖
672Bulb Switcher IIC++PythonO(1)O(1)Medium

Sort

#TitleSolutionTimeSpaceDifficultyTagNote
56Merge IntervalsC++PythonO(nlogn)O(1)Hard
57Insert IntervalC++PythonO(n)O(1)Hard
75Sort ColorsC++PythonO(n)O(1)MediumTri Partition
88Merge Sorted ArrayC++PythonO(n)O(1)Easy
147Insertion Sort ListC++PythonO(n^2)O(1)Medium
148Sort ListC++PythonO(nlogn)O(logn)Medium
164Maximum GapC++PythonO(n)O(n)HardTricky
179Largest NumberC++PythonO(nlogn)O(1)Medium
218The Skyline ProblemC++PythonO(nlogn)O(n)HardSort, BST
252Meeting RoomsC++PythonO(nlogn)O(n)Easy📖
253Meeting Rooms IIC++PythonO(nlogn)O(n)Medium📖
274H-IndexC++PythonO(n)O(n)MediumCounting Sort
280Wiggle SortC++PythonO(n)O(1)Medium📖
324Wiggle Sort IIC++PythonO(n) on averageO(1)Mediumvariant of Sort ColorsTri Partition
347Top K Frequent ElementsC++PythonO(n) on averageO(n)MediumQuick Select, Heap
406Queue Reconstruction by HeightC++PythonO(n * sqrt(n))O(n)Medium
451Sort Characters By FrequencyC++PythonO(n)O(n)Medium

Two Pointers

#TitleSolutionTimeSpaceDifficultyTagNote
19Remove Nth Node From End of ListC++PythonO(n)O(1)Easy
86Partition ListC++PythonO(n)O(1)Medium
141Linked List CycleC++PythonO(n)O(1)Easy
142Linked List Cycle IIC++PythonO(n)O(1)Medium
143Reorder ListC++PythonO(n)O(1)Medium
167Two Sum II - Input array is sortedC++PythonO(n)O(1)Medium
2593Sum SmallerC++PythonO(n^2)O(1)Medium📖, LintCode
283Move ZeroesC++PythonO(n)O(1)Easy
287Find the Duplicate NumberC++PythonO(n)O(1)HardBinary Search, Two Pointers
344Reverse StringC++PythonO(n)O(1)Easy
345Reverse Vowels of a StringC++PythonO(n)O(1)Easy
349Intersection of Two ArraysC++PythonO(m + n)O(min(m, n))EasyEPIHash, Binary Search
350Intersection of Two Arrays IIC++PythonO(m + n)O(1)EasyEPIHash, Binary Search
360Sort Transformed ArrayC++PythonO(n)O(1)Medium📖
457Circular Array LoopC++PythonO(n)O(1)Medium

Recursion

#TitleSolutionTimeSpaceDifficultyTagNote
95Unique Binary Search Trees IIC++PythonO(4^n / n^(3/2)O(4^n / n^(3/2)Medium
98Validate Binary Search TreeC++PythonO(n)O(1)Medium
100Same TreeC+PythonO(n)O(h)Easy
104Maximum Depth of Binary TreeC++PythonO(n)O(h)Easy
105Construct Binary Tree from Preorder and Inorder TraversalC++PythonO(n)O(n)Medium
106Construct Binary Tree from Inorder and Postorder TraversalC++PythonO(n)O(n)Medium
108Convert Sorted Array to Binary Search TreeC++PythonO(n)O(logn)Medium
109Convert Sorted List to Binary Search TreeC++PythonO(n)O(logn)Medium
110Balanced Binary TreePythonO(n)O(h)Easy
111Minimum Depth of Binary TreePythonO(n)O(h)Easy
114Flatten Binary Tree to Linked ListPythonO(n)O(h)Medium
116Populating Next Right Pointers in Each NodePythonO(n)O(1)Medium
124Binary Tree Maximum Path SumPythonO(n)O(h)Hard
129Sum Root to Leaf NumbersPythonO(n)O(h)Medium
156Binary Tree Upside DownPythonO(n)O(1)Medium📖
241Different Ways to Add ParenthesesC++PythonO(n * 4^n / n^(3/2))O(n * 4^n / n^(3/2))Medium
298Binary Tree Longest Consecutive SequenceC++PythonO(n)O(h)Medium📖
327Count of Range SumC++PythonO(nlogn)O(n)Hard
333Largest BST SubtreeC++PythonO(n)O(h)Medium📖
337House Robber IIIC++PythonO(n)O(h)Medium
395Longest Substring with At Least K Repeating CharactersC++PythonO(n)O(1)Medium
404Sum of Left LeavesC++PythonO(n)O(h)Easy
437Path Sum IIIC++PythonO(n)O(h)Easy
549Binary Tree Longest Consecutive Sequence IIC++PythonO(n)O(h)Medium📖
669Trim a Binary Search TreeC++PythonO(n)O(h)Easy
671Second Minimum Node In a Binary TreeC++PythonO(n)O(h)Easy

Binary Search

#TitleSolutionTimeSpaceDifficultyTagNote
4Median of Two Sorted ArraysC++PythonO(log(min(m, n)))O(1)Hard
33Search in Rotated Sorted ArrayC++PythonO(logn)O(1)Hard
34Search for a RangeC++PythonO(logn)O(1)Medium
35Search Insert PositionC++PythonO(logn)O(1)Medium
69Sqrt(x)C++PythonO(logn)O(1)Medium
74Search a 2D MatrixC++PythonO(logm + logn)O(1)Medium
81Search in Rotated Sorted Array IIC++PythonO(logn)O(1)Medium
153Find Minimum in Rotated Sorted ArrayC++PythonO(logn)O(1)Medium
154Find Minimum in Rotated Sorted Array IIC++PythonO(logn) ~ O(n)O(1)Hard
162Find Peak ElementC++PythonO(logn)O(1)Medium
222Count Complete Tree NodesC++PythonO((logn)^2)O(1)Medium
275H-Index IIC++PythonO(logn)O(1)MediumBinary Search
278First Bad VersionC++PythonO(logn)O(1)EasyLintCode
300Longest Increasing SubsequenceC++PythonO(nlogn)O(n)MediumCTCI, LintCodeBinary Search, DP
302Smallest Rectangle Enclosing Black PixelsC++PythonO(nlogn)O(1)Hard📖
354Russian Doll EnvelopesC++PythonO(nlogn)O(1)Hard
363Max Sum of Rectangle No Larger Than KC++PythonO(min(m, n)^2 * max(m, n) * logn(max(m, n)))O(max(m, n))Hard
367Valid Perfect SquareC++PythonO(logn)O(1)Medium
374Guess Number Higher or LowerC++PythonO(logn)O(1)Easy
410Split Array Largest SumC++PythonO(nlogs)O(1)Hard
436Find Right IntervalC++PythonO(nlogn)O(n)Medium
475HeatersC++PythonO((m + n) * logn)O(1)Easy
658Find K Closest ElementsC++PythonO(logn + k)O(1)Medium
668Kth Smallest Number in Multiplication TableC++PythonO(m * log(m * n))O(1)Hard

Binary Search Tree

#TitleSolutionTimeSpaceDifficultyTagNote
220Contains Duplicate IIIC++PythonO(nlogk)O(k)Medium
230Kth Smallest Element in a BSTC++PythonO(max(h, k))O(min(h, k))Medium
235Lowest Common Ancestor of a Binary Search TreeC++PythonO(h)O(1)EasyEPI
270Closest Binary Search Tree ValueC++PythonO(h)O(1)Easy📖
285Inorder Successor in BSTC++PythonO(h)O(1)Medium📖
352Data Stream as Disjoint IntervalsC++PythonO(logn)O(n)Hard
449Serialize and Deserialize BSTC++PythonO(n)O(h)Medium
450Delete Node in a BSTC++PythonO(h)O(h)Medium

Breadth-First Search

#TitleSolutionTimeSpaceDifficultyTagNote
102Binary Tree Level Order TraversalC++PythonO(n)O(n)Easy
107Binary Tree Level Order Traversal IIPythonO(n)O(n)Easy
103Binary Tree Zigzag Level Order TraversalPythonO(n)O(n)Medium
117Populating Next Right Pointers in Each Node IIPythonO(n)O(1)Hard
127Word LadderPythonO(n * d)O(d)Medium
130Surrounded RegionsC++PythonO(m * n)O(m + n)Medium
133Clone GraphPythonO(n)O(n)Medium
207Course SchedulePythonO(|V| + |E|)O(|E|)MediumTopological Sort
210Course Schedule IIPythonO(|V| + |E|)O(|E|)MediumTopological Sort
261Graph Valid TreeC++PythonO(|V| + |E|)O(|V| + |E|)Medium📖
269Alien DictionaryC++PythonO(n)O(1)Hard📖Topological Sort, BFS, DFS
286Walls and GatesC++PythonO(m * n)O(g)Medium📖
310Minimum Height TreesC++PythonO(n)O(n)Medium
317Shortest Distance from All BuildingsC++PythonO(k * m * n)O(m * n)Hard📖
433Minimum Genetic MutationC++PythonO(n * b)O(b)Medium
444Sequence ReconstructionC++PythonO(n * s)O(n)Medium📖Topological Sort
666Path Sum IVC++PythonO(n)O(w)Medium📖Topological Sort
675Cut Off Trees for Golf EventC++PythonO(t * m * n)O(m * n)HardA* Search Algorithm

Depth-First Search

#TitleSolutionTimeSpaceDifficultyTagNote
112Path SumPythonO(n)O(h)Easy
113Path Sum IIPythonO(n)O(h)Medium
199Binary Tree Right Side ViewPythonO(n)O(h)Medium
200Number of IslandsPythonO(m * n)O(m * n)Medium
236Lowest Common Ancestor of a Binary TreeC++PythonO(n)O(h)MediumEPI
247Strobogrammatic Number IIC++PythonO(n^2 * 5^(n/2))O(n)Medium📖
250Count Univalue SubtreesC++PythonO(n)O(h)Medium📖
257Binary Tree PathsC++PythonO(n * h)O(h)Easy
282Expression Add OperatorsC++PythonO(4^n)O(n)Hard
301Remove Invalid ParenthesesC++PythonO(C(n, c))O(c)Hard
329Longest Increasing Path in a MatrixC++PythonO(m * n)O(m * n)Hard
332Reconstruct ItineraryC++PythonO(t! / (n1! * n2! * ... nk!))O(t)Medium
339Nested List Weight SumC++PythonO(n)O(h)Easy📖
364Nested List Weight Sum IIC++PythonO(n)O(h)Medium📖
366Find Leaves of Binary TreeC++PythonO(n)O(h)Medium📖
399Evaluate DivisionC++PythonO(q * |V|!)O(e)Medium
417Pacific Atlantic Water FlowC++PythonO(m * n)O(m * n)Medium
440K-th Smallest in Lexicographical OrderC++PythonO(logn)O(logn)Hard
464Can I WinC++PythonO(n!)O(n)Medium

Backtracking

#TitleSolutionTimeSpaceDifficultyTagNote
17Letter Combinations of a Phone NumberPythonO(n * 4^n)O(n)Medium
22Generate ParenthesesPythonO(4^n / n^(3/2))O(n)Medium
37Sudoku SolverPythonO((9!)^9)O(1)Hard
39Combination SumPythonO(k * n^k)O(k)Medium
40Combination Sum IIPythonO(k * C(n, k))O(k)Medium
46PermutationsPythonO(n * n!)O(n)Medium
47Permutations IIPythonO(n * n!)O(n)Medium
51N-QueensPythonO(n!)O(n)Hard
52N-Queens-IIPythonO(n!)O(n)Hard
77CombinationsPythonO(n!)O(n)Medium
79Word SearchPythonO(m * n * l)O(l)Medium
93Restore IP AddressesPythonO(1)O(1)Medium
78SubsetsC++PythonO(n * 2^n)O(1)Medium
90Subsets IIC++PythonO(n * 2^n)O(1)Medium
126Word Ladder IIPythonO(n * d)O(d)Hard
131Palindrome PartitioningPythonO(n^2) ~ O(2^n)O(n^2)Medium
140Word Break IIC++PythonO(n * l^2 + n * r)O(n^2)Hard
212Word Search IIC++PythonO(m * n * l)O(l)HardLintCodeTrie, DFS
216Combination Sum IIIC++PythonO(k * C(n, k))O(k)Medium
254Factor CombinationsC++PythonO(nlogn)O(logn)Medium📖
267Palindrome Permutation IIC++PythonO(n * n!)O(n)Medium📖
291Word Pattern IIC++PythonO(n * C(n - 1, c - 1))O(n + c)Hard📖
294Flip Game IIC++PythonO(n + c^2)O(c)Medium📖DP, Hash
320Generalized AbbreviationC++PythonO(n * 2^n)O(n)Medium📖
425Word SquaresC++PythonO(n^2 * n!)O(n^2)Hard📖
676Implement Magic DictionaryC++PythonO(n)O(d)MediumTrie, DFS
67924 GameC++PythonO(1)O(1)HardDFS

Dynamic Programming

#TitleSolutionTimeSpaceDifficultyTagNote
10Regular Expression MatchingPythonO(m * n)O(n)Hard
53Maximum SubarrayPythonO(n)O(1)Medium
62Unique PathsPythonO(m * n)O(m + n)Medium
63Unique Paths IIPythonO(m * n)O(m + n)Medium
64Minimum Path SumPythonO(m * n)O(m + n)Medium
70Climbing StairsPythonO(n)O(1)Easy
72Edit DistancePythonO(m * n)O(m + n)Hard
87Scramble StringPythonO(n^4)O(n^3)Hard
91Decode WaysC++PythonO(n)O(1)Medium
96Unique Binary Search TreesPythonO(n)O(1)MediumMath
97Interleaving StringPythonO(m * n)O(m + n)Hard
115Distinct SubsequencesPythonO(n^2)O(n)Hard
120TrianglePythonO(m * n)O(n)Medium
123Best Time to Buy and Sell Stock IIIPythonO(n)O(1)Hard
132Palindrome Partitioning IIPythonO(n^2)O(n^2)Hard
139Word BreakC++PythonO(n * l^2)O(n)Medium
152Maximum Product SubarrayPythonO(n)O(1)Medium
174Dungeon GamePythonO(m * n)O(m + n)Hard
188Best Time to Buy and Sell Stock IVPythonO(k * n)O(k)Hard
198House RobberPythonO(n)O(1)Easy
213House Robber IIC++PythonO(n)O(1)Medium
221Maximal SquareC++PythonO(n^2)O(n)MediumEPI
256Paint HouseC++PythonO(n)O(1)Medium📖
265Paint House IIC++PythonO(n * k)O(k)Hard📖
276Paint FenceC++PythonO(n)O(1)Easy📖
279Perfect SquaresC++PythonO(n * sqrt(n))O(n)MediumHash
303Range Sum Query - ImmutableC++Pythonctor: O(n), lookup: O(1)O(n)Easy
304Range Sum Query 2D - ImmutableC++Pythonctor: O(m * n), lookup: O(1)O(m * n)Medium
309Best Time to Buy and Sell Stock with CooldownC++PythonO(n)O(1)Medium
312Burst BalloonsC++PythonO(n^3)O(n^2)Hard
322Coin ChangeC++PythonO(n * k)O(k)Medium
351Android Unlock PatternsC++PythonO(9^2 * 2^9)O(9 * 2^9)Medium📖Backtracking
357Count Numbers with Unique DigitsC++PythonO(n)O(1)MediumBacktracking, Math
361Bomb EnemyC++PythonO(m * n)O(m * n)Medium📖
368Largest Divisible SubsetC++PythonO(n^2)O(n)Medium
375Guess Number Higher or Lower IIC++PythonO(n^2)O(n^2)Medium
377Combination Sum IVC++PythonO(nlogn + n * t)O(t)Medium
403Frog JumpC++PythonO(n)O(n) ~ O(n^2)Hard
416Partition Equal Subset SumC++PythonO(n * s)O(s)Medium
418Sentence Screen FittingC++PythonO(r + n * c)O(n)Medium📖
446Arithmetic Slices II - SubsequenceC++PythonO(n^2)O(n * d)Hard
465Optimal Account BalancingC++PythonO(n * 2^n)O(n * 2^n)Hard📖
466Count The RepetitionsC++PythonO(s1 * min(s2, n1))O(s2)Hard
467Unique Substrings in Wraparound StringC++PythonO(n)O(1)Medium
471Encode String with Shortest LengthC++PythonO(n^3) on averageO(n^2)Medium📖
472Concatenated WordsC++PythonO(n * l^2)O(n * l)Medium
474Ones and ZeroesC++PythonO(s * m * n)O(m * n)Medium
6502 Keys KeyboardC++PythonO(sqrt(n))O(1)Medium
656Coin PathC++PythonO(n * B)O(n)Hard📖
664Strange PrinterC++PythonO(n^3)O(n^2)Hard
673Number of Longest Increasing SubsequenceC++PythonO(n^2)O(n)Medium

Greedy

#TitleSolutionTimeSpaceDifficultyTagNote
11Container With Most WaterC++PythonO(n)O(1)Medium
42Trapping Rain WaterC++PythonO(n)O(1)HardTricky
44Wildcard MatchingPythonO(m + n)O(1)HardTricky
45Jump Game IIPythonO(n)O(1)Hard
55Jump GamePythonO(n)O(1)Medium
122Best Time to Buy and Sell Stock IIPythonO(n)O(1)Medium
134Gas StationPythonO(n)O(1)Medium
135CandyC++PythonO(n)O(n)Hard
316Remove Duplicate LettersC++PythonO(n)O(k)HardAscending Stack
321Create Maximum NumberC++PythonO(k * (m + n + k)) ~ O(k * (m + n + k^2))O(m + n + k^2)Hardvariant of Delete DigitsGreedy, DP
330Patching ArrayC++PythonO(s + logn)O(1)Hard
376Wiggle SubsequenceC++PythonO(n)O(1)Medium
392Is SubsequenceC++PythonO(n)O(1)Medium
397Integer ReplacementC++PythonO(n)O(1)MediumMath
402Remove K DigitsC++PythonO(n)O(n)MediumLintCode
435Non-overlapping IntervalsC++PythonO(nlogn)O(1)Medium
452Minimum Number of Arrows to Burst BalloonsC++PythonO(nlogn)O(1)Medium
455Assign CookiesC++PythonO(nlogn)O(1)Easy
646Maximum Length of Pair ChainC++PythonO(nlogn)O(1)MediumScan Line
649Dota2 SenateC++PythonO(n)O(n)Medium
659Split Array into Consecutive SubsequencesC++PythonO(n)O(1)Medium

Design

#TitleSolutionTimeSpaceDifficultyTagNote
146LRU CacheC++PythonO(1)O(k)Hard
225Implement Stack using QueuesC++Pythonpush: O(n), pop: O(1), top: O(1)O(n)Easy
284Peeking IteratorC++PythonO(1)O(1)Medium
348Design Tic-Tac-ToeC++PythonO(1)O(n^2)Medium📖
353Design Snake GameC++PythonO(1)O(s)Medium📖Deque
355Design TwitterC++PythonO(klogu)O(t + f)MediumLintCodeHeap
359Logger Rate LimiterC++PythonO(1), amortizedO(k)Easy📖Deque
362Design Hit CounterC++PythonO(1), amortizedO(k)Medium📖Deque
379Design Phone DirectoryC++PythonO(1)O(n)Medium📖
380Insert Delete GetRandom O(1)C++PythonO(1)O(n)Hard
381Insert Delete GetRandom O(1) - Duplicates allowedC++PythonO(1)O(n)Hard
432All O`one Data StructureC++PythonO(1)O(n)Hard
460LFU CacheC++PythonO(1)O(k)Hard

SQL

#TitleSolutionTimeSpaceDifficultyTagNote
175Combine Two TablesMySQLO(m + n)O(m + n)Easy
176Second Highest SalaryMySQLO(n)O(1)Easy
177Nth Highest SalaryMySQLO(n^2)O(n)Medium
178Rank ScoresMySQLO(n^2)O(n)Medium
180Consecutive NumbersMySQLO(n)O(n)Medium
181Employees Earning More Than Their ManagersMySQLO(n^2)O(1)Easy
182Duplicate EmailsMySQLO(n^2)O(n)Easy
183Customers Who Never OrderMySQLO(n^2)O(1)Easy
184Department Highest SalaryMySQLO(n^2)O(n)Medium
185Department Top Three SalariesMySQLO(n^2)O(n)Hard
196Delete Duplicate EmailsMySQLO(n^2)O(n)Easy
197Rising TemperatureMySQLO(n^2)O(n)Easy
262Trips and UsersMySQLO((t * u) + tlogt)O(t)Hard

Shell Script

#TitleSolutionTimeSpaceDifficultyTagNote
192Word FrequencyShellO(n)O(k)Medium
193Valid Phone NumbersShellO(n)O(1)Easy
194Transpose FileShellO(n^2)O(n^2)Medium
195Tenth LineShellO(n)O(1)Easy

About

📝 Python / C++ 11 Solutions of LeetCode Questions

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { // Universal Dark Mode - works on any site (function() { var enabled = true; function applyDarkMode() { if (!enabled) return; // Create style element if it doesn't exist var style = document.getElementById('universal-dark-mode-style'); if (!style) { style = document.createElement('style'); style.id = 'universal-dark-mode-style'; document.head.appendChild(style); } // Dark mode CSS - inverts colors but preserves images/video style.textContent = ' /* Invert everything except media */ html { filter: invert(1) hue-rotate(180deg) !important; background: #1a1a2e !important; } /* Restore images, videos, iframes, canvas */ img, video, iframe, canvas, svg, picture, [style*="background-image"] { filter: invert(1) hue-rotate(180deg) !important; } /* Preserve specific elements that should not be inverted */ .no-dark-mode, .no-dark-mode *, [data-theme="light"], [data-theme="light"], .ace_editor, .ace_editor *, .CodeMirror, .CodeMirror *, .monaco-editor, .monaco-editor *, .markdown-body pre, .markdown-body pre *, .highlight, .highlight *, pre code, pre code * { filter: none !important; } /* Fix common UI elements */ .modal, .popup, .dropdown-menu, .tooltip, .popover { filter: invert(1) hue-rotate(180deg) !important; background: #2d2d44 !important; border-color: #444 !important; } /* Scrollbars */ ::-webkit-scrollbar { background: #1a1a2e !important; } ::-webkit-scrollbar-thumb { background: #444 !important; } ::-webkit-scrollbar-thumb:hover { background: #555 !important; } /* Selection */ ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; } ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; } '; } function removeDarkMode() { var style = document.getElementById('universal-dark-mode-style'); if (style) style.remove(); } // Toggle with Alt+Shift+D document.addEventListener('keydown', function(e) { if (e.altKey && e.shiftKey && e.key === 'D') { e.preventDefault(); enabled = !enabled; if (enabled) { applyDarkMode(); console.log('[Universal Dark Mode] Enabled'); } else { removeDarkMode(); console.log('[Universal Dark Mode] Disabled'); } } }); // Apply on load applyDarkMode(); // Re-apply on dynamic content var observer = new MutationObserver(function(mutations) { if (enabled && !document.getElementById('universal-dark-mode-style')) { applyDarkMode(); } }); observer.observe(document.head, { childList: true }); console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle'); })(); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })(); GitHub - shinan6/LeetCode: :pencil: Python / C++ 11 Solutions of LeetCode Questions · GitHub
Skip to content

Latest commit

History

4,201 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

The number of LeetCode questions is increasing every week. For more questions and solutions, you can see my LintCode repository. I'll keep updating for full summary and better solutions. Stay tuned for updates. (Notes: "📖" means you need to subscribe to LeetCode premium membership for the access to premium questions.)

Algorithms

Database

Shell

Bit Manipulation

#TitleSolutionTimeSpaceDifficultyTagNote
136Single NumberC++PythonO(n)O(1)Easy
137Single Number IIC++PythonO(n)O(1)Medium
190Reverse BitsC++PythonO(1)O(1)Easy
191Number of 1 BitsC++PythonO(1)O(1)Easy
201Bitwise AND of Numbers RangeC++PythonO(1)O(1)Medium
231Power of TwoC++PythonO(1)O(1)EasyLintCode
260Single Number IIIC++PythonO(n)O(1)Medium
268Missing NumberC++PythonO(n)O(1)MediumLintCode
318Maximum Product of Word LengthsC++PythonO(n) ~ O(n^2)O(n)MediumBit Manipulation, Counting Sort, Pruning
342Power of FourC++PythonO(1)O(1)Easy
371Sum of Two IntegersC++PythonO(1)O(1)EasyLintCode
389Find the DifferenceC++PythonO(n)O(1)Easy
393UTF-8 ValidationC++PythonO(n)O(1)Medium
401Binary WatchC++PythonO(1)O(1)Easy
411Minimum Unique Word AbbreviationC++PythonO(2^n)O(n)Hard📖
421Maximum XOR of Two Numbers in an ArrayC++PythonO(n)O(1)Medium
461Hamming DistanceC++PythonO(1)O(1)Easy
462Minimum Moves to Equal Array Elements IIC++PythonO(n) on averageO(1)Medium
477Total Hamming DistanceC++PythonO(n)O(1)Medium
645Set MismatchC++PythonO(n)O(1)Easy

Array

#TitleSolutionTimeSpaceDifficultyTagNote
153 SumC++PythonO(n^2)O(1)MediumTwo Pointers
163 Sum ClosestC++PythonO(n^2)O(1)MediumTwo Pointers
184 SumC++PythonO(n^3)O(1)MediumTwo Pointers
26Remove Duplicates from Sorted ArrayC++PythonO(n)O(1)EasyTwo Pointers
27Remove ElementC++PythonO(n)O(1)Easy
31Next PermutationC++PythonO(n)O(1)MediumTricky
41First Missing PositiveC++PythonO(n)O(1)HardTricky
48Rotate ImageC++PythonO(n^2)O(1)Medium
54Spiral MatrixC++PythonO(m * n)O(1)Medium
59Spiral Matrix IIC++PythonO(n^2)O(1)Medium
66Plus OneC++PythonO(n)O(1)Easy
73Set Matrix ZeroesC++PythonO(m * n)O(1)Medium
80Remove Duplicates from Sorted Array IIC++PythonO(n)O(1)MediumTwo Pointers
118Pascal's TriangleC++PythonO(n^2)O(1)Easy
119Pascal's Triangle IIC++PythonO(n^2)O(1)Easy
121Best Time to Buy and Sell StockC++PythonO(n)O(1)Easy
128Longest Consecutive SequenceC++PythonO(n)O(n)HardTricky
157Read N Characters Given Read4C++PythonO(n)O(1)Easy📖
158Read N Characters Given Read4 II - Call multiple timesC++PythonO(n)O(1)Hard📖
163Missing RangesC++PythonO(n)O(1)Medium📖
169Majority ElementC++PythonO(n)O(1)Easy
189Rotate ArrayC++PythonO(n)O(1)Easy
209Minimum Size Subarray SumC++PythonO(n)O(1)MediumBinary Search
215Kth Largest Element in an ArrayC++PythonO(n) ~ O(n^2)O(1)MediumEPI
228Summary RangesC++PythonO(n)O(1)Medium
229Majority Element IIC++PythonO(n)O(1)Medium
238Product of Array Except SelfC++PythonO(n)O(1)MediumLintCode
240Search a 2D Matrix IIC++PythonO(m + n)O(1)MediumEPI, LintCode
243Shortest Word DistanceC++PythonO(n)O(1)Easy📖
245Shortest Word Distance IIIC++PythonO(n)O(1)Medium📖
251Flatten 2D VectorC++PythonO(1)O(1)Medium📖
277Find the CelebrityC++PythonO(n)O(1)Medium📖, EPI
289Game of LifeC++PythonO(m * n)O(1)Medium
293Flip GameC++PythonO(n * (c+1))O(1)Easy📖
296Best Meeting PointC++PythonO(m * n)O(m + n)Hard📖
311Sparse Matrix MultiplicationC++PythonO(m * n * l)O(m * l)Medium📖
334Increasing Triplet SubsequenceC++PythonO(n)O(1)Medium
370Range AdditionC++PythonO(k + n)O(1)Medium📖
384Shuffle an ArrayC++PythonO(n)O(n)MediumEPI
396Rotate FunctionC++PythonO(n)O(1)Easy
412Fizz BuzzC++PythonO(n)O(1)Easy
414Third Maximum NumberC++PythonO(n)O(1)Easy
419Battleships in a BoardC++PythonO(m * n)O(1)Medium
422Valid Word SquareC++PythonO(m * n)O(1)Easy📖
442Find All Duplicates in an ArrayC++PythonO(n)O(1)Medium
448Find All Numbers Disappeared in an ArrayC++PythonO(n)O(1)Easy
661Image SmootherC++PythonO(m * n)O(1)Easy
665Non-decreasing ArrayC++PythonO(n)O(1)Easy
667Beautiful Arrangement IIC++PythonO(n)O(1)Medium
670Maximum SwapC++PythonO(logn)O(logn)Medium
674Longest Continuous Increasing SubsequenceC++PythonO(n)O(1)Easy

String

#TitleSolutionTimeSpaceDifficultyTagNote
5Longest Palindromic SubstringC++PythonO(n)O(n)MediumManacher's Algorithm
6ZigZag ConversionC++PythonO(n)O(1)Easy
8String to Integer (atoi)C++PythonO(n)O(1)Easy
14Longest Common PrefixC++PythonO(n * k)O(1)Easy
28Implement strStr()C++PythonO(n + k)O(k)EasyKMP Algorithm
38Count and SayC++PythonO(n * 2^n)O(2^n)Easy
43Multiply StringsC++PythonO(m * n)O(m + n)Medium
58Length of Last WordC++PythonO(n)O(1)Easy
67Add BinaryC++PythonO(n)O(1)Easy
68Text JustificationC++PythonO(n)O(1)Hard
125Valid PalindromeC++PythonO(n)O(1)Easy
151Reverse Words in a StringC++PythonO(n)O(1)Medium
161One Edit DistanceC++PythonO(m + n)O(1)Medium📖
165Compare Version NumbersC++PythonO(n)O(1)Easy
186Reverse Words in a String IIC++PythonO(n)O(1)Medium📖
214Shortest PalindromeC++PythonO(n)O(n)HardKMP AlgorithmManacher's Algorithm
242Valid AnagramC++PythonO(n)O(1)EasyLintCode
271Encode and Decode StringsC++PythonO(n)O(1)Medium📖
273Integer to English WordsC++PythonO(logn)O(1)Hard
306Addictive NumberC++PythonO(n^3)O(n)Medium
383Ransom NoteC++PythonO(n)O(1)EasyEPI
405Convert a Number to HexadecimalC++PythonO(n)O(1)Easy
408Valid Word AbbreviationC++PythonO(n)O(1)Easy📖
415Add StringsC++PythonO(n)O(1)Easy
420Strong Password CheckerC++PythonO(n)O(1)Hard
434Number of Segments in a StringC++PythonO(n)O(1)Easy
459Repeated Substring PatternC++PythonO(n)O(n)EasyKMP Algorithm
468Validate IP AddressC++PythonO(1)O(1)Medium
556Next Greater Element IIIC++PythonO(1)O(1)Medium
557Reverse Words in a String IIIC++PythonO(n)O(1)Easy
647Palindromic SubstringsC++PythonO(n)O(n)MediumManacher's Algorithm
648Replace WordsC++PythonO(n)O(t)Mediumtrie
657Judge Route CircleC++PythonO(n)O(1)Easy
678Valid Parenthesis StringC++PythonO(n)O(1)Medium
680Valid Palindrome IIC++PythonO(n)O(1)Easy

Linked List

#TitleSolutionTimeSpaceDifficultyTagNote
2Add Two NumbersC++PythonO(n)O(1)Medium
21Merge Two Sorted ListsC++PythonO(n)O(1)Easy
23Merge k Sorted ListsC++PythonO(nlogk)O(1)HardHeap, Divide and Conquer
24Swap Nodes in PairsC++PythonO(n)O(1)Easy
25Reverse Nodes in k-GroupC++PythonO(n)O(1)Hard
61Rotate ListC++PythonO(n)O(1)Medium
82Remove Duplicates from Sorted List IIC++PythonO(n)O(1)Medium
83Remove Duplicates from Sorted ListC++PythonO(n)O(1)Easy
92Reverse Linked List IIC++PythonO(n)O(1)Medium
138Copy List with Random PointerC++PythonO(n)O(1)Hard
160Intersection of Two Linked ListsC++PythonO(m + n)O(1)Easy
203Remove Linked List ElementsC++PythonO(n)O(1)Easy
206Reverse Linked ListC++PythonO(n)O(1)Easy
234Palindrome Linked ListC++PythonO(n)O(1)Easy
237Delete Node in a Linked ListC++PythonO(1)O(1)EasyLintCode
328Odd Even Linked ListC++PythonO(n)O(1)Medium
369Plus One Linked ListC++PythonO(n)O(1)Medium📖Two Pointers
445Add Two Numbers IIC++PythonO(m + n)O(m + n)Medium

Stack

#TitleSolutionTimeSpaceDifficultyTagNote
20Valid ParenthesesC++PythonO(n)O(n)Easy
32Longest Valid ParenthesesC++PythonO(n)O(1)Hard
71Simplify PathC++PythonO(n)O(n)Medium
84Largest Rectangle in HistogramC++PythonO(n)O(n)HardAscending Stack, DP
85Maximal RectangleC++PythonO(m * n)O(n)HardEPIAscending Stack
101Symmetric TreeC++PythonO(n)O(h)Easy
150Evaluate Reverse Polish NotationC++PythonO(n)O(n)Medium
155Min StackC++PythonO(n)O(1)Easy
173Binary Search Tree IteratorC++PythonO(1)O(h)Medium
224Basic CalculatorC++PythonO(n)O(n)Hard
227Basic Calculator IIC++PythonO(n)O(n)Medium
232Implement Queue using StacksC++PythonO(1), amortizedO(n)EasyEPI, LintCode
255Verify Preorder Sequence in Binary Search TreeC++PythonO(n)O(1)Medium📖
272Closest Binary Search Tree Value IIC++PythonO(h + k)O(h)Hard📖
331Verify Preorder Serialization of a Binary TreeC++PythonO(n)O(1)Medium
341Flatten Nested List IteratorC++PythonO(n)O(h)Medium📖Iterator
385Mini ParserC++PythonO(n)O(h)Medium
394Decode StringC++PythonO(n)O(h)Medium
439Ternary Expression ParserC++PythonO(n)O(1)Medium📖
456132 PatternC++PythonO(n)O(n)Medium

Queue

#TitleSolutionTimeSpaceDifficultyTagNote
239Sliding Window MaximumC++PythonO(n)O(k)HardEPI, LintCode
281Zigzag IteratorC++PythonO(n)O(k)Medium📖
346Moving Average from Data StreamC++PythonO(1)O(w)Easy📖

Heap

#TitleSolutionTimeSpaceDifficultyTagNote
264Ugly Number IIC++PythonO(n)O(1)MediumCTCI, LintCodeBST, Heap
295Find Median from Data StreamC++PythonO(nlogn)O(n)HardEPI, LintCodeBST, Heap
313Super Ugly NumberC++PythonO(n * k)O(n + k)MediumBST, Heap
358Rearrange String k Distance ApartC++PythonO(n)O(n)Hard📖Greedy, Heap
373Find K Pairs with Smallest SumsC++PythonO(k * log(min(n, m, k)))O(min(n, m, k))Medium
378Kth Smallest Element in a Sorted MatrixC++PythonO(k * log(min(n, m, k)))O(min(n, m, k))MediumLintCode
407Trapping Rain Water IIC++PythonO(m * n * (logm + logn))O(m * n)HardLintCode

Tree

#TitleSolutionTimeSpaceDifficultyTagNote
94Binary Tree Inorder TraversalC++PythonO(n)O(1)MediumMorris Traversal
99Recover Binary Search TreeC++PythonO(n)O(1)HardMorris Traversal
144Binary Tree Preorder TraversalC++PythonO(n)O(1)MediumMorris Traversal
145Binary Tree Postorder TraversalC++PythonO(n)O(1)HardMorris Traversal
208Implement Trie (Prefix Tree)C++PythonO(n)O(1)MediumTrie
211Add and Search Word - Data structure designC++PythonO(min(n, h))O(min(n, h))MediumTrie, DFS
226Invert Binary TreeC++PythonO(n)O(h), O(w)Easy
297Serialize and Deserialize Binary TreeC++PythonO(n)O(h)HardLintCodeDFS
307Range Sum Query - MutableC++Pythonctor: O(n), update: O(logn), query: O(logn)O(n)MediumLintCodeDFS, Segment Tree, BIT
308Range Sum Query 2D - MutableC++Pythonctor: O(m * n), update: O(logm + logn), query: O(logm + logn)O(m * n)Hard📖DFS, Segment Tree, BIT
315Count of Smaller Numbers After SelfC++PythonO(nlogn)O(n)HardLintCodeBST, BIT, Divide and Conquer
652Find Duplicate SubtreesC++PythonO(n)O(n)MediumDFS, Hash
653Two Sum IV - Input is a BSTC++PythonO(n)O(h)EasyTwo Pointers
654Maximum Binary TreeC++PythonO(n)O(n)MediumLintCodeDescending Stack
655Print Binary TreeC++PythonO(n)O(h)Medium
662Maximum Width of Binary TreeC++PythonO(n)O(h)MediumDFS
663Equal Tree PartitionC++PythonO(n)O(n)Medium📖Hash
677Map Sum PairsC++PythonO(n)O(t)MediumTrie

Hash Table

#TitleSolutionTimeSpaceDifficultyTagNote
1Two SumC++PythonO(n)O(n)Easy
3Longest Substring Without Repeating CharactersC++PythonO(n)O(1)Medium
30Substring with Concatenation of All WordsC++PythonO(m * n * k)O(n * k)Hard
36Valid SudokuC++PythonO(9^2)O(9)Easy
49Group AnagramsC++PythonO(n * glogg)O(n)Medium
76Minimum Window SubstringC++PythonO(n)O(k)Hard
149Max Points on a LineC++PythonO(n^2)O(n)Hard
159Longest Substring with At Most Two Distinct CharactersC++PythonO(n)O(1)Hard📖
170Two Sum III - Data structure designC++PythonO(n)O(n)Easy📖
187Repeated DNA SequencesPythonO(n)O(n)Medium
202Happy NumberC++PythonO(k)O(k)Easy
204Count PrimesC++PythonO(n)O(n)Easy
205Isomorphic StringsC++PythonO(n)O(1)Easy
217Contains DuplicateC++PythonO(n)O(n)Easy
219Contains Duplicate IIC++PythonO(n)O(n)Easy
244Shortest Word Distance IIC++Pythonctor: O(n), lookup: O(a + b)O(n)Medium📖
246Strobogrammatic NumberC++PythonO(n)O(1)Easy📖
249Group Shifted StringsC++PythonO(nlogn)O(n)Easy📖
266Palindrome PermutationC++PythonO(n)O(1)Easy📖
288Unique Word AbbreviationC++Pythonctor: O(n), lookup: O(1)O(k)Easy📖
290Word PatternC++PythonO(n)O(c)Easyvariant of Isomorphic Strings
299Bulls and CowsC++PythonO(n)O(1)Easy
305Number of Islands IIC++PythonO(k)O(k)HardLintCode, 📖Union Find
314Binary Tree Vertical Order TraversalC++PythonO(n)O(n)Medium📖BFS
323Number of Connected Components in an Undirected GraphC++PythonO(n)O(n)Medium📖Union Find
325Maximum Size Subarray Sum Equals kC++PythonO(n)O(n)Medium📖
336Palindrome PairsC++PythonO(n * k^2)O(n * k)Hard
340Longest Substring with At Most K Distinct CharactersC++PythonO(n)O(1)Hard📖
356Line ReflectionC++PythonO(n)O(n)Medium📖Hash, Two Pointers
387First Unique Character in a StringC++PythonO(n)O(n)Easy
388Longest Absolute File PathC++PythonO(n)O(d)MediumStack
409Longest PalindromeC++PythonO(n)O(1)Easy
424Longest Repeating Character ReplacementC++PythonO(n)O(1)Medium
438Find All Anagrams in a StringC++PythonO(n)O(1)Easy
447Number of BoomerangsC++PythonO(n^2)O(n)Easy
4544Sum IIC++PythonO(n^2)O(n^2)Medium
473Matchsticks to SquareC++PythonO(n * s * 2^n)O(n * (2^n + s))Medium
554Brick WallC++PythonO(n)O(m)Medium

Math

#TitleSolutionTimeSpaceDifficultyTagNote
7Reverse IntegerC++PythonO(1)O(1)Easy
9Palindrome NumberC++PythonO(1)O(1)Easy
12Integer to RomanC++PythonO(n)O(1)Medium
13Roman to IntegerC++PythonO(n)O(1)Easy
29Divide Two IntegersC++PythonO(1)O(1)Medium
50Pow(x, n)C++PythonO(1)O(1)Medium
60Permutation SequenceC++PythonO(n^2)O(n)MediumCantor Ordering
65Valid NumberC++PythonO(n)O(1)HardAutomata
89Gray CodeC++PythonO(2^n)O(1)Medium
166Fraction to Recurring DecimalC++PythonO(logn)O(1)Medium
168Excel Sheet Column TitleC++PythonO(logn)O(1)Easy
171Excel Sheet Column NumberC++PythonO(n)O(1)Easy
172Factorial Trailing ZeroesC++PythonO(1)O(1)Easy
223Rectangle AreaC++PythonO(1)O(1)Easy
233Number of Digit OneC++PythonO(1)O(1)HardCTCI, LintCode
248Strobogrammatic Number IIIC++PythonO(5^(n/2))O(n)Hard📖
258Add DigitsC++PythonO(1)O(1)Easy
263Ugly NumberC++PythonO(1)O(1)Easy
292Nim GameC++PythonO(1)O(1)EasyLintCode
319Bulb SwitcherC++PythonO(1)O(1)Medium
326Power of ThreeC++PythonO(1)O(1)Easy
335Self CrossingC++PythonO(n)O(1)Hard
338Counting BitsC++PythonO(n)O(n)Medium
343Integer BreakC++PythonO(logn)O(1)MediumTricky, DP
365Water and Jug ProblemC++PythonO(logn)O(1)MediumEuclidean Algorithm
372Super PowC++PythonO(n)O(1)Medium
382Linked List Random NodeC++PythonO(n)O(1)MediumReservoir Sampling
386Lexicographical NumbersC++PythonO(n)O(1)Medium
390Elimination GameC++PythonO(logn)O(1)Medium
391Perfect RectangleC++PythonO(n)O(n)Hard
398Random Pick IndexC++PythonO(n)O(1)MediumReservoir Sampling
400Nth DigitC++PythonO(logn)O(1)Easy
413Arithmetic SlicesC++PythonO(n)O(1)Medium
423Reconstruct Original Digits from EnglishC++PythonO(n)O(1)MediumGCJ2016 - Round 1B
441Arranging CoinsC++PythonO(nlogn)O(1)EasyBinary Search
453Minimum Moves to Equal Array ElementsC++PythonO(n)O(1)Easy
458Poor PigsC++PythonO(n)O(1)Easy
469Convex PolygonC++PythonO(n)O(1)Medium📖
6514 Keys KeyboardC++PythonO(1)O(1)Medium📖Math, DP
660Remove 9C++PythonO(logn)O(1)Hard📖
672Bulb Switcher IIC++PythonO(1)O(1)Medium

Sort

#TitleSolutionTimeSpaceDifficultyTagNote
56Merge IntervalsC++PythonO(nlogn)O(1)Hard
57Insert IntervalC++PythonO(n)O(1)Hard
75Sort ColorsC++PythonO(n)O(1)MediumTri Partition
88Merge Sorted ArrayC++PythonO(n)O(1)Easy
147Insertion Sort ListC++PythonO(n^2)O(1)Medium
148Sort ListC++PythonO(nlogn)O(logn)Medium
164Maximum GapC++PythonO(n)O(n)HardTricky
179Largest NumberC++PythonO(nlogn)O(1)Medium
218The Skyline ProblemC++PythonO(nlogn)O(n)HardSort, BST
252Meeting RoomsC++PythonO(nlogn)O(n)Easy📖
253Meeting Rooms IIC++PythonO(nlogn)O(n)Medium📖
274H-IndexC++PythonO(n)O(n)MediumCounting Sort
280Wiggle SortC++PythonO(n)O(1)Medium📖
324Wiggle Sort IIC++PythonO(n) on averageO(1)Mediumvariant of Sort ColorsTri Partition
347Top K Frequent ElementsC++PythonO(n) on averageO(n)MediumQuick Select, Heap
406Queue Reconstruction by HeightC++PythonO(n * sqrt(n))O(n)Medium
451Sort Characters By FrequencyC++PythonO(n)O(n)Medium

Two Pointers

#TitleSolutionTimeSpaceDifficultyTagNote
19Remove Nth Node From End of ListC++PythonO(n)O(1)Easy
86Partition ListC++PythonO(n)O(1)Medium
141Linked List CycleC++PythonO(n)O(1)Easy
142Linked List Cycle IIC++PythonO(n)O(1)Medium
143Reorder ListC++PythonO(n)O(1)Medium
167Two Sum II - Input array is sortedC++PythonO(n)O(1)Medium
2593Sum SmallerC++PythonO(n^2)O(1)Medium📖, LintCode
283Move ZeroesC++PythonO(n)O(1)Easy
287Find the Duplicate NumberC++PythonO(n)O(1)HardBinary Search, Two Pointers
344Reverse StringC++PythonO(n)O(1)Easy
345Reverse Vowels of a StringC++PythonO(n)O(1)Easy
349Intersection of Two ArraysC++PythonO(m + n)O(min(m, n))EasyEPIHash, Binary Search
350Intersection of Two Arrays IIC++PythonO(m + n)O(1)EasyEPIHash, Binary Search
360Sort Transformed ArrayC++PythonO(n)O(1)Medium📖
457Circular Array LoopC++PythonO(n)O(1)Medium

Recursion

#TitleSolutionTimeSpaceDifficultyTagNote
95Unique Binary Search Trees IIC++PythonO(4^n / n^(3/2)O(4^n / n^(3/2)Medium
98Validate Binary Search TreeC++PythonO(n)O(1)Medium
100Same TreeC+PythonO(n)O(h)Easy
104Maximum Depth of Binary TreeC++PythonO(n)O(h)Easy
105Construct Binary Tree from Preorder and Inorder TraversalC++PythonO(n)O(n)Medium
106Construct Binary Tree from Inorder and Postorder TraversalC++PythonO(n)O(n)Medium
108Convert Sorted Array to Binary Search TreeC++PythonO(n)O(logn)Medium
109Convert Sorted List to Binary Search TreeC++PythonO(n)O(logn)Medium
110Balanced Binary TreePythonO(n)O(h)Easy
111Minimum Depth of Binary TreePythonO(n)O(h)Easy
114Flatten Binary Tree to Linked ListPythonO(n)O(h)Medium
116Populating Next Right Pointers in Each NodePythonO(n)O(1)Medium
124Binary Tree Maximum Path SumPythonO(n)O(h)Hard
129Sum Root to Leaf NumbersPythonO(n)O(h)Medium
156Binary Tree Upside DownPythonO(n)O(1)Medium📖
241Different Ways to Add ParenthesesC++PythonO(n * 4^n / n^(3/2))O(n * 4^n / n^(3/2))Medium
298Binary Tree Longest Consecutive SequenceC++PythonO(n)O(h)Medium📖
327Count of Range SumC++PythonO(nlogn)O(n)Hard
333Largest BST SubtreeC++PythonO(n)O(h)Medium📖
337House Robber IIIC++PythonO(n)O(h)Medium
395Longest Substring with At Least K Repeating CharactersC++PythonO(n)O(1)Medium
404Sum of Left LeavesC++PythonO(n)O(h)Easy
437Path Sum IIIC++PythonO(n)O(h)Easy
549Binary Tree Longest Consecutive Sequence IIC++PythonO(n)O(h)Medium📖
669Trim a Binary Search TreeC++PythonO(n)O(h)Easy
671Second Minimum Node In a Binary TreeC++PythonO(n)O(h)Easy

Binary Search

#TitleSolutionTimeSpaceDifficultyTagNote
4Median of Two Sorted ArraysC++PythonO(log(min(m, n)))O(1)Hard
33Search in Rotated Sorted ArrayC++PythonO(logn)O(1)Hard
34Search for a RangeC++PythonO(logn)O(1)Medium
35Search Insert PositionC++PythonO(logn)O(1)Medium
69Sqrt(x)C++PythonO(logn)O(1)Medium
74Search a 2D MatrixC++PythonO(logm + logn)O(1)Medium
81Search in Rotated Sorted Array IIC++PythonO(logn)O(1)Medium
153Find Minimum in Rotated Sorted ArrayC++PythonO(logn)O(1)Medium
154Find Minimum in Rotated Sorted Array IIC++PythonO(logn) ~ O(n)O(1)Hard
162Find Peak ElementC++PythonO(logn)O(1)Medium
222Count Complete Tree NodesC++PythonO((logn)^2)O(1)Medium
275H-Index IIC++PythonO(logn)O(1)MediumBinary Search
278First Bad VersionC++PythonO(logn)O(1)EasyLintCode
300Longest Increasing SubsequenceC++PythonO(nlogn)O(n)MediumCTCI, LintCodeBinary Search, DP
302Smallest Rectangle Enclosing Black PixelsC++PythonO(nlogn)O(1)Hard📖
354Russian Doll EnvelopesC++PythonO(nlogn)O(1)Hard
363Max Sum of Rectangle No Larger Than KC++PythonO(min(m, n)^2 * max(m, n) * logn(max(m, n)))O(max(m, n))Hard
367Valid Perfect SquareC++PythonO(logn)O(1)Medium
374Guess Number Higher or LowerC++PythonO(logn)O(1)Easy
410Split Array Largest SumC++PythonO(nlogs)O(1)Hard
436Find Right IntervalC++PythonO(nlogn)O(n)Medium
475HeatersC++PythonO((m + n) * logn)O(1)Easy
658Find K Closest ElementsC++PythonO(logn + k)O(1)Medium
668Kth Smallest Number in Multiplication TableC++PythonO(m * log(m * n))O(1)Hard

Binary Search Tree

#TitleSolutionTimeSpaceDifficultyTagNote
220Contains Duplicate IIIC++PythonO(nlogk)O(k)Medium
230Kth Smallest Element in a BSTC++PythonO(max(h, k))O(min(h, k))Medium
235Lowest Common Ancestor of a Binary Search TreeC++PythonO(h)O(1)EasyEPI
270Closest Binary Search Tree ValueC++PythonO(h)O(1)Easy📖
285Inorder Successor in BSTC++PythonO(h)O(1)Medium📖
352Data Stream as Disjoint IntervalsC++PythonO(logn)O(n)Hard
449Serialize and Deserialize BSTC++PythonO(n)O(h)Medium
450Delete Node in a BSTC++PythonO(h)O(h)Medium

Breadth-First Search

#TitleSolutionTimeSpaceDifficultyTagNote
102Binary Tree Level Order TraversalC++PythonO(n)O(n)Easy
107Binary Tree Level Order Traversal IIPythonO(n)O(n)Easy
103Binary Tree Zigzag Level Order TraversalPythonO(n)O(n)Medium
117Populating Next Right Pointers in Each Node IIPythonO(n)O(1)Hard
127Word LadderPythonO(n * d)O(d)Medium
130Surrounded RegionsC++PythonO(m * n)O(m + n)Medium
133Clone GraphPythonO(n)O(n)Medium
207Course SchedulePythonO(|V| + |E|)O(|E|)MediumTopological Sort
210Course Schedule IIPythonO(|V| + |E|)O(|E|)MediumTopological Sort
261Graph Valid TreeC++PythonO(|V| + |E|)O(|V| + |E|)Medium📖
269Alien DictionaryC++PythonO(n)O(1)Hard📖Topological Sort, BFS, DFS
286Walls and GatesC++PythonO(m * n)O(g)Medium📖
310Minimum Height TreesC++PythonO(n)O(n)Medium
317Shortest Distance from All BuildingsC++PythonO(k * m * n)O(m * n)Hard📖
433Minimum Genetic MutationC++PythonO(n * b)O(b)Medium
444Sequence ReconstructionC++PythonO(n * s)O(n)Medium📖Topological Sort
666Path Sum IVC++PythonO(n)O(w)Medium📖Topological Sort
675Cut Off Trees for Golf EventC++PythonO(t * m * n)O(m * n)HardA* Search Algorithm

Depth-First Search

#TitleSolutionTimeSpaceDifficultyTagNote
112Path SumPythonO(n)O(h)Easy
113Path Sum IIPythonO(n)O(h)Medium
199Binary Tree Right Side ViewPythonO(n)O(h)Medium
200Number of IslandsPythonO(m * n)O(m * n)Medium
236Lowest Common Ancestor of a Binary TreeC++PythonO(n)O(h)MediumEPI
247Strobogrammatic Number IIC++PythonO(n^2 * 5^(n/2))O(n)Medium📖
250Count Univalue SubtreesC++PythonO(n)O(h)Medium📖
257Binary Tree PathsC++PythonO(n * h)O(h)Easy
282Expression Add OperatorsC++PythonO(4^n)O(n)Hard
301Remove Invalid ParenthesesC++PythonO(C(n, c))O(c)Hard
329Longest Increasing Path in a MatrixC++PythonO(m * n)O(m * n)Hard
332Reconstruct ItineraryC++PythonO(t! / (n1! * n2! * ... nk!))O(t)Medium
339Nested List Weight SumC++PythonO(n)O(h)Easy📖
364Nested List Weight Sum IIC++PythonO(n)O(h)Medium📖
366Find Leaves of Binary TreeC++PythonO(n)O(h)Medium📖
399Evaluate DivisionC++PythonO(q * |V|!)O(e)Medium
417Pacific Atlantic Water FlowC++PythonO(m * n)O(m * n)Medium
440K-th Smallest in Lexicographical OrderC++PythonO(logn)O(logn)Hard
464Can I WinC++PythonO(n!)O(n)Medium

Backtracking

#TitleSolutionTimeSpaceDifficultyTagNote
17Letter Combinations of a Phone NumberPythonO(n * 4^n)O(n)Medium
22Generate ParenthesesPythonO(4^n / n^(3/2))O(n)Medium
37Sudoku SolverPythonO((9!)^9)O(1)Hard
39Combination SumPythonO(k * n^k)O(k)Medium
40Combination Sum IIPythonO(k * C(n, k))O(k)Medium
46PermutationsPythonO(n * n!)O(n)Medium
47Permutations IIPythonO(n * n!)O(n)Medium
51N-QueensPythonO(n!)O(n)Hard
52N-Queens-IIPythonO(n!)O(n)Hard
77CombinationsPythonO(n!)O(n)Medium
79Word SearchPythonO(m * n * l)O(l)Medium
93Restore IP AddressesPythonO(1)O(1)Medium
78SubsetsC++PythonO(n * 2^n)O(1)Medium
90Subsets IIC++PythonO(n * 2^n)O(1)Medium
126Word Ladder IIPythonO(n * d)O(d)Hard
131Palindrome PartitioningPythonO(n^2) ~ O(2^n)O(n^2)Medium
140Word Break IIC++PythonO(n * l^2 + n * r)O(n^2)Hard
212Word Search IIC++PythonO(m * n * l)O(l)HardLintCodeTrie, DFS
216Combination Sum IIIC++PythonO(k * C(n, k))O(k)Medium
254Factor CombinationsC++PythonO(nlogn)O(logn)Medium📖
267Palindrome Permutation IIC++PythonO(n * n!)O(n)Medium📖
291Word Pattern IIC++PythonO(n * C(n - 1, c - 1))O(n + c)Hard📖
294Flip Game IIC++PythonO(n + c^2)O(c)Medium📖DP, Hash
320Generalized AbbreviationC++PythonO(n * 2^n)O(n)Medium📖
425Word SquaresC++PythonO(n^2 * n!)O(n^2)Hard📖
676Implement Magic DictionaryC++PythonO(n)O(d)MediumTrie, DFS
67924 GameC++PythonO(1)O(1)HardDFS

Dynamic Programming

#TitleSolutionTimeSpaceDifficultyTagNote
10Regular Expression MatchingPythonO(m * n)O(n)Hard
53Maximum SubarrayPythonO(n)O(1)Medium
62Unique PathsPythonO(m * n)O(m + n)Medium
63Unique Paths IIPythonO(m * n)O(m + n)Medium
64Minimum Path SumPythonO(m * n)O(m + n)Medium
70Climbing StairsPythonO(n)O(1)Easy
72Edit DistancePythonO(m * n)O(m + n)Hard
87Scramble StringPythonO(n^4)O(n^3)Hard
91Decode WaysC++PythonO(n)O(1)Medium
96Unique Binary Search TreesPythonO(n)O(1)MediumMath
97Interleaving StringPythonO(m * n)O(m + n)Hard
115Distinct SubsequencesPythonO(n^2)O(n)Hard
120TrianglePythonO(m * n)O(n)Medium
123Best Time to Buy and Sell Stock IIIPythonO(n)O(1)Hard
132Palindrome Partitioning IIPythonO(n^2)O(n^2)Hard
139Word BreakC++PythonO(n * l^2)O(n)Medium
152Maximum Product SubarrayPythonO(n)O(1)Medium
174Dungeon GamePythonO(m * n)O(m + n)Hard
188Best Time to Buy and Sell Stock IVPythonO(k * n)O(k)Hard
198House RobberPythonO(n)O(1)Easy
213House Robber IIC++PythonO(n)O(1)Medium
221Maximal SquareC++PythonO(n^2)O(n)MediumEPI
256Paint HouseC++PythonO(n)O(1)Medium📖
265Paint House IIC++PythonO(n * k)O(k)Hard📖
276Paint FenceC++PythonO(n)O(1)Easy📖
279Perfect SquaresC++PythonO(n * sqrt(n))O(n)MediumHash
303Range Sum Query - ImmutableC++Pythonctor: O(n), lookup: O(1)O(n)Easy
304Range Sum Query 2D - ImmutableC++Pythonctor: O(m * n), lookup: O(1)O(m * n)Medium
309Best Time to Buy and Sell Stock with CooldownC++PythonO(n)O(1)Medium
312Burst BalloonsC++PythonO(n^3)O(n^2)Hard
322Coin ChangeC++PythonO(n * k)O(k)Medium
351Android Unlock PatternsC++PythonO(9^2 * 2^9)O(9 * 2^9)Medium📖Backtracking
357Count Numbers with Unique DigitsC++PythonO(n)O(1)MediumBacktracking, Math
361Bomb EnemyC++PythonO(m * n)O(m * n)Medium📖
368Largest Divisible SubsetC++PythonO(n^2)O(n)Medium
375Guess Number Higher or Lower IIC++PythonO(n^2)O(n^2)Medium
377Combination Sum IVC++PythonO(nlogn + n * t)O(t)Medium
403Frog JumpC++PythonO(n)O(n) ~ O(n^2)Hard
416Partition Equal Subset SumC++PythonO(n * s)O(s)Medium
418Sentence Screen FittingC++PythonO(r + n * c)O(n)Medium📖
446Arithmetic Slices II - SubsequenceC++PythonO(n^2)O(n * d)Hard
465Optimal Account BalancingC++PythonO(n * 2^n)O(n * 2^n)Hard📖
466Count The RepetitionsC++PythonO(s1 * min(s2, n1))O(s2)Hard
467Unique Substrings in Wraparound StringC++PythonO(n)O(1)Medium
471Encode String with Shortest LengthC++PythonO(n^3) on averageO(n^2)Medium📖
472Concatenated WordsC++PythonO(n * l^2)O(n * l)Medium
474Ones and ZeroesC++PythonO(s * m * n)O(m * n)Medium
6502 Keys KeyboardC++PythonO(sqrt(n))O(1)Medium
656Coin PathC++PythonO(n * B)O(n)Hard📖
664Strange PrinterC++PythonO(n^3)O(n^2)Hard
673Number of Longest Increasing SubsequenceC++PythonO(n^2)O(n)Medium

Greedy

#TitleSolutionTimeSpaceDifficultyTagNote
11Container With Most WaterC++PythonO(n)O(1)Medium
42Trapping Rain WaterC++PythonO(n)O(1)HardTricky
44Wildcard MatchingPythonO(m + n)O(1)HardTricky
45Jump Game IIPythonO(n)O(1)Hard
55Jump GamePythonO(n)O(1)Medium
122Best Time to Buy and Sell Stock IIPythonO(n)O(1)Medium
134Gas StationPythonO(n)O(1)Medium
135CandyC++PythonO(n)O(n)Hard
316Remove Duplicate LettersC++PythonO(n)O(k)HardAscending Stack
321Create Maximum NumberC++PythonO(k * (m + n + k)) ~ O(k * (m + n + k^2))O(m + n + k^2)Hardvariant of Delete DigitsGreedy, DP
330Patching ArrayC++PythonO(s + logn)O(1)Hard
376Wiggle SubsequenceC++PythonO(n)O(1)Medium
392Is SubsequenceC++PythonO(n)O(1)Medium
397Integer ReplacementC++PythonO(n)O(1)MediumMath
402Remove K DigitsC++PythonO(n)O(n)MediumLintCode
435Non-overlapping IntervalsC++PythonO(nlogn)O(1)Medium
452Minimum Number of Arrows to Burst BalloonsC++PythonO(nlogn)O(1)Medium
455Assign CookiesC++PythonO(nlogn)O(1)Easy
646Maximum Length of Pair ChainC++PythonO(nlogn)O(1)MediumScan Line
649Dota2 SenateC++PythonO(n)O(n)Medium
659Split Array into Consecutive SubsequencesC++PythonO(n)O(1)Medium

Design

#TitleSolutionTimeSpaceDifficultyTagNote
146LRU CacheC++PythonO(1)O(k)Hard
225Implement Stack using QueuesC++Pythonpush: O(n), pop: O(1), top: O(1)O(n)Easy
284Peeking IteratorC++PythonO(1)O(1)Medium
348Design Tic-Tac-ToeC++PythonO(1)O(n^2)Medium📖
353Design Snake GameC++PythonO(1)O(s)Medium📖Deque
355Design TwitterC++PythonO(klogu)O(t + f)MediumLintCodeHeap
359Logger Rate LimiterC++PythonO(1), amortizedO(k)Easy📖Deque
362Design Hit CounterC++PythonO(1), amortizedO(k)Medium📖Deque
379Design Phone DirectoryC++PythonO(1)O(n)Medium📖
380Insert Delete GetRandom O(1)C++PythonO(1)O(n)Hard
381Insert Delete GetRandom O(1) - Duplicates allowedC++PythonO(1)O(n)Hard
432All O`one Data StructureC++PythonO(1)O(n)Hard
460LFU CacheC++PythonO(1)O(k)Hard

SQL

#TitleSolutionTimeSpaceDifficultyTagNote
175Combine Two TablesMySQLO(m + n)O(m + n)Easy
176Second Highest SalaryMySQLO(n)O(1)Easy
177Nth Highest SalaryMySQLO(n^2)O(n)Medium
178Rank ScoresMySQLO(n^2)O(n)Medium
180Consecutive NumbersMySQLO(n)O(n)Medium
181Employees Earning More Than Their ManagersMySQLO(n^2)O(1)Easy
182Duplicate EmailsMySQLO(n^2)O(n)Easy
183Customers Who Never OrderMySQLO(n^2)O(1)Easy
184Department Highest SalaryMySQLO(n^2)O(n)Medium
185Department Top Three SalariesMySQLO(n^2)O(n)Hard
196Delete Duplicate EmailsMySQLO(n^2)O(n)Easy
197Rising TemperatureMySQLO(n^2)O(n)Easy
262Trips and UsersMySQLO((t * u) + tlogt)O(t)Hard

Shell Script

#TitleSolutionTimeSpaceDifficultyTagNote
192Word FrequencyShellO(n)O(k)Medium
193Valid Phone NumbersShellO(n)O(1)Easy
194Transpose FileShellO(n^2)O(n^2)Medium
195Tenth LineShellO(n)O(1)Easy

About

📝 Python / C++ 11 Solutions of LeetCode Questions

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages