[LeetCode Online Judge] (https://leetcode.com/) is a website containing many algorithm questions. Most of them are real interview questions of Google, Facebook, LinkedIn, Apple, etc. This repo shows my solutions by Go with the code style strictly follows the Google Golang Style Guide. Please feel free to reference and STAR to support this repo, thank you!
数据结构 | 变种 | 相关题目 |
---|---|---|
顺序线性表:向量 | ||
单链表 | 1.双链表 2.静态链表 3.对称矩阵 4.稀疏矩阵 |
|
栈 | 广义栈 | |
队列 | 1.链表实现 2.循环数组实现 3.双端队列 |
|
字符串 | 1.KMP算法 2.有限状态自动机 3.模式匹配有限状态自动机 4.BM模式匹配算法 5.BM-KMP算法 |
|
树 | 1.二叉树 2.并查集 3.Huffman数 |
|
数组实现的堆 | 1.极大堆和极小堆 2.极大极小堆 3.双端堆 4.d叉堆 |
|
树实现的堆 | 1.左堆 2.扁堆 3.二项式堆 4.斐波那契堆 5.配对堆 |
|
查找 | 1.哈希表 2.跳跃表 3.排序二叉树 4.AVL树 5.B树 6.AA树 7.红黑树 8.排序二叉堆 9.Splay树 10.双链树 11.Trie树 |
标识了 (已全部做完) 的专题是全部完成了的,没有标识的是还没有做完的
- Array
- String
- Linked List
- Stack
- Tree
- Dynamic programming
- Depth-first search
- Math
- Search
- Sort
- Union Find
以下免费的算法题,暂时不能使用 Go 解答
- 116.Populating Next Right Pointers in Each Node
- 117.Populating Next Right Pointers in Each Node II
- 133.Clone Graph
- 138.Copy List with Random Pointer
- 141.Linked List Cycle
- 142.Linked List Cycle II
- 151.Reverse Words in a String
- 160.Intersection of Two Linked Lists
- 173.Binary Search Tree Iterator
- 190.Reverse Bits
- 191.Number of 1 Bits
- 222.Count Complete Tree Nodes
- 235.Lowest Common Ancestor of a Binary Search Tree
- 236.Lowest Common Ancestor of a Binary Tree
- 237.Delete Node in a Linked List
- 278.First Bad Version
- 284.Peeking Iterator
- 297.Serialize and Deserialize Binary Tree
- 341.Flatten Nested List Iterator
- 374.Guess Number Higher or Lower
- 386.Lexicographical Numbers
- 449.Serialize and Deserialize BST
- 535.Encode and Decode TinyURL
- 690.Employee Importance
Title | Solution | Difficulty | Time | Space |
---|---|---|---|---|
Max Consecutive Ones | Go | Easy | O(n) | O(1) |
Heaters | Go | Easy | O(nlogn) | O(1) |
Number of Boomerangs | Go | Easy | O(n ^ 2) | O(n) |
Island Perimeter | Go | Easy | O(nm) | O(1) |
Majority Element | Go | Easy | O(n) | O(1) |
Majority Element II | Go | Medium | O(n) | O(1) |
Intersection of Two Arrays | Go | Easy | O(n) | O(n) |
Intersection of Two Arrays II | Go | Easy | O(n) | O(n) |
Contains Duplicate | Go | Easy | O(n) | O(n) |
Contains Duplicate II | Go | Easy | O(n) | O(n) |
Remove Duplicates from Sorted Array | Go | Easy | O(n) | O(1) |
Remove Duplicates from Sorted Array II | Go | Medium | O(n) | O(1) |
Move Zeroes | Go | Easy | O(n) | O(1) |
Remove Element | Go | Easy | O(n) | O(1) |
Two Sum | Go | Easy | O(n) | O(n) |
3Sum | Go | Medium | O(n^2) | O(nC3) |
3Sum Closest | Go | Medium | O(n^2) | O(nC3) |
4Sum | Go | Medium | O(n^3) | O(nC4) |
Summary Ranges | Go | Medium | O(n) | O(n) |
Shortest Word Distance | Go | Easy | O(n) | O(1) |
Shortest Word Distance III | Go | Medium | O(n) | O(1) |
Minimum Size Subarray Sum | Go | Medium | O(n) | O(1) |
Maximum Size Subarray Sum Equals k | Go | Medium | O(n) | O(n) |
Smallest Range | Go | Hard | O(nm) | O(nm) |
Product of Array Except Self | Go | Medium | O(n) | O(n) |
Rotate Array | Go | Easy | O(n) | O(1) |
Rotate Image | Go | Medium | O(n^2) | O(1) |
Spiral Matrix | Go | Medium | O(n^2) | O(1) |
Spiral Matrix II | Go | Medium | O(n^2) | O(1) |
Valid Sudoku | Go | Easy | O(n^2) | O(n) |
Set Matrix Zero | Go | Medium | O(n^2) | O(1) |
Next Permutation | Go | Medium | O(n) | O(1) |
Gas Station | Go | Medium | O(n) | O(1) |
Game of Life | Go | Medium | O(n) | O(1) |
Task Scheduler | Go | Medium | O(nlogn) | O(n) |
Sliding Window Maximum | Go | Hard | O(n) | O(n) |
Longest Consecutive Sequence | Go | Hard | O(n) | O(n) |
- 巧妙的构造虚拟头结点。可以使遍历处理逻辑更加统一。
- 灵活使用递归。构造递归条件,使用递归可以巧妙的解题。不过需要注意有些题目不能使用递归,因为递归深度太深会导致超时和栈溢出。
- 链表区间逆序。第 92 题。
- 链表寻找中间节点。第 876 题。链表寻找倒数第 n 个节点。第 19 题。只需要一次遍历就可以得到答案。
- 合并 K 个有序链表。第 21 题,第 23 题。
- 链表归类。第 86 题,第 328 题。
- 链表排序,时间复杂度要求 O(n * log n),空间复杂度 O(1)。只有一种做法,归并排序,至顶向下归并。第 148 题。
- 判断链表是否存在环,如果有环,输出环的交叉点的下标;判断 2 个链表是否有交叉点,如果有交叉点,输出交叉点。第 141 题,第 142 题,第 160 题。
Title | Solution | Difficulty | Time | Space |
---|---|---|---|---|
Valid Parentheses | Go | Easy | O(n) | O(n) |
Longest Valid Parentheses | Go | Hard | O(n) | O(n) |
Evaluate Reverse Polish Notation | Go | Medium | O(n) | O(n) |
Simplify Path | Go | Medium | O(n) | O(n) |
Remove K Digits | Go | Medium | O(n) | O(n) |
Ternary Expression Parser | Go | Medium | O(n) | O(n) |
Binary Tree Preorder Traversal | Go | Medium | O(n) | O(n) |
Binary Tree Inorder Traversal | Go | Medium | O(n) | O(n) |
Binary Tree Postorder Traversal | Go | Hard | O(n) | O(n) |
Title | Solution | Difficulty | Time | Space |
---|---|---|---|---|
Permutations | Go | Medium | O(n!) | O(n) |
Permutations II | Go | Medium | O(n!) | O(n) |
Subsets | Go | Medium | O(n!) | O(n) |
Subsets II | Go | Medium | O(n!) | O(n) |
Combinations | Go | Medium | O(n!) | O(n) |
Combination Sum | Go | Medium | O(n^n) | O(2^n - 1) |
Combination Sum II | Go | Medium | O(n!) | O(2^n - 2) |
Combination Sum III | Go | Medium | O(n!) | O(nCk) |
Letter Combinations of a Phone Number | Go | Medium | O(mn) | O(n) |
Factor Combinations | Go | Medium | O(n^n)) | O(2^n - 1) |
Generalized Abbreviation | Go | Medium | O(n!) | O(2^n) |
Palindrome Partitioning | Go | Medium | O(n!) | O(n) |
Number of Islands | Go | Medium | O((mn)^2) | O(1) |
Walls and Gates | Go | Medium | O(n!) | O(2^n) |
Word Search | Go | Medium | O((n^2)!) | O(n^2) |
Word Search II | Go | Hard | O(((mn)^2)) | O(n^2) |
Add and Search Word - Data structure design | Go | Medium | O(n) | O(n) |
N-Queens | Go | Hard | O((n^4)) | O(n^2) |
N-Queens II | Go | Hard | O((n^3)) | O(n) |
Sudoku Solver | Go | Hard | O(n^4) | O(1) |
Remove Invalid Parentheses | Go | Hard | O(n!) | O(n) |
Expression Add Operators | Go | Hard | O(n!) | O(n) |
Title | Solution | Difficulty | Time | Space |
---|---|---|---|---|
Add Binary | Go | Easy | O(n) | O(n) |
Add Two Numbers | Go | Medium | O(n) | O(1) |
Add Digits | Go | Easy | O(1) | O(1) |
Plus One | Go | Easy | O(n) | O(1) |
Divide Two Integers | Go | Medium | O(logn) | O(1) |
Number Complement | Go | Easy | O(n) | O(1) |
Hamming Distance | Go | Easy | O(n) | O(1) |
Integer Break | Go | Medium | O(logn) | O(1) |
Happy Number | Go | Easy | O(n) | O(n) |
Single Number | Go | Medium | O(n) | O(1) |
Ugly Number | Go | Easy | O(logn) | O(1) |
Ugly Number II | Go | Medium | O(n) | O(n) |
Super Ugly Number | Go | Medium | O(n^2) | O(n) |
Count Primes | Go | Easy | O(n) | O(n) |
String to Integer (atoi) | Go | Easy | O(n) | O(1) |
Pow(x, n) | Go | Medium | O(logn) | O(1) |
Power of Two | Go | Easy | O(1) | O(1) |
Power of Three | Go | Easy | O(1) | O(1) |
Super Power | Go | Medium | O(n) | O(1) |
Sum of Two Integers | Go | Easy | O(n) | O(1) |
Reverse Integer | Go | Easy | O(n) | O(1) |
Excel Sheet Column Number | Go | Easy | O(n) | O(1) |
Integer to Roman | Go | Medium | O(n) | O(1) |
Roman to Integer | Go | Easy | O(n) | O(n) |
Integer to English Words | Go | Hard | O(n) | O(1) |
Rectangle Area | Go | Easy | O(1) | O(1) |
Trapping Rain Water | Go | Hard | O(n) | O(n) |
Container With Most Water | Go | Medium | O(n) | O(1) |
Counting Bits | Go | Medium | O(n) | O(n) |
K-th Smallest in Lexicographical Order | Go | Hard | O(n) | O(1) |
Title | Solution | Difficulty | Time | Space |
---|---|---|---|---|
Closest Binary Search Tree Value | Go | Easy | O(logn) | O(1) |
Closest Binary Search Tree Value II | Go | Hard | O(n) | O(n) |
Search in Rotated Sorted Array | Go | Hard | O(logn) | O(1) |
Search in Rotated Sorted Array II | Go | Medium | O(logn) | O(1) |
Find Minimum in Rotated Sorted Array | Go | Medium | O(logn) | O(1) |
Find Minimum in Rotated Sorted Array II | Go | Hard | O(logn) | O(1) |
Search a 2D Matrix | Go | Medium | O(log(m + n)) | O(1) |
Search a 2D Matrix II | Go | Medium | O(m + n) | O(1) |
Search for a Range | Go | Medium | O(logn) | O(1) |
Search Insert Position | Go | Medium | O(logn) | O(1) |
Find Peak Element | Go | Medium | O(logn) | O(1) |
Sqrt(x) | Go | Medium | O(logn) | O(1) |
Median of Two Sorted Arrays | Go | Hard | O(log(m + n)) | O(1) |
- 深刻的理解多路快排。第 75 题。
- 链表的排序,插入排序(第 147 题)和归并排序(第 148 题)
- 桶排序和基数排序。第 164 题。
- "摆动排序"。第 324 题。
- 两两不相邻的排序。第 767 题,第 1054 题。
- "饼子排序"。第 969 题。
Title | Solution | Difficulty | Time | Space | 收藏 |
---|---|---|---|---|---|
56. Merge Intervals | Go | Medium | O(n log n) | O(log n) | |
57. Insert Interval | Go | Hard | O(n) | O(1) | |
75. Sort Colors | Go | Medium | O(n) | O(1) | ❤️ |
147. Insertion Sort List | Go | Medium | O(n) | O(1) | ❤️ |
148. Sort List | Go | Medium | O(n log n) | O(log n) | ❤️ |
164. Maximum Gap | Go | Hard | O(n log n) | O(log n) | ❤️ |
179. Largest Number | Go | Medium | O(n log n) | O(log n) | ❤️ |
220. Contains Duplicate III | Go | Medium | O(n^2) | O(1) | |
242. Valid Anagram | Go | Easy | O(n) | O(n) | |
274. H-Index | Go | Medium | O(n) | O(n) | |
324. Wiggle Sort II | Go | Medium | O(n) | O(n) | ❤️ |
349. Intersection of Two Arrays | Go | Easy | O(n) | O(n) | |
350. Intersection of Two Arrays II | Go | Easy | O(n) | O(n) | |
524. Longest Word in Dictionary through Deleting | Go | Medium | O(n) | O(1) | |
767. Reorganize String | Go | Medium | O(n log n) | O(log n) | ❤️ |
853. Car Fleet | Go | Medium | O(n log n) | O(log n) | |
710. Random Pick with Blacklist | Go | Hard | O(n) | O(n) | |
922. Sort Array By Parity II | Go | Easy | O(n) | O(1) | |
969. Pancake Sorting | Go | Medium | O(n log n) | O(log n) | ❤️ |
973. K Closest Points to Origin | Go | Medium | O(n log n) | O(log n) | |
976. Largest Perimeter Triangle | Go | Easy | O(n log n) | O(log n) | |
1030. Matrix Cells in Distance Order | Go | Easy | O(n^2) | O(1) | |
1054. Distant Barcodes | Go | Medium | O(n log n) | O(log n) | |
----------------------------------------------------------------- | ------------- | ------------- | -------------------------- | -------------------------- | ------------- |
Title | Solution | Difficulty | Time | Space |
---|---|---|---|---|
Number of Connected Components in an Undirected Graph | Go | Medium | O(nlogn) | O(n) |
Graph Valid Tree | Go | Medium | O(nlogn) | O(n) |
Title | Solution | Difficulty | Frequency |
---|---|---|---|
Plus One | Go | Easy | ★★★★★★ |
Number of Islands | Go | Medium | ★★★★ |
Summary Ranges | Go | Medium | ★★★★ |
Perfect Squares | Go | Medium | ★★★★ |
Merge Intervals | Go | Hard | ★★★ |
Valid Parentheses | Go | Easy | ★★★ |
Trapping Rain Water | Go | Hard | ★★ |
Merge k Sorted Lists | Go | Hard | ★★ |
Longest Consecutive Sequence | Go | Hard | ★★ |
Find Peak Element | Go | Medium | ★★ |
Power of Two | Go | Easy | ★★ |
Spiral Matrix | Go | Medium | ★★ |
Sliding Window Maximum | Go | Hard | ★★ |
Pow(x, n) | Go | Medium | ★★ |
Letter Combinations of a Phone Number | Go | Medium | ★★ |
Heaters | Go | Easy | ★ |
Title | Solution | Difficulty | Frequency |
---|---|---|---|
3Sum | Go | Medium | ★★★★★★ |
Valid Palindrome | Go | Easy | ★★★★★★ |
Valid Palindrome II | Go | Easy | ★★★★★★ |
Move Zeroes | Go | Easy | ★★★★★★ |
Remove Invalid Parentheses | Go | Hard | ★★★★★★ |
Add Binary | Go | Easy | ★★★★★ |
Two Sum | Go | Easy | ★★★★★ |
Bnary Tree Paths | Go | Easy | ★★★★ |
Letter Combinations of a Phone Number | Go | Medium | ★★★★ |
Merge k Sorted Lists | Go | Hard | ★★★★ |
Reverse Linked List | Go | Easy | ★★★ |
Merge Intervals | Go | Hard | ★★★ |
Number of Islands | Go | Medium | ★★★ |
Reverse Linked List | Go | Easy | ★★★ |
Expression Add Operators | Go | Hard | ★★★ |
Subsets | Go | Medium | ★★★ |
Sort Colors | Go | Medium | ★★ |
Title | Solution | Difficulty | Frequency |
---|---|---|---|
Game of Life | Go | Medium | ★★★★★★ |
Meeting Rooms II | Go | Medium | ★★★★★★ |
Valid Sudoku | Go | Easy | ★★★★★ |
Binary Tree Vertical Order Traversal | Go | Medium | ★★★★ |
Alien Dictionary | Go | Hard | ★★★★ |
One Edit Distance | Go | Medium | ★★★ |
Sudoku Solver | Go | Hard | ★★★ |
Reverse Linked List | Go | Easy | ★★ |
Unique Binary Search Trees | Go | Medium | ★★ |
Minimum Window Substring | Go | Hard | ★★ |
Remove K Digits | Go | Medium | ★ |
Ternary Expression Parser | Go | Medium | ★ |
Title | Solution | Difficulty | Frequency |
---|---|---|---|
Valid Sudoku | Go | Easy | ★★★★ |
Spiral Matrix | Go | Medium | ★★★★ |
Letter Combinations of a Phone Number | Go | Medium | ★★★★ |
Group Anagrams | Go | Medium | ★★★★ |
Word Pattern | Go | Easy | ★★★ |
Roman to Integer | Go | Easy | ★★★ |
Combination Sum | Go | Medium | ★★ |
Title | Solution | Difficulty | Frequency |
---|---|---|---|
Two Sum | Go | Easy | ★★★★★ |
Text Justification | Go | Hard | ★★★★ |
House Robber | Go | Easy | ★★ |
Single Number | Go | Medium | ★★ |
Word Search II | Go | Hard | ★★ |
Add Two Numbers | Go | Medium | ★★ |
Title | Solution | Difficulty | Frequency |
---|---|---|---|
Maximum Subarray | Go | Medium | ★★★★★★ |
Pow(x, n) | Go | Medium | ★★★★★★ |
Merge Intervals | Go | Hard | ★★★★★★ |
Isomorphic Strings | Go | Easy | ★★★★★★ |
Search in Rotated Sorted Array | Go | Hard | ★★★★★ |
Search for a Range | Go | Medium | ★★★★★ |
Two Sum | Go | Easy | ★★★★ |
Binary Tree Level Order Traversal | Go | Easy | ★★★★ |
Evaluate Reverse Polish Notation | Go | Medium | ★★★ |
Maximum Product Subarray | Go | Medium | ★★★ |
Product of Array Except Self | Go | Medium | ★★★ |
Symmetric Tree | Go | Easy | ★★ |
Title | Solution | Difficulty | Frequency |
---|---|---|---|
Two Sum | Go | Easy | ★★★★★★ |
Min Cost Climbing Stairs | Go | Easy | ★★★★ |
Number of Islands | Go | Medium | ★★ |
Add Two Numbers | Go | Medium | ★★ |
Reverse Linked List | Go | Easy | ★★ |
Valid Parentheses | Go | Easy | ★★ |
Longest Palindromic Substring | Go | Medium | ★★ |
Trapping Rain Water | Go | Hard | ★★ |
Longest Substring Without Repeating Characters | Go | Medium | ★★ |
Letter Combinations of a Phone Number | Go | Medium | ★★ |
Valid Anagram | Go | Easy | ★★ |
Rotate Image | Go | Medium | ★★ |
Best Time to Buy and Sell Stock | Go | Easy | ★★ |
3Sum | Go | Medium | ★★ |
Sliding Window Maximum | Go | Hard | ★★ |
Title | Solution | Difficulty | Frequency |
---|---|---|---|
Reverse Linked List | Go | Easy | ★★★★★★ |
Two Sum | Go | Easy | ★★★★★ |
String to Integer (atoi) | Go | Easy | ★★★★ |
Add Two Numbers | Go | Medium | ★★★★ |
Excel Sheet Column Number | Go | Easy | ★★★★ |
Validate Binary Search Tree | Go | Medium | ★★★ |
Merge Two Sorted Lists | Go | Easy | ★★★ |