Skip to content

Collections of my accepted solutions in Java for the online problems by LeetCode (https://leetcode.com)

License

Notifications You must be signed in to change notification settings

shogo54/leetcode

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

LeetCode

This is the list of my accepted LeetCode solutions in Java for more than 150 problems (as of 01/03/2020).

How to Read the List

  • The first column is the index number of each question in LeetCode.
  • The second column is the name of the problem. It is linked to each problem page in LeetCode.
  • The third column shows the problem's difficulty in LeetCode.
  • The forth column has my solutions for each problem. Each solution is linked to my Java code in this repository. The list of abbreviations in this section is provided bellow.
  • The fifth column shows the link to the JUnit test cases for the problem. "-" means I have not created test cases for the problem yet.
  • The last column is the date when I solved the problem last time.

Abbreviations

DP = Dynamic Programming
D&C = Divide and Conquer
DFS = Depth First Search
BFS = Breadth First Search

Note

* = problems that can be improved in time or space complexity.
** = problems that I should revisit in the future because I spent too long on solving it.
*** = problems that have follow-up or different approaches.
^ = problems that I like.

Log

  • Solved 150 problems (as of 01/03/2020).
  • Solved 100 problems (as of 10/29/2019).

Java Solution List

# Problem Name Difficulty Solutions Test Case Last Solved
1 Two Sum Easy Brute Force / Hash Table array 05/21/2020
2 Add Two Numbers*** Medium Recursion linkedlist 08/18/2019
3 Longest Substring Without Repeating Characters*** Medium Iteration & Set / Sliding Window (Opt) & Table string 05/06/2020
5 Longest Palindromic Substring* Medium Expand Around Center string 10/05/2019
9 Palindrome Number* Easy Recursion - 08/18/2019
11 Container With Most Water** Medium Two Pointers array 11/18/2019
12 Integer to Roman* Medium Iteration integer 05/05/2020
13 Roman to Integer* Easy Recursion string 06/17/2019
14 Longest Common Prefix*** Easy Vertical Scanning / Horizontal Scanning array 05/29/2020
17 Letter Combinations of a Phone Number* Medium Recursion string 11/07/2019
19 Remove Nth Node From End of List**^ Medium Recursion linkedlist 08/20/2019
20 Valid Parentheses* Easy Recursion / Stack / Stack & Map string 05/05/2020
21 Merge Two Sorted Lists** Easy Recursion linkedlist 08/20/2019
22 Generate Parentheses* Medium DP (Bottom up) integer 11/28/2019
23 Merge k Sorted Lists*** Hard D&C linkedlist 08/27/2019
24 Swap Nodes in Pairs*** Medium Recursion linkedlist 05/12/2020
26 Remove Duplicates from Sorted Array** Easy Two Pointers / For Each array 05/19/2020
27 Remove Element*** Easy Two Pointers array 06/04/2020
28 Implement strStr()* Easy Brute Force string 05/25/2020
31 Next Permutation* Medium Swap and Reverse array 11/28/2019
32 Longest Valid Parentheses* Hard Stack string 12/12/2019
33 Search in Rotated Sorted Array*** Medium Recursion & BS array 11/16/2019
34 Find First and Last Position of Element in Sorted Array* Medium Binary Search & Iteration array 12/02/2019
35 Search Insert Position* Easy Binary Search & Iteration array 05/04/2020
36 Valid Sudoku*** Medium Hash Table matrix 05/22/2020
38 Count and Say Easy DP (Bottom up) integer 05/29/2020
39 Combination Sum* Medium Backtracking array 08/29/2019
41 First Missing Positive***^ Hard Mark as Negatives array 11/22/2019
46 Permutations*** Medium Backtracking array 10/13/2019
47 Permutations II* Medium Backtracking & String Set array 06/21/2020
48 Rotate Image*** Medium Layer by Layer / Two Reverses matrix 05/24/2020
49 Group Anagrams* Medium Hash Map & Sort array 06/03/2020
53 Maximum Subarray*** Easy DP (Bottom up) array 05/30/2020
54 Spiral Matrix** Medium Recursion matrix 08/22/2019
55 Jump Game**^ Medium Greedy array 08/23/2019
56 Merge Intervals** Medium Recursion interval 08/13/2019
57 Insert Interval* Hard Iteration interval 12/11/2019
61 Rotate List* Medium Recursion linkedlist 06/19/2020
62 Unique Paths** Medium -
64 Minimum Path Sum* Medium A* matrix 11/29/2019
66 Plus One*** Easy Iteration array 05/21/2020
70 Climbing Stairs Easy DP (Bottom up) integer 08/14/2019
73 Set Matrix Zeroes*** Medium O(M+N) space matrix 08/13/2019
75 Sort Colors* Medium Iteration Two Pass array 10/31/2019
76 Minimum Window Substring* Hard Sliding Window string 09/24/2019
77 Combinations*** Medium Backtracking / Backtracking (Opt) integer 01/12/2020
78 Subsets*** Medium Iteration array 10/10/2019
79 Word Search* Medium DFS matrix 08/22/2019
80 Remove Duplicates from Sorted Array II* Medium Iteration array 05/14/2020
88 Merge Sorted Array* Easy Iteration array 11/03/2019
91 Decode Ways* Medium D&C string 09/28/2019
92 Reverse Linked List II** Medium Recursion linkedlist 08/18/2019
94 Binary Tree Inorder Traversal*** Medium Recursion binarytree 08/02/2019
96 Unique Binary Search Trees*** Medium DP (Top down) integer 01/13/2020
98 Validate Binary Search Tree* Medium Inorder binarytree 08/18/2019
99 Recover Binary Search Tree* Hard Inorder & List binarytree 05/09/2020
100 Same Tree Easy Recursion binarytree 08/13/2019
101 Symmetric Tree** Easy Recursion binarytree 11/04/2019
102 Binary Tree Level Order Traversal*** Medium Recursion binarytree 08/18/2019
103 Binary Tree Zigzag Level Order Traversal** Medium BFS and Reverse binarytree 11/11/2019
104 Maximum Depth of Binary Tree Easy Recursion / Queue & Iteration binarytree 12/26/2019
105 Construct Binary Tree from Preorder and Inorder Traversal** Medium Recursion / Recursion (Opt) binarytree 10/17/2019
107 Binary Tree Level Order Traversal II*** Easy DFS & Recursion binarytree 07/10/2020
108 Convert Sorted Array to Binary Search Tree^ Easy Recursion binarytree 06/17/2019
110 Balanced Binary Tree*** Easy Recursion binarytree 06/26/2020
112 Path Sum* Easy DFS & Recursion binarytree 07/05/2020
114 Flatten Binary Tree to Linked List** Medium Recursion binarytree 12/05/2019
116 Populating Next Right Pointers in Each Node* Medium Stack - 11/16/2019
118 Pascal's Triangle*** Easy DP (Bottom up) - 10/29/2019
120 Triangle* Medium DP (Bottom up) array 06/29/2020
121 Best Time to Buy and Sell Stock* Easy Iteration Two Pass array 10/15/2019
122 Best Time to Buy and Sell Stock II* Easy Iteration (Peak Valley) / Iteration (One Pass) array 05/19/2020
124 Binary Tree Maximum Path Sum*** Hard Recursion binarytree 10/07/2019
125 Valid Palindrome*** Easy 2 Pointers / 2 Pointers (Opt) string 05/08/2020
127 Word Ladder* Medium BFS string 11/09/2019
128 Longest Consecutive Sequence* Hard Hash Map with Class array 09/23/2019
130 Surrounded Regions** Medium DFS From Edges matrix 12/17/2019
133 Clone Graph Medium -
136 Single Number* Easy Set / Set & Math array 05/20/2020
138 Copy List with Random Pointer* Medium Recursion & Map - 11/23/2019
141 Linked List Cycle*** Easy Recursion / Floyd's Cycle Detection linkedlist 10/01/2019
142 Linked List Cycle II** Medium Floyd's Cycle Detection linkedlist 12/02/2019
143 Reorder List* Medium Recursion linkedlist 08/22/2019
144 Binary Tree Preorder Traversal*** Medium Recursion binarytree 01/26/2020
145 Binary Tree Postorder Traversal*** Hard Recursion binarytree 06/11/2019
148 Sort List* Medium Quick Sort linkedlist 12/24/2019
150 Evaluate Reverse Polish Notation*** Medium Stack array 12/16/2019
152 Maximum Product Subarray** Medium D&C array 09/27/2019
153 Find Minimum in Rotated Sorted Array*** Medium Recursion array 08/28/2019
155 Min Stack*** Easy Two Node Pointers - 12/04/2019
160 Intersection of Two Linked Lists*** Easy Recursion with Count linkedlist 12/03/2019
162 Find Peak Element** Medium Recursive Binary Search array 11/12/2019
169 Majority Element Easy -
171 Excel Sheet Column Number*** Easy Iteration string 10/08/2019
173 Binary Search Tree Iterator*** Medium Stack binarytree 01/29/2020
189 Rotate Array*** Easy 3 Reverses array 05/19/2020
198 House Robber Easy DP (Bottom up) array 07/31/2019
199 Binary Tree Right Side View* Medium BFS & Queue binarytree 07/03/2020
200 Number of Islands* Medium DFS matrix 10/04/2019
202 Happy Number* Easy Hash Set integer 11/10/2019
206 Reverse Linked List Easy Iteration / Recursion linkedlist 08/17/2019
207 Course Schedule** Medium DFS graph 08/15/2019
208 Implement Trie (Prefix Tree)*** Medium Recursion string 10/18/2019
210 Course Schedule II* Medium DFS graph 12/19/2019
211 Add and Search Word - Data structure design* Medium Trie string 10/03/2019
212 Word Search II* Hard DFS matrix 10/06/2019
213 House Robber II*** Medium DP (Bottom up) array 10/16/2019
215 Kth Largest Element in an Array*** Medium Sort array 10/27/2019
217 Contains Duplicate*** Easy Hash Set1 / Hash Set2 array 05/20/2020
226 Invert Binary Tree*** Easy Recursion binarytree 08/18/2019
227 Basic Calculator II* Medium Two Lists string 11/05/2019
230 Kth Smallest Element in a BST*** Medium Recursion binarytree 08/27/2019
234 Palindrome Linked List* Easy Recursion linkedlist 12/04/2019
235 Lowest Common Ancestor of a Binary Search Tree*** Easy Recursion binarytree 08/28/2019
237 Delete Node in a Linked List Easy Swap linkedlist 01/06/2020
238 Product of Array Except Self* Medium Left And Right Product Lists array 02/16/2020
242 Valid Anagram* Easy Sort / Hash Table string 05/24/2020
260 Single Number III* Medium -
268 Missing Number*** Easy Formula array 08/29/2019
278 First Bad Version*** Easy Binary Search integer 05/30/2020
279 Perfect Squares* Medium DP (Bottom up) integer 11/06/2019
283 Move Zeroes Easy Two Pointers / Iteration array 05/21/2020
287 Find the Duplicate Number*^ Medium Brute Force / Floyd's Cycle Detection array 10/01/2019
289 Game of Life*** Medium In-place (Opt) matrix 12/18/2019
295 Find Median from Data Stream* Hard Sort / Insertion Sort array 10/09/2019
297 Serialize and Deserialize Binary Tree* Hard Iteration binarytree 09/27/2019
300 Longest Increasing Subsequence* Medium DP (Bottom up) array 09/26/2019
315 Count of Smaller Numbers After Self*** Hard Binary Search & Iteration array 12/09/2019
322 Coin Change Medium DP (Bottom up) array 08/15/2019
328 Odd Even Linked List Medium Iteration linkedlist 08/02/2019
329 Longest Increasing Path in a Matrix*** Hard DFS matrix 12/06/2019
334 Increasing Triplet Subsequence** Medium Iteration array 01/03/2020
337 House Robber III Medium Recursion binarytree 11/30/2019
341 Flatten Nested List Iterator* Medium Stack - 12/18/2019
344 Reverse String Easy Iteration / Recursion string 05/23/2018
347 Top K Frequent Elements*** Medium Map array 08/27/2019
349 Intersection of Two Arrays Easy Two Sets array 08/19/2019
350 Intersection of Two Arrays II* Easy Map & Iteration / 2 Maps array 05/20/2020
373 Find K Pairs with Smallest Sums* Medium Dijkstra array 06/23/2020
387 First Unique Character in a String*** Easy Hash Table string 05/24/2020
389 Find the Difference* Easy Array string 02/21/2020
394 Decode String*** Medium Stack string 12/11/2019
412 Fizz Buzz* Easy Iteration integer 06/19/2020
417 Pacific Atlantic Water Flow** Medium DFS matrix 10/02/2019
424 Longest Repeating Character Replacement* Medium Sliding Window string 10/20/2019
435 Non-overlapping Intervals* Medium Greedy interval 10/14/2019
437 Path Sum III* Easy Recursion & List binarytree 11/30/2019
438 Find All Anagrams in a String* Medium Map & Iteration string 12/04/2019
445 Add Two Numbers II*** Medium Recursion linkedlist 06/14/2019
448 Find All Numbers Disappeared in an Array** Easy Mark as Negatives array 11/17/2019
461 Hamming Distance* Easy -
508 Most Frequent Subtree Sum* Medium Recursion binarytree 06/12/2019
509 Fibonacci Number Easy -
543 Diameter of Binary Tree*** Easy Recursion binarytree 11/20/2019
547 Friend Circles* Medium -
560 Subarray Sum Equals K* Medium Brute Force array 12/24/2019
572 Subtree of Another Tree*** Easy Recursion binarytree 08/29/2019
581 Shortest Unsorted Continuous Subarray*** Easy Iteration & Two Pass array 11/26/2019
599 Minimum Index Sum of Two Lists* Easy 2 Maps array 01/11/2020
617 Merge Two Binary Trees*** Easy Recursion binarytree 11/14/2019
623 Add One Row to Tree*** Medium Recursion binarytree 01/15/2020
647 Palindromic Substrings* Medium DP (Bottom up) string 10/11/2019
654 Maximum Binary Tree* Medium Recursion binarytree 09/30/2019
657 Robot Return to Origin Easy Iteration / Recursion string 08/18/2019
700 Search in a Binary Search Tree Easy Recursion binarytree 06/10/2019
701 Insert into a Binary Search Tree*** Medium Recursion binarytree 05/07/2020
814 Binary Tree Pruning*** Medium Recursion binarytree 12/20/2019
876 Middle of the Linked List*** Easy Iteration linkedlist 04/14/2019
881 Boats to Save People* Medium Sort and Greedy array 05/22/2020
905 Sort Array By Parity*** Easy Two Pointers array 04/20/2019
912 Sort An Array* Medium -
917 Reverse Only Letters* Easy Two Pointers & Iteration string 02/09/2020
938 Range Sum of BST*** Easy Recursion binarytree 10/19/2019
958 Check Completeness of a Binary Tree* Medium Recursion binarytree 01/21/2020
977 Squares of a Sorted Array* Easy Iteration array 12/19/2019
980 Unique Paths III* Hard -
1003 Check If Word Is Valid After Substitutions* Medium Recursion / Stack & Iteration string 01/01/2020
1047 Remove All Adjacent Duplicates In String* Easy Recursion string 06/12/2019
1053 Previous Permutation With One Swap* Medium -
1078 Occurrences After Bigram Easy Iteration string 06/15/2019
1110 Delete Nodes And Return Forest*** Medium Recursion binarytree 01/09/2020
1161 Maximum Level Sum of a Binary Tree*** Medium Recursion binarytree 01/24/2020
1207 Unique Number of Occurrences* Easy Map array 10/24/2019
1222 Queens That Can Attack the King Medium Set & Iteration matrix 05/16/2020
1249 Minimum Remove to Make Valid Parentheses* Medium Stack and Array string 02/22/2020
1261 Find Elements in a Contaminated Binary Tree*** Medium Recursion binarytree 01/25/2020
1295 Find Numbers with Even Number of Digits* Easy Iteration array 01/05/2019
1448 Count Good Nodes in Binary Tree*** Medium Recursion binarytree 05/22/2020

Releases

No releases published

Packages

No packages published

Languages