My Leetcode Solutions All In One

By Frank Luo

My Leetcode Solutions are here, and this post will update frequently.

All code are in Github.


Leetcode ProblemsDifficultySolutions
2. Add Two NumbersEasy2 Approaches: Iteration and Recursion
3. Longest Substring Without Repeating CharactersEasyTwo Pointers: Sliding Window with HashSet
11. Container With Most WaterMedium2 Approaches: Brute Force and Two Pointers with Image Explanation
17. Letter Combinations of a Phone NumberMedium4 Approaches: BF 4 Loops, Backtracking, BFS, Queue with Image Explaination
19. Remove Nth Node From End of ListMedium3 Approaches: Brute Force, Stack, Slow Fast Pointers with Animation
21. Merge Two Sorted ListsEasyThe Recursive Algorithm with Detailed Image Explanation
29. Divide Two IntegersMedium5 Approaches: BF use Long, BF use Int, Binary Search use Long, Binary Search use Int and Recursion
31. Next PermutationMediumTwo Pointers Solution with Detailed Explanation and Code Commented
34. Find First and Last Position of Element in Sorted ArrayMediumBinary Search Twice
36. Valid SudokuMedium2 Approaches: HashSet and Array
39. Combination SumMediumEasy Backtracking Approach: Deduplicating and Pruning
42. Trapping Rain WaterHardFrom Brute Force to DP then Two Pointers with Detail Explaination
43. Multiply StringsMediumMath Geek: Fast Fourier Transform and Number Theoretic Transform
47. Permutations IIMediumPermutation Problems: Backtracking and Pruning
50. Pow(x, n)MediumFast Power Algorithm: Binary Exponentiation
55. Jump GameMediumLeetcode Jump Game Problems Series
62. Unique PathsMedium3 Approaches: DP, Recursion, Math
69. Sqrt(x)Medium4 Approaches: Brute Force, Exponent, Binary Search and the Newton Iteration Method
70. Climbing StairsEasy9 Fibonacci Algorithms: the Most Complete Solutions All In One
71. Simplify PathMediumStack Solution with Easy Detailed Explanation
74. Search a 2D MatrixMedium6 Approaches: Brute Force, Row Search, Column Search, One Binary Search, 2D Coordinate Axis
81. Search in Rotated Sorted Array IIMediumBinary Search: The Key is Narrow the Search Interval Step by Step
88. Merge Sorted ArrayEasy3 Approaches: Sorting, Two Pointers and Reverse Two Pointers
98. Validate Binary Search TreeMedium3 Approaches: Recursion, DFS and Iteration
102. Binary Tree Level Order TraversalMediumJava Simple BFS Solution
114. Flatten Binary Tree to Linked ListMediumImage Explanation to Understand the Recursion Solution
117. Populating Next Right Pointers in Each Node IIMedium3 Approaches: BFS, BFS + LinkedList, Recursion
118. Pascal's TriangleEasyMath Intuition of Dynamic Programming: Shift One and Added
120. TriangleMediumDynamic Programming Space O(n) Solutions: Top-Down and Bottom-Up Approaches
125. Valid PalindromeEasySimple Two Pointers Solution
136. Single NumberEasyThe XOR Cheat Sheet and Bit Manipulation with Easy Detailed Explanation
Binary and N-ary Tree Traversal: 144. Binary Tree Preorder TraversalEasyTree Traversals All In One: PreOrder, InOrder and PostOrder
160. Intersection of Two Linked ListsEasy2 Approaches: HashSet and Two Pointers with Detailed Explanation
167. Two Sum II - Input Array Is SortedMedium4 Approaches: Brute Force, HashMap, Binary Search, Two Pointers
171. Excel Sheet Column NumberEasy2 Approaches: Base Conversion from High to Low and from Low to High
191. Number of 1 BitsEasy6 Approaches: Cycle, API, Divide and Conquer, Low Bit, Bit Set, Recursion
199. Binary Tree Right Side ViewMedium2 Approaches: DFS and BFS with Detailed Explanation
215. Kth Largest Element in an ArrayMedium3 Approaches: Sorting, Priority Queue, Divide and Conquer
216. Combination Sum IIIMedium2 Approaches: Backtracking and Bit Mask
219. Contains Duplicate IIEasy3 Approaches: Brute Force, HashMap and Sliding Window
240. Search a 2D Matrix IIMedium5 Approaches: Brute Force, Binary Search(Row), Binary Search(Diagonal, Row, Col), Binary Search(Global), 2D Coord Axis
242. Valid AnagramEasy3 Approaches: HashMap, Sorting and Counting
268. Missing NumberEasy4 Approaches: Sorting, Hash, XOR, Math
278. First Bad VersionEasyJava Binary Search Solution
287. Find the Duplicate NumberMedium9 Approaches:Brute Force, Count, Hash, In-place Marked, Sorting, Index Sort, Binary Search, Bit Manipulation, Fast Slow Pointers
295. Find Median from Data StreamMediumTwo Heaps with the Follow Up's Solution
300. Longest Increasing SubsequenceMedium3 Approaches: Backtrack, DP, Binary Search
316. Remove Duplicate LettersMediumThe Detailed Explanation with 3-Steps to Understand the Stack Approach
322. Coin ChangeMedium3 Approaches: DFS, BFS, DP
329. Longest Increasing Path in a MatrixHard4 Approaches: BFS, Memory + DFS, DP, Topological Sorting
334. Increasing Triplet SubsequenceMediumWhy Greedy works?
341. Flatten Nested List IteratorMedium2 Approaches: DFS and Iteration(Using Stack)
342. Power of FourEasyJust One Line Code and Detailed Explanation
344. Reverse StringEasy2 Approaches: Two Pointers and Recursion
350. Intersection of Two Arrays IIEasy2 Approaches: Sorting with Two Pointers and HashMap
372. Super PowMedium2 Approaches: Brute Force and Binary Exponentiation
377. Combination Sum IVMedium2 Approaches: Backtrack and DP with Follow Up Analysis
378. Kth Smallest Element in a Sorted MatrixMedium3 Approaches: Sorting, Merge Sort, Binary Search
380. Insert Delete GetRandom O(1)MediumData Structures: Thought Process from HashMap to HashMap + Array
399. Evaluate DivisionMedium4 Approaches: BFS, DFS, Floyd, Union Find
406. Queue Reconstruction by HeightMediumPattern of Data Pairs Problem: Sorting First then Greedy
429. N-ary Tree Level Order TraversalMediumTraverse the Tree Level by Level: Standard BFS Solution
456. 132 PatternMedium4 Approaches: BF O(n^3), BF O(n^2), TreeMap, Monotonic Stack
509. Fibonacci NumberEasy9 Fibonacci Algorithms: the Most Complete Solutions All In One with Image Explanation
617. Merge Two Binary TreesEasy4 Approaches: Recursion, Iteration, BFS and DFS
658. Find K Closest ElementsMedium4 Approaches: Two Pointers, Sorting, Priority Queue and Binary Search
680. Valid Palindrome IIEasy3 Approaches: Brute Force, Recursion and Two Pointers
692. Top K Frequent WordsMedium2 Approaches: HashMap + Sort and Priority Queue
729. My Calendar IMedium3 Approaches: Brute Force, Binary Search, Segment Tree
746. Min Cost Climbing StairsEasyFibonacci Numbers: 9 Approaches: Recursion, DP, Math, Matrix
763. Partition LabelsMediumGreedy Solution with Easy Detailed Explanation
856. Score of ParenthesesMediumThe Tricky and Clean Solution: Replace Core by 1
858. Mirror ReflectionMediumWhat If the Ray Not Reflected and Expand the Room with Image Explanation
881. Boats to Save PeopleMediumGreedy: Let the Heaviest Match the Lightest, if Not Matched, Let it Alone
895. Maximum Frequency StackHardHashMap Solution with Detailed Explanation
923. 3Sum With MultiplicityMedium5 Approaches: BF, Three Pointers, TreeMap, Count, DP
946. Validate Stack SequencesMedium4 Approaches: Stack, Array, Greedy and O(n) Time O(1) Space
947. Most Stones Removed with Same Row or ColumnMediumIt is Literally a Graph: DFS and Union Find)
991. Broken CalculatorMediumGreedy: Reverse Thinking with Binary Algorithm
1029. Two City SchedulingMediumGreedy: Sorting the Costs Array by What?
1091. Shortest Path in Binary MatrixMediumWhy Use BFS? Search Every Possible Path vs Search A Possible Path
1137. N-th Tribonacci NumberEasyAny Language Beats 100% with Time O(1) Solution
1249. Minimum Remove to Make Valid ParenthesesMediumStack Solution with Easy Detailed Explanation
1302. Deepest Leaves SumMedium2 Approaches: BFS(Level Order Travesal) and DFS(Get Max Depth, then Traversal)
1631. Path With Minimum EffortMedium 3 Approaches: BFS(Dijkstra), Binary Search, Union Find
1641. Count Sorted Vowel StringsMediumImage Explanation: Distributing N Balls into 5 Boxes, Some Boxes May Be Empty
1642. Furthest Building You Can ReachMediumPriority Queue + Greedy: Consider Ladder as a One-time Unlimited Bricks
1663. Smallest String With A Given Numeric ValueMediumGreedy O(n) Solution with Image Explaination
1721. Swapping Nodes in a Linked ListMedium3 Approaches: Swapping Values and Swapping Nodes with Image Explanation
1780. Check if Number is a Sum of Powers of ThreeEasy3 Approaches: Tricky, Recursive and Base 3 Conversion
1834. Single-Threaded CPUMediumHow to Maintain the Enqueued Tasks? Sorting the array then Priority Queue
2215. Find the Difference of Two ArraysEasyJava HashSet Solution
2475. Number of Unequal Triplets in ArrayEasyThe Fastest O(n) Solution: Math Combinations

All suggestions are welcome.
If you have any query or suggestion please comment below .
Please upvote👍 if you like💗 it. Thank you:-)

All code are in Github.

Comments (6)