This article is for helping someone who wants to practice algorithms on leetcode, and already has some basic data structure knowledge like hash table, stack, queue, etc.
After solving some problems, I think some conceptual problems/solutions that once are understood, other problems are just the same. And without knowing these solutions, some problems are just not so easy to come up with a good solution.
Frequently Used Coding Tips, General Templates & Ideas
1. getting a bigger/smaller number
instead of using if/else:
1 | // C# |
1 | # Python |
2. check if two variables are null
If a or b is null, return false.
If both of them are null, return true.
Otherwise continue.
This snippet of code is so concise and beautiful, it gets rid of so much if/else.
1 | // C# |
1 | # Python |
3. BFS(Breath First Search) template
1 | // C# |
1 | # Python |
4. binary tree solution template
When visiting a node, usually there are three things we can do, base on the problem to design the order of steps.
- visit a node, do something(like adding the node's value into the result collection)
- visit the left child node
- visit the right child node
5. iterate an array over and over again
Iterate each item in an array from start to end again and again without worrying the index out of bound problem at the end of the array.
1 | // C# |
1 | # Python |
6. sorted data
Searching for something and the data is sorted or some math problem which is from 1 to N. Binary Search!
7. int overflow in binary search
Avoid integer overflow when adding two large integer in binary search
1 | // C# |
8. recusive solution might not be space complexity O(1)
In the Discuss page of the problem, some solution's code might looks concise by using recursion, but keep in mind that the stack takes memory space still, so usually the space complexity is not O(1).
Bit Manupliation
191. Number of 1 Bits
1 | // C# |
1 | # Python |
Array
448. Find All Numbers Disappeared in an Array
1 | // C# |
1 | # Python |
Key/Value
1. Two Sum
1 | // C# |
1 | class Solution: |
Binary Search
704. Binary Search
1 | // C# |
1 | class Solution: |
744. Find Smallest Letter Greater Than Target
1 | // C# |
1 | class Solution: |
Breath First Search
111. Minimum Depth of Binary Tree
1 | // C# |
1 | # Definition for a binary tree node. |
Linked List
83. Remove Duplicates from Sorted List
1 | // C# |
1 | # Definition for singly-linked list. |
141. Linked List Cycle
1 | // C# |
1 | # Definition for singly-linked list. |
142. Linked List Cycle II
Using Floyd's cycle detection algoritm to solve this problem only takes O(1) space complexity.
Why Floyd's cycle detection algorithm works? Detecting loop in a linked list.
1 | // C# |
1 | # Definition for singly-linked list. |
203. Remove Linked List Elements
1 | // C# |
1 | # Definition for singly-linked list. |
206. Reverse Linked List
1 | // C# |
1 | # Definition for singly-linked list. |
Sliding Window
Sliding Window algorithm template to solve all the Leetcode substring search problem.
438. Find All Anagrams in a String
567. Permutation in String
Prefix Sum
560. Subarray Sum Equals K
437. Path Sum III
Permutation, Backtracking
backtracking solution template
46. Permutations
1 | // C# |
1 | class Solution: |
1079. Letter Tile Possibilities
784. Letter Case Permutation
Dynamic Programming
746. Min Cost Climbing Stairs
53. Maximum Subarray
121. Best Time to Buy and Sell Stock
303. Range Sum Query - Immutable
1 | // C# |
1 | class NumArray: |