- Notifications
You must be signed in to change notification settings - Fork39
interviewcoder/leetcode
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Solutions, unit tests, and code skeletons for problems fromLeetcode OJ. (in progress)
root |--- doc | |-- category.md // category for problems in leetcode | |-- problems.tsv // searchable tsv file listing problems from leetcode | |-- road.md // general things I learnt through the process |--- lib | |-- *.jar // jar file referenced in this project |--- src // solutions for problems | |-- _001_TwoSum | | |-- Practice.java // code skeleton | | |-- Solution.java | | |-- Solution*.java // other solutions for this problem | |-- ...... |--- test // unit tests | |-- _001_TwoSum | | |-- PracticeTest.java | | |-- SolutionTest.java | | |-- Solution*Test.java | |-- ......- Discussions and code reviews are more than welcome.
Solution.javaprovides OJ-accepted solution.Practice.javaprovides code skeleton for each problemSolutionTest.javaandPracticeTest.javaprovides basic unit tests to verify your solution.
P.S.:
- Please use Java 8 to compile and run
- Many solutions are from online sources, like Leetcode OJ discussion, coders' blogs, etc.
- Solution that passed unit tests does not guarantee to pass Leetcode OJ.
| # | Problem | Difficulty | Tags | Note |
|---|---|---|---|---|
| 001 | Two Sum | Medium | ArrayHash Table | O(N^2) -> O(N) + O(N) hashtable |
| 002 | Add Two Numbers | Medium | Linked ListMath | |
| 003 | Longest Substring Without Repeating Characters | Medium | Hash TableTwo PointersString | two pointers to keep window -> O(N) |
| 004 | Median of Two Sorted Arrays | Hard | Divide and ConquerArrayBinary Search | |
| 005 | Longest Palindromic Substring | Medium | String | expanding two pointers |
| 006 | ZigZag Conversion | Easy | String | |
| 007 | Reverse Integer | Easy | Math | How to monitor Stack Overflow |
| 008 | String to Integer (atoi) | Easy | MathString | |
| 009 | Palindrome Number | Easy | Math | 1) count digits; 2) get digit from number's front/back; 3) to avoid StackOverflow, only reverse half part of number |
| 010 | Regular Expression Matching | Hard | Dynamic ProgrammingBacktrackingString | finite state machine! |
| 011 | Container With Most Water | Medium | ArrayTwo Pointers | narrowing two pointers |
| 012 | Integer to Roman | Medium | MathString | |
| 013 | Roman to Integer | Easy | MathString | |
| 014 | Longest Common Prefix | Easy | String | |
| 015 | 3Sum | Medium | ArrayTwo Pointers | sorting then 2sum |
| 016 | 3Sum Closest | Medium | ArrayTwo Pointers | |
| 017 | Letter Combinations of a Phone Number | Medium | BacktrackingString | BFS: iterative V.S. DFS: backtracking |
| 018 | 4Sum | Medium | ArrayHash TableTwo Pointers | |
| 019 | Remove Nth Node From End of List | Easy | Linked ListTwo Pointers | fast-slow two pointers on Linked List |
| 020 | Valid Parentheses | Easy | StackString | stack basic, corner case |
| 021 | Merge Two Sorted Lists | Easy | Linked List | |
| 022 | Generate Parentheses | Medium | BacktrackingString | BT template |
| 023 | Merge k Sorted Lists | Hard | Divide and ConquerLinked ListHeap | |
| 024 | Swap Nodes in Pairs | Medium | Linked List | |
| 025 | Reverse Nodes in k-Group | Hard | Linked List | |
| 026 | Remove Duplicates from Sorted Array | Easy | ArrayTwo Pointers | partition techinque on array |
| 027 | Remove Element | Easy | ArrayTwo Pointers | |
| 028 | Implement strStr() | Easy | Two PointersString | |
| 029 | Divide Two Integers | Medium | MathBinary Search | ! |
| 030 | Substring with Concatenation of All Words | Hard | Hash TableTwo PointersString | |
| 031 | Next Permutation | Medium | Array | |
| 032 | Longest Valid Parentheses | Hard | Dynamic ProgrammingString | DP |
| 033 | Search in Rotated Sorted Array | Hard | ArrayBinary Search | |
| 034 | Search for a Range | Medium | ArrayBinary Search | ! one binary search to find start position, and another one find ending position |
| 035 | Search Insert Position | Medium | ArrayBinary Search | standard binary search with minor modification |
| 036 | Valid Sudoku | Easy | Hash Table | |
| 037 | Sudoku Solver | Hard | BacktrackingHash Table | |
| 038 | Count and Say | Easy | String | |
| 039 | Combination Sum | Medium | ArrayBacktracking | BT template + prune siblings |
| 040 | Combination Sum II | Medium | ArrayBacktracking | BT template + prune siblings + skip duplicate |
| 041 | First Missing Positive | Hard | Array | swap-until technique |
| 042 | Trapping Rain Water | Hard | ArrayStackTwo Pointers | !! |
| 043 | Multiply Strings | Medium | MathString | ! simulate multiplication |
| 044 | Wildcard Matching | Hard | Dynamic ProgrammingBacktrackingGreedyString | |
| 045 | Jump Game II | Hard | ArrayGreedy | |
| 046 | Permutations | Medium | Backtracking | backtrack template + select to add |
| 047 | Permutations II | Hard | Backtracking | backtrack template + select to add + skip duplicate using set |
| 048 | Rotate Image | Medium | Array | |
| 049 | Group Anagrams | Medium | Hash TableString | !! |
| 050 | Pow(x, n) | Medium | MathBinary Search | |
| 051 | N-Queens | Hard | Backtracking | |
| 052 | N-Queens II | Hard | Backtracking | |
| 053 | Maximum Subarray | Medium | Divide and ConquerArrayDynamic Programming | |
| 054 | Spiral Matrix | Medium | Array | |
| 055 | Jump Game | Medium | ArrayGreedy | |
| 056 | Merge Intervals | Hard | ArraySort | how to decide the boundary of merged interval |
| 057 | Insert Interval | Hard | ArraySort | ! enlarge to merge interval |
| 058 | Length of Last Word | Easy | String | |
| 059 | Spiral Matrix II | Medium | Array | |
| 060 | Permutation Sequence | Medium | BacktrackingMath | |
| 061 | Rotate List | Medium | Linked ListTwo Pointers | fast-slow pointers; [189] Rotate Array |
| 062 | Unique Paths | Medium | ArrayDynamic Programming | |
| 063 | Unique Paths II | Medium | ArrayDynamic Programming | |
| 064 | Minimum Path Sum | Medium | ArrayDynamic Programming | |
| 065 | Valid Number | Hard | MathString | |
| 066 | Plus One | Easy | ArrayMath | |
| 067 | Add Binary | Easy | MathString | add binary numbers |
| 068 | Text Justification | Hard | String | ! very careful string operations |
| 069 | Sqrt(x) | Medium | MathBinary Search | !binary search along y-axle |
| 070 | Climbing Stairs | Easy | Dynamic Programming | |
| 071 | Simplify Path | Medium | StackString | |
| 072 | Edit Distance | Hard | Dynamic ProgrammingString | |
| 073 | Set Matrix Zeroes | Medium | Array | |
| 074 | Search a 2D Matrix | Medium | ArrayBinary Search | 2D coordidates --> 1D index |
| 075 | Sort Colors | Medium | ArrayTwo PointersSort | |
| 076 | Minimum Window Substring | Hard | Hash TableTwo PointersString | ![substring/sublist] hashing + two pointers -> O(N) |
| 077 | Combinations | Medium | Backtracking | |
| 078 | Subsets | Medium | ArrayBacktrackingBit Manipulation | seeREADME |
| 079 | Word Search | Medium | ArrayBacktracking | |
| 080 | Remove Duplicates from Sorted Array II | Medium | ArrayTwo Pointers | from 2 to k (general) |
| 081 | Search in Rotated Sorted Array II | Medium | ArrayBinary Search | |
| 082 | Remove Duplicates from Sorted List II | Medium | Linked List | |
| 083 | Remove Duplicates from Sorted List | Easy | Linked List | |
| 084 | Largest Rectangle in Histogram | Hard | ArrayStack | |
| 085 | Maximal Rectangle | Hard | ArrayHash TableStackDynamic Programming | |
| 086 | Partition List | Medium | Linked ListTwo Pointers | |
| 087 | Scramble String | Hard | Dynamic ProgrammingString | |
| 088 | Merge Sorted Array | Easy | ArrayTwo Pointers | in-place merging -> from end to begin; two pointers on two lists |
| 089 | Gray Code | Medium | Backtracking | |
| 090 | Subsets II | Medium | ArrayBacktracking | backtrack template + skip duplicate |
| 091 | Decode Ways | Medium | Dynamic ProgrammingString | |
| 092 | Reverse Linked List II | Medium | Linked List | |
| 093 | Restore IP Addresses | Medium | BacktrackingString | |
| 094 | Binary Tree Inorder Traversal | Medium | TreeHash TableStack | |
| 095 | Unique Binary Search Trees II | Medium | TreeDynamic Programming | |
| 096 | Unique Binary Search Trees | Medium | TreeDynamic Programming | |
| 097 | Interleaving String | Hard | Dynamic ProgrammingString | |
| 098 | Validate Binary Search Tree | Medium | TreeDepth-first Search | |
| 099 | Recover Binary Search Tree | Hard | TreeDepth-first Search | |
| 100 | Same Tree | Easy | TreeDepth-first Search | |
| 101 | Symmetric Tree | Easy | TreeDepth-first Search | |
| 102 | Binary Tree Level Order Traversal | Easy | TreeBreadth-first Search | |
| 103 | Binary Tree Zigzag Level Order Traversal | Medium | TreeBreadth-first SearchStack | |
| 104 | Maximum Depth of Binary Tree | Easy | TreeDepth-first Search | |
| 105 | Construct Binary Tree from Preorder and Inorder Traversal | Medium | TreeArrayDepth-first Search | |
| 106 | Construct Binary Tree from Inorder and Postorder Traversal | Medium | TreeArrayDepth-first Search | |
| 107 | Binary Tree Level Order Traversal II | Easy | TreeBreadth-first Search | |
| 108 | Convert Sorted Array to Binary Search Tree | Medium | TreeDepth-first Search | |
| 109 | Convert Sorted List to Binary Search Tree | Medium | Depth-first SearchLinked List | |
| 110 | Balanced Binary Tree | Easy | TreeDepth-first Search | |
| 111 | Minimum Depth of Binary Tree | Easy | TreeDepth-first SearchBreadth-first Search | |
| 112 | Path Sum | Easy | TreeDepth-first Search | |
| 113 | Path Sum II | Medium | TreeDepth-first Search | |
| 114 | Flatten Binary Tree to Linked List | Medium | TreeDepth-first Search | |
| 115 | Distinct Subsequences | Hard | Dynamic ProgrammingString | |
| 116 | Populating Next Right Pointers in Each Node | Medium | TreeDepth-first Search | |
| 117 | Populating Next Right Pointers in Each Node II | Hard | TreeDepth-first Search | |
| 118 | Pascal's Triangle | Easy | Array | |
| 119 | Pascal's Triangle II | Easy | Array | |
| 120 | Triangle | Medium | ArrayDynamic Programming | |
| 121 | Best Time to Buy and Sell Stock | Medium | ArrayDynamic Programming | O(N^2) -> O(N) |
| 122 | Best Time to Buy and Sell Stock II | Medium | ArrayGreedy | |
| 123 | Best Time to Buy and Sell Stock III | Hard | ArrayDynamic Programming | |
| 124 | Binary Tree Maximum Path Sum | Hard | TreeDepth-first Search | post-order, local and global ,README |
| 125 | Valid Palindrome | Easy | Two PointersString | |
| 126 | Word Ladder II | Hard | ArrayBacktrackingBreadth-first SearchString | |
| 127 | Word Ladder | Medium | Breadth-first Search | |
| 128 | Longest Consecutive Sequence | Hard | Array | |
| 129 | Sum Root to Leaf Numbers | Medium | TreeDepth-first Search | |
| 130 | Surrounded Regions | Medium | Breadth-first Search | |
| 131 | Palindrome Partitioning | Medium | Backtracking | |
| 132 | Palindrome Partitioning II | Hard | Dynamic Programming | |
| 133 | Clone Graph | Medium | Depth-first SearchBreadth-first SearchGraph | DFS V.S.BFS |
| 134 | Gas Station | Medium | Greedy | |
| 135 | Candy | Hard | Greedy | |
| 136 | Single Number | Medium | Hash TableBit Manipulation | |
| 137 | Single Number II | Medium | Bit Manipulation | |
| 138 | Copy List with Random Pointer | Hard | Hash TableLinked List | |
| 139 | Word Break | Medium | Dynamic Programming | |
| 140 | Word Break II | Hard | Dynamic ProgrammingBacktracking | |
| 141 | Linked List Cycle | Medium | Linked ListTwo Pointers | |
| 142 | Linked List Cycle II | Medium | Linked ListTwo Pointers | |
| 143 | Reorder List | Medium | Linked List | |
| 144 | Binary Tree Preorder Traversal | Medium | TreeStack | |
| 145 | Binary Tree Postorder Traversal | Hard | TreeStack | |
| 146 | LRU Cache | Hard | Data Structure | |
| 147 | Insertion Sort List | Medium | Linked ListSort | |
| 148 | Sort List | Medium | Linked ListSort | ! fast-slow to find middle + iterative reverse + merge sort |
| 149 | Max Points on a Line | Hard | Hash TableMath | |
| 150 | Evaluate Reverse Polish Notation | Medium | Stack | |
| 151 | Reverse Words in a String | Medium | String | README |
| 152 | Maximum Product Subarray | Medium | ArrayDynamic Programming | |
| 153 | Find Minimum in Rotated Sorted Array | Medium | ArrayBinary Search | |
| 154 | Find Minimum in Rotated Sorted Array II | Hard | ArrayBinary Search | |
| 155 | Min Stack | Easy | StackData Structure | |
| 157 | Read N Characters Given Read4 | Easy | String | |
| 158 | Read N Characters Given Read4 II | Hard | String | |
| 160 | Intersection of Two Linked Lists | Easy | Linked List | two pointers on two lists; combine long and short lists into one |
| 161 | One Edit Distance | Medium | String | |
| 162 | Find Peak Element | Medium | ArrayBinary Search | |
| 163 | Missing Ranges | Medium | Array | |
| 164 | Maximum Gap | Hard | Sort | |
| 165 | Compare Version Numbers | Easy | String | |
| 166 | Fraction to Recurring Decimal | Medium | Hash TableMath | |
| 168 | Excel Sheet Column Title | Easy | Math | convert decimal to other BASE system |
| 169 | Majority Element | Easy | Divide and ConquerArrayBit Manipulation | |
| 171 | Excel Sheet Column Number | Easy | Math | convert other BASE system to decimal system |
| 172 | Factorial Trailing Zeroes | Easy | Math | count the number of certain number (e.g. 5) in n! |
| 173 | Binary Search Tree Iterator | Medium | TreeStack | |
| 174 | Dungeon Game | Hard | Dynamic ProgrammingBinary Search | |
| 179 | Largest Number | Medium | Sort | |
| 186 | Reverse words in a string II | Medium | Array | two pass + include last position trick |
| 187 | Repeated DNA Sequences | Medium | Hash TableBit Manipulation | |
| 188 | Best Time to Buy and Sell Stock IV | Hard | Dynamic Programming | |
| 189 | Rotate Array | Easy | Array | |
| 190 | Reverse Bits | Easy | Bit Manipulation | |
| 191 | Number of 1 Bits | Easy | Bit Manipulation | |
| 198 | House Robber | Easy | Dynamic Programming | |
| 199 | Binary Tree Right Side View | Medium | TreeDepth-first SearchBreadth-first Search | |
| 200 | Number of Islands | Medium | Depth-first SearchBreadth-first Search | |
| 201 | Bitwise AND of Numbers Range | Medium | Bit Manipulation | |
| 202 | Happy Number | Easy | Hash TableMath | |
| 203 | Remove Linked List Elements | Easy | Linked List | |
| 204 | Count Primes | Easy | Hash TableMath | sieve algorithm |
| 205 | Isomorphic Strings | Easy | Hash Table | |
| 206 | Reverse Linked List | Easy | Linked List | |
| 207 | Course Schedule | Medium | Depth-first SearchBreadth-first SearchGraphTopological Sort | |
| 208 | Implement Trie (Prefix Tree) | Medium | Data StructureTrie | |
| 209 | Minimum Size Subarray Sum | Medium | ArrayTwo PointersBinary Search | |
| 210 | Course Schedule II | Medium | Depth-first SearchBreadth-first SearchGraphTopological Sort | |
| 211 | Add and Search Word - Data structure design | Medium | BacktrackingData StructureTrie | |
| 212 | Word Search II | Hard | BacktrackingTrie | |
| 213 | House Robber II | Medium | Dynamic Programming | |
| 214 | Shortest Palindrome | Hard | String | |
| 215 | Kth Largest Element in an Array | Medium | Divide and ConquerHeap | max-heap using Java's Priority Queue |
| 216 | Combination Sum III | Medium | ArrayBacktracking | |
| 217 | Contains Duplicate | Easy | ArrayHash Table | |
| 218 | The Skyline Problem | Hard | Divide and ConquerHeap | |
| 219 | Contains Duplicate II | Easy | ArrayHash Table | |
| 220 | Contains Duplicate III | Medium | Binary Search Tree | |
| 221 | Maximal Square | Medium | Dynamic Programming | |
| 222 | Count Complete Tree Nodes | Medium | TreeBinary Search | |
| 223 | Rectangle Area | Easy | Math | |
| 224 | Basic Calculator | Medium | StackMath | |
| 225 | Implement Stack using Queues | Easy | StackData Structure | |
| 226 | Invert Binary Tree | Easy | Tree | |
| 227 | Basic Calculator II | Medium | String | |
| 228 | Summary Ranges | Easy | Array | |
| 229 | Majority Element II | Medium | Array | !vote algorithm |
| 230 | Kth Smallest Element in a BST | Medium | TreeBinary Search | |
| 231 | Power of Two | Easy | MathBit Manipulation | |
| 232 | Implement Queue using Stacks | Easy | StackData Structure | README |
| 233 | Number of Digit One | Medium | Math | |
| 234 | Palindrome Linked List | Easy | Linked ListTwo Pointers | |
| 235 | Lowest Common Ancestor of a Binary Search Tree | Easy | Tree | |
| 236 | Lowest Common Ancestor of a Binary Tree | Medium | Tree | README |
| 237 | Delete Node in a Linked List | Easy | Linked List | |
| 238 | Product of Array Except Self | Medium | Array | O(N^2) -> two pass O(N) |
| 239 | Sliding Window Maximum | Hard | Heap | descending queue |
| 240 | Search a 2D Matrix II | Medium | Divide and ConquerBinary Search | |
| 241 | Different Ways to Add Parentheses | Medium | Divide and Conquer | |
| 242 | Valid Anagram | Easy | Hash TableSort | |
| 246 | Strobogrammatic Number | Easy | Hash TableMath | |
| 251 | Flatten2DVector | Medium | Design | |
| 252 | Meeting Rooms | Easy | Sort | |
| 253 | Meeting Rooms II | Medium | HeapGreedySort | |
| 256 | Paint House | Medium | Dynamic Programming | |
| 257 | Binary Tree Paths | Easy | Tree | |
| 258 | Add Digits | Easy | Math | |
| 260 | Single Number III | Medium | Bit Manipulation | |
| 261 | Graph Valid Tree | Medium | Breadth-first SearchDepth-first SearchGraphUnion-Find | |
| 263 | Ugly Number | Easy | Math | |
| 264 | Ugly Number II | Medium | MathHeap | |
| 265 | Paint House | Hard | Dynamic Programming | |
| 268 | Missing Number | Medium | ArrayMathBit Manipulation | |
| 269 | Alien Dictionary | Hard | GraphTopological Sort | BFS |
| 273 | Integer to English Words | Medium | MathString | |
| 274 | H-Index | Medium | Hash TableSort | |
| 275 | H-Index II | Medium | Binary Search | |
| 278 | First Bad Version | Easy | Binary Search | |
| 279 | Perfect Squares | Medium | Dynamic ProgrammingBreadth-first SearchMath | |
| 282 | Expression Add Operators | Hard | Divide and Conquer | |
| 283 | Move Zeroes | Easy | ArrayTwo Pointers | |
| 284 | Peeking Iterator | Medium | Design | |
| 285 | Inorder Successor in BST | Medium | Tree | |
| 286 | Walls And Gates | Medium | BFS | |
| 287 | Find the Duplicate Number | Hard | ArrayTwo PointersBinary Search | |
| 289 | Game of Life | Medium | Array | |
| 290 | Word Pattern | Easy | Hash Table |
About
Leetcode solutions, code skeletons, and unit tests in Java (in progress)
Resources
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
No releases published
Packages0
No packages published