GitHub - dailiang18bb/Leetcode: My practice on Leetcode · GitHub
Skip to content

dailiang18bb/Leetcode

Folders and files

Repository files navigation

Leetcode

My practice on Leetcode
Create a index for each question that I've been solved.

Notes

  1. Ask questions
  2. Example (Datatype, max value, overflow)
  3. Edge case
  4. Algorithm (Time Complexity, Space Complexity)
  5. Code
  6. Go through example and edge case
  7. Optimization (Trade off, using different data structure)

Questions Table

# Title Tag Level Question Link Key Point Dates
724 Find Pivot Index Array Easy Algorithms 724
747 Largest Number At Least Twice of Others Array Easy Algorithms 747
001 Two Sum Array, HashTable Easy Algorithms 001 Using HashMap to trade space for time
167 Two Sum II - Input array is sorted Array, Two Pointers, Binary Search Easy Algorithms 167 Using Two pointers search
007 Reverse Integer Math Easy Algorithms 007 pop and push idea, overflow check
190 Reverse Bits Bit Manipulation Easy Algorithms 190 bit-wise opeartor
009 Palindrome Number Math Easy Algorithms 009 check half , two pointers search
344 Reverse String Two Pointers, String Easy Algorithms 344 two pointers search, in-place sort
027 Remove Element Two Pointers, Array Easy Algorithms 027 two pointers search, in-place sort 06/11/2019
412 Fizz Buzz LinkedList Easy Algorithms 412 LinkedList vs ArrayList 06/12/2019
026 Remove Duplicates from Sorted Array Array Easy Algorithms 026 sorted array two pointers(slow, fast) 06/12/2019
088 Merge Sorted Array Array Easy Algorithms 088 three pointers 06/15/2019
118 Pascal's Triangle Array Easy Algorithms 118 DP 06/16/2019
119 Pascal's Triangle II Array Easy Algorithms 119 List.set(), backward search 06/16/2019
189 Rotate Array Array Easy Algorithms 189 Cyclic Replacements/ Reverse 06/17/2019
217 Contains Duplicate Array, Hash Table Easy Algorithms 217 check duplicate use hashset 06/17/2019
219 Contains Duplicate II Array, Hash Table Easy Algorithms 219 Map put return value 06/18/2019
136 Single Number Hash Table, Bit Manipulation Easy Algorithms 136 Bit Manipulation 06/24/2019
268 Missing Number Array, Math, Bit Manipulation Easy Algorithms 268 Math ,Bit Manipulation 06/24/2019
137 Single Number II Bit Manipulation Medium Algorithms 137 Bit Manipulation 06/25/2019
202 Happy Number Hash Table, Math Easy Algorithms 202 how to break the loop 06/25/2019
204 Count Primes Hash Table, Math Easy Algorithms 204 Sieve of Eratosthenes 06/26/2019
263 Ugly Number Math Easy Algorithms 263 prime factors division 06/26/2019
205 Isomorphic Strings Hash Table Easy Algorithms 205 Integer autoboxing, caching 06/27/2019
242 Valid Anagram Hash Table, Sort Easy Algorithms 242 Array, map check equality 06/27/2019
290 Word Pattern Hash Table Easy Algorithms 290 try to put everything into one HashMap 06/28/2019
020 Valid Parentheses String, Stack Easy Algorithms 020 Stack 06/28/2019
496 Next Greater Element I Stack Easy Algorithms 496 Stack 07/02/2019
682 Baseball Game Stack Easy Algorithms 682 Stack 07/03/2019
349 Intersection of Two Arrays Hash table, Two pointer, sort Easy Algorithms 349 set contains perform O(1) 07/04/2019
599 Minimum Index Sum of Two Lists Hash table Easy Algorithms 599 list toArray() 07/05/2019
387 First Unique Character in a String Hash table, String Easy Algorithms 387 getOrDefault() 07/05/2019
350 Intersection of Two Arrays II Hash table, Two pointer, sort Easy Algorithms 350 sorted use two pointer 07/06/2019
049 Group Anagrams Hash table, String Medium Algorithms 049 anagrams, sorted char array equals 07/07/2019
144 Binary Tree Preorder Traversal Tree, Stack Medium Algorithms 144 user helper function 07/08/2019
094 Binary Tree Inorder Traversal Tree, Stack, HashTable Medium Algorithms 094 Recursive, iterative, Morris Traversal 07/10/2019
145 Binary Tree Postorder Traversal Tree, Stack Hard Algorithms 145 reverse the process of preorder traversal, using link.add(0, val) 07/11/2019
002 Add Two Numbers LinkedList, Math medium Algorithms 002 calculate numbers should care the data type 07/11/2019
104 Maximum Depth of Binary Tree Tree, DFS Easy Algorithms 104 Recursively, Top-down, Bottom-up 07/16/2019
101 Symmetric Tree Tree, DFS, BFS Easy Algorithms 101 use two node compare left and right at the same time 07/16/2019
112 Path Sum Tree, DFS Easy Algorithms 112 DPS, recursive 07/17/2019
707 Design Linked List LinkedList, Design Easy Algorithms 707 edge case 07/22/2019
141 Linked List Cycle LinkedList, Two pointers Easy Algorithms 141 fast and slow pointers detect cycle 07/22/2019
142 Linked List Cycle II LinkedList, Two pointers medium Algorithms 142 when meet move to the begining, move same length 07/23/2019
160 Intersection of Two Linked Lists LinkedList, Two pointers Easy Algorithms 160 align the list or move twice iteration 07/24/2019
019 Remove Nth Node From End of List LinkedList, Two pointers Medium Algorithms 019 add a dummy node before the head to make the head case easier 07/24/2019
206 Reverse Linked List LinkedList Easy Algorithms 206 Recursive, go to the deepest and return piece by piece 07/26/2019
203 Remove Linked List Elements LinkedList Easy Algorithms 203 Recursive, draw a map to help 07/27/2019
328 Odd Even Linked List LinkedList Medium Algorithms 328 use two list 07/27/2019
234 Palindrome Linked List LinkedList Easy Algorithms 234 Use slow and fast pointer to get the middle point 07/27/2019
622 Design Circular Queue Design, Queue Medium Algorithms 622 ArrayList.add(index, val) will shift if the target index has value, use ArrayList.set(ind,val) to solve 07/29/2019
994 Rotting Oranges BFS Easy Algorithms 994 BFS apply in a grid 07/31/2019
690 Employee Importance Hash Table, DFS, BFS Easy Algorithms 690 BFS solving inherited problem 07/31/2019
155 Min Stack Stack, Design Easy Algorithms 155 use a node which has min value 07/31/2019
739 Daily Temperatures Hash Table, Stack Medium Algorithms 739 stack store the index 08/01/2019
150 Evaluate Reverse Polish Notation Stack Medium Algorithms 150 convert between int and String 08/01/2019
200 Number of Islands DFS, BFS, Union Find Medium Algorithms 200 use a marker to achieve the search 08/01/2019
589 N-ary Tree Preorder Traversal Tree Easy Algorithms 589 Preorder use Stack 08/02/2019
590 N-ary Tree Postorder Traversal Tree Easy Algorithms 590 Reverse the Preorder list 08/02/2019
429 N-ary Tree Level Order Traversal Tree, BFS Easy Algorithms 429 BFS == Queue 08/02/2019
559 Maximum Depth of N-ary Tree Tree, BFS, DFS Easy Algorithms 559 Tree problem, using Top-down/ Bottom-up 08/02/2019
069 Sqrt(x) Math, Binary Search Easy Algorithms 069 mid = (temp + x/temp)/2 08/05/2019
374 Guess Number Higher or Lower Binary Search Easy Algorithms 374 Ternary search 08/05/2019
278 First Bad Version Binary Search Easy Algorithms 278 Binary search template 2, get the first ()number 08/06/2019
162 Find Peak Element Binary Search, Array Medium Algorithms 162 think of the whole picture 08/06/2019
034 Find First and Last Position of Element in Sorted Array Binary Search, Array Medium Algorithms 034 search two point, can use twice Binary Search 08/08/2019
098 Validate Binary Search Tree Tree, DFS Medium Algorithms 098 draw a tree map with the range, think of inorder traversal 08/11/2019
173 Binary Search Tree Iterator Stack, Tree, Design Medium Algorithms 173 don't forget the recursive way to do inorder traversal, it's easy 08/13/2019
048 Rotate Image Array Medium Algorithms 048 be careful of the index 08/18/2019
784 Letter Case Permutation Backtracking, Bit Manipulation Easy Algorithms 784 draw the tree 08/24/2019
046 Permutations Backtracking Medium Algorithms 046 use boolean array to track visited elements 08/28/2019
047 Permutations II Backtracking Medium Algorithms 047 sort first and then avoid duplicate 08/28/2019
039 Combination Sum Array, Backtracking Medium Algorithms 039 use i = startIndex to avoid duplicate 08/29/2019
040 Combination Sum II Array, Backtracking Medium Algorithms 040 sort and use i > currIndex to avoid duplicate 08/29/2019
078 Subsets Array, Backtracking, Bit Manipulation Medium Algorithms 078 dfs draw a tree 08/29/2019
013 Roman to Integer Math, String Easy Algorithms 013 warmup 11/26/2019
0028 Implement strStr() Two pointers, String Easy Algorithms 0028 Arise 03/21/2020
0035 Search Insert Position Array, Binary Search Easy Algorithms 0035 Binary search, recursive, iteritive 03/21/2020
1389 Create Target Array in the Given Order Array Easy Algorithms 1389 List.add(index, num) 03/22/2020
1390 Four Divisors Math Medium Algorithms 1390 recursive, Math.sqrt(n) 03/22/2020
0058 Length of Last Word String Easy Algorithms 0058 going backward make things easy 03/23/2020
0067 Add Binary Math, String Easy Algorithms 0067 put where using index inside the if statement 03/23/2020
0083 Remove Duplicates from Sorted List Linked List Easy Algorithms 0083 linked list think of recursion or iteration 03/24/2020
0876 Middle of the Linked List Linked List Easy Algorithms 0876 fast and slow pointer 03/24/2020
1290 Convert Binary Number in a Linked List to Integer Linked List Easy Algorithms 1290 sum = sum* 2 + node.val 03/24/2020
0445 Add Two Numbers II Linked List medium Algorithms 0445 build ListNode backward 03/24/2020
0100 Same Tree Tree, DFS Easy Algorithms 0100 DFS, recurrsion 03/25/2020
0111 Minimum Depth of Binary Tree Tree, DFS, BFS Easy Algorithms 0111 if(node.left==null or node.right==null) 03/26/2020
0257 Binary Tree Paths Tree, DFS Easy Algorithms 0257 leaf node condition 03/26/2020
0107 Binary Tree Level Order Traversal II Tree, BFS Easy Algorithms 0107 BFS warm up 03/27/2020
0103 Binary Tree Zigzag Level Order Traversal Tree, BFS Medium Algorithms 0103 BFS 03/27/2020
0637 Average of Levels in Binary Tree Tree Easy Algorithms 0637 BFS 03/27/2020
0404 Sum of Left Leaves Tree Easy Algorithms 0404 DFS, Leaves condition, return as sum 03/27/2020
0704 Binary Search Binary Search Easy Algorithms 0704 while(left <= right), return helper() 03/29/2020
0283 Move Zeroes Array, Two Pointers Easy Algorithms 0283 two pointer track non zeros 04/06/2020
0122 Best Time to Buy and Sell Stock II Array, Greedy Easy Algorithms 0122 sum all arise 04/06/2020
0053 Maximum Subarray Array, Divide and conquer, DP Easy Algorithms 0053 track best move on every step 04/06/2020
noNum Counting Elements HashSet Easy 30days coding week1-7 HashSet 04/08/2020
0062 Unique Paths Array, DP Medium Algorithms 0062 recursive, int[][] do memo 04/08/2020
0063 Unique Paths II Array, DP Medium Algorithms 0062 bottom up, build the arr 04/08/2020
0064 Minimum Path Sum Array, DP Medium Algorithms 0064 previous min value + current value 04/08/2020
0844 Backspace String Compare Two pointer, Stack Easy Algorithms 0844 backspace related to stack.pop() 04/10/2020
0198 House Robber DP Easy Algorithms 0198 currentMax = Math.max(prevPrevMax + currentValue, prevMax) 04/10/2020
noNum Perform String Shifts String Easy 30days coding week2-7 use list to make the shift 04/14/2020
0238 Product of Array Except Self Array Medium Algorithms 0238 use two arrays to tracking the product 04/15/2020
0213 House Robber II DP Medium Algorithms 0213 divided into sub problems 04/15/2020
0120 Triangle DP, Array Medium Algorithms 0120 top-down, bottom-up 04/16/2020
1413 Minimum Value to Get Positive Step by Step Sum Array Easy Algorithms 1413 find the min sum value 04/18/2020
1414 Find the Minimum Number of Fibonacci Numbers Whose Sum Is K Array, Greedy Medium Algorithms 1414 find the largest fib num x smaller than k, return helper(k - x) + 1 04/18/2020
0108 Convert Sorted Array to Binary Search Tree Tree, DFS Easy Algorithms 0108 get the mid point as root 04/19/2020
0701 Insert into a Binary Search Tree Tree Medium Algorithms 0701 like greedy, compare val with node.val everytime then move 04/19/2020
0110 Balanced Binary Tree Tree, DFS Easy Algorithms 0110 compare Max Depth of each node 04/19/2020
0113 Path Sum II Tree, DFS Medium Algorithms 0113 Top-down 04/19/2020
1008 Construct Binary Search Tree from Preorder Traversal Tree Medium Algorithms 1008 use a universal i to traverse the array, construct a node, then increse the i 04/20/2020
0560 Subarray Sum Equals K Array, HashTable Medium Algorithms 0560 prefix sum A[i,j] = A[0,j] - A[0,i-1] 04/29/2020
0523 Continuous Subarray Sum Math, DP Medium Algorithms 0523 sum % k = A[0, j] % k - A[0, i - 1] % k 04/30/2020
0713 Subarray Product Less Than K Array, Two Pointers Medium Algorithms 0713 keep maintain a sliding window,which max product less than k 05/01/2020
0152 Maximum Product Subarray Array, DP Medium Algorithms 0152 tracking the max and min product at the same time 05/01/2020
0974 Subarray Sums Divisible by K Array, Hash Table Medium Algorithms 0974 tracking the mod of sum, turn negative mod into positive 05/01/2020
0771 Jewels and Stones Hash Table Easy Algorithms 0771 HashSet 05/02/2020
0383 Ransom Note String Easy Algorithms 0383 dealing with letter, can use arr[26] 05/04/2020
0476 Number Complement Bit Manipulation Easy Algorithms 0476 '111' = '101' + '010' 05/04/2020
0169 Majority Element Array, Bit Manipulation Easy Algorithms 0169 sort and return nums[nums.length /2] 05/06/2020
0146 LRU Cache Design Medium Algorithms 0146 LinkedList, HashMap 05/06/2020
0993 Cousins in Binary Tree Tree, BFS Easy Algorithms 0993 BFS, queue, check i for same parents 05/07/2020
1162 As Far from Land as Possible BFS, Graph Medium Algorithms 1162 BFS, once from every land, fill step 05/07/2020
0127 Word Ladder BFS Medium Algorithms 0127 BFS, think about how to find neighbours 05/07/2020
0367 Valid Perfect Square Math, Binary Search Easy Algorithms 0367 num / mid < mid avoid overflow 05/09/2020
1232 Check If It Is a Straight Line Array, Math, Geometry Easy Algorithms 1232 check slope 05/09/2020
0997 Find the Town Judge Graph Easy Algorithms 0997 graph,check incoming and outgoing edges 05/10/2020
0773 Flood Fill DFS Easy Algorithms 0773 DFS, recursion, easy implement 05/11/2020
0583 Delete Operation for Two Strings String Medium Algorithms 0583 convert to longest common subsequence, use 2D-array 05/12/2020
0540 Single Element in a Sorted Array Array, Binary Search Medium Algorithms 0540 (even-odd)(even-odd)..... Binary search check break point 05/12/2020
0921 Minimum Add to Make Parentheses Valid Stack, Greedy Medium Algorithms 0921 stack check valid first, or use two counter count balanced parentheses 05/13/2020
0402 Remove K Digits Stack, Greedy Medium Algorithms 0402 sliding window, stack track lowest num 05/13/2020
0208 Implement Trie (Prefix Tree) Design, Trie Medium Algorithms 0208 A Trie contains the root TrieNode 05/14/2020
0677 Map Sum Pairs Trie Medium Algorithms 0677 Don't stuck with on direction 05/14/2020
0567 Permutation in String Two Pointers, Sliding Window Medium Algorithms 0567 think of a sliding window 05/19/2020
1277 Count Square Submatrices with All Ones Array, DP Medium Algorithms 1277 dp[][], min(dp[r-1][c-1], dp[c][r-1], dp[c-1][r]) + 1 05/21/2020
1035 Uncrossed Lines Array Medium Algorithms 1035 convert to lcs question 05/25/2020

Topic Review

  1. ArrayList vs LinkedList
  2. Two pointer search, left/right, slow/fast, for/while
  3. Java Arrays.sort() time complexity is nlog(n)
  4. Java array copy, System.arraycopy(arr,0,des,0,length), arr.clone()
  5. List.set()
  6. ArrayList add(Index, E) worst case cost O(n), LinkedList add(Index, E) always cost constant time.
  7. Rotate Array, extra array/ Cyclic Replacements/ Reverse
  8. Check duplicate use Set
  9. Hashtable.put(K,V) return null when the key not duplicate, return previous value when the key is duplicate.
  10. An Object(Integer, String) can hold null, a primitive type cannot hold null
  11. When solving absolute difference ..., using Math.abs()
  12. The prefix 0b usually means "binary". e.g. 0b111 => 7
  13. Bit Manipulation review
  14. HashSet.iterator().next(), HashMap.keySet().iterator().next()
  15. Math approach
  16. Sieve of Eratosthenes algorithm get all the prime number, time complexity, O(nloglogn)
  17. Integer.valueOf(i), This method will always cache values in the range -128 to 127, Thus, the reference are the same.
  18. Autoboxing, int => Integer; unboxing, Integer => int.
  19. Map, use getOrDefault(key, defaultValue) instead of checking containsKey()
  20. Array check equality Arrays.equals(arr1, arr2); Map check equality map1.equals(map2)
  21. For comparing String use equals() instead of == equality operator.
  22. use == to compare primitive e.g. boolean, int, char etc, while use equals() to compare objects in Java.
  23. == return true if two reference are of same object. Result of equals() method depends on overridden implementation.
  24. Stack to keep track the next large element.
  25. Switch(){ case "C": break; default:}
  26. res.toArray(new String[res.size()])
  27. If a array is larger than the main memory, use external sort, and streaming
  28. If a array is sorted, using two pointer search usually better.
  29. Two strings are anagrams if and only if their sorted strings are equal.
  30. Return a new empty list, return new ArrayList();
  31. Binary Tree Preorder Traversal => current > left > right
  32. Create helper function
  33. Always add to the head of a list, List.add(0, val)
  34. Pointer
  35. Using Queue to do BFS(Level Order Traversal), Stack to do DFS(Preorder/inorder/postorder Traversal)
  36. Cycle detection Floyd's Cycle Detection Algorithm (Two pointers)
  37. Two pointers, align the list, or make two iterations.
  38. LinkedList alway watch the head and tail case
  39. LinkedList add one dummy node before the head node can make the head case easier.
  40. Recursive, call the other result without me, go to the deepest and then start the return.
  41. Slow and fast pointer to get the middle point.
  42. ArrayList.add(index, val) will shift if the target index has value, use ArrayList.set(ind,val) to solve
  43. use a node which has min value to create the Min Stack
  44. Stack can remember
  45. Change the value in a grid to achieve the DFS or BFS
  46. Tree problem use Recursive, top-down, bottom-up
  47. int overflow control in Binary Search, mid = left + (right - left)/2;
  48. Bit manipulate, a>>1 == a/2
  49. HashSet<List<Integer>> to remove duplicate List works

Ask Questions

  1. How big is the input number? smaller than 32 bits(integer), int(-2^31, 2^31-1)?
  2. Does the linkedlist contains cycle or intersection?

About

My practice on Leetcode

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

Contributors

Languages