[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!
标识了 (已全部做完) 的专题是完成所有题目了的,没有标识的是还没有做完所有题目的
- Array
- String
- Linked List
- Stack
- Tree
- Dynamic programming
- Depth-first search
- Breadth-first Search
- Binary Search
- Math
- Hash Table
- Sort
数据结构 | 变种 | 相关题目 |
---|---|---|
顺序线性表:向量 | ||
单链表 | 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树 |
Title | Solution | Difficulty | Time | Space | 收藏 |
---|---|---|---|---|---|
3. Longest Substring Without Repeating Characters | Go | Medium | O(n) | O(1) | |
17. Letter Combinations of a Phone Number | Go | Medium | O(log n) | O(1) | |
20. Valid Parentheses | Go | Easy | O(log n) | O(1) | |
22. Generate Parentheses | Go | Medium | O(log n) | O(1) | |
28. Implement strStr() | Go | Easy | O(n) | O(1) | |
30. Substring with Concatenation of All Words | Go | Hard | O(n) | O(n) | |
49. Group Anagrams | Go | Medium | O(n log n) | O(n) | |
71. Simplify Path | Go | Medium | O(n) | O(n) | |
76. Minimum Window Substring | Go | Hard | O(n) | O(n) | |
91. Decode Ways | Go | Medium | O(n) | O(n) | |
93. Restore IP Addresses | Go | Medium | O(log n) | O(n) | |
125. Valid Palindrome | Go | Easy | O(n) | O(1) | |
126. Word Ladder II | Go | Hard | O(n) | O(n^2) | |
344. Reverse String | Go | Easy | O(n) | O(1) | |
345. Reverse Vowels of a String | Go | Easy | O(n) | O(1) | |
767. Reorganize String | Go | Medium | O(n log n) | O(log n) | ❤️ |
842. Split Array into Fibonacci Sequence | Go | Medium | O(n^2) | O(1) | |
856. Score of Parentheses | Go | Medium | O(n) | O(n) | |
925. Long Pressed Name | Go | Easy | O(n) | O(1) | |
1003. Check If Word Is Valid After Substitutions | Go | Medium | O(n) | O(1) | |
----------------------------------------------------------------- | ------------- | ------------- | -------------------------- | -------------------------- | ------------- |
- 巧妙的构造虚拟头结点。可以使遍历处理逻辑更加统一。
- 灵活使用递归。构造递归条件,使用递归可以巧妙的解题。不过需要注意有些题目不能使用递归,因为递归深度太深会导致超时和栈溢出。
- 链表区间逆序。第 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 | 收藏 |
---|---|---|---|---|---|
53. Maximum Subarray | Go | Easy | O(n) | O(n) | |
62. Unique Paths | Go | Medium | O(n^2) | O(n^2) | |
63. Unique Paths II | Go | Medium | O(n^2) | O(n^2) | |
64. Minimum Path Sum | Go | Medium | O(n^2) | O(n^2) | |
70. Climbing Stairs | Go | Easy | O(n) | O(n) | |
91. Decode Ways | Go | Medium | O(n) | O(n) | |
96. Unique Binary Search Trees | Go | Medium | O(n) | O(n) | |
120. Triangle | Go | Medium | O(n^2) | O(n) | |
121. Best Time to Buy and Sell Stock | Go | Easy | O(n) | O(1) | |
152. Maximum Product Subarray | Go | Medium | O(n) | O(1) | |
198. House Robber | Go | Easy | O(n) | O(n) | |
213. House Robber II | Go | Medium | O(n) | O(n) | |
300. Longest Increasing Subsequence | Go | Medium | O(n log n) | O(n) | |
309. Best Time to Buy and Sell Stock with Cooldown | Go | Medium | O(n) | O(n) | |
322. Coin Change | Go | Medium | O(n) | O(n) | |
338. Counting Bits | Go | Medium | O(n) | O(n) | |
343. Integer Break | Go | Medium | O(n^2) | O(n) | |
357. Count Numbers with Unique Digits | Go | Medium | O(1) | O(1) | |
392. Is Subsequence | Go | Medium | O(n) | O(1) | |
416. Partition Equal Subset Sum | Go | Medium | O(n^2) | O(n) | |
714. Best Time to Buy and Sell Stock with Transaction Fee | Go | Medium | O(n) | O(1) | |
746. Min Cost Climbing Stairs | Go | Easy | O(n) | O(1) | |
838. Push Dominoes | Go | Medium | O(n) | O(1) | |
1025. Divisor Game | Go | Easy | O(1) | O(1) | |
891. Sum of Subsequence Widths | Go | Hard | O(n) | O(1) | |
942. DI String Match | Go | Easy | O(n) | O(1) | |
----------------------------------------------------------------- | ------------- | ------------- | -------------------------- | -------------------------- | ------------- |
Title | Solution | Difficulty | Time | Space | 收藏 |
---|---|---|---|---|---|
101. Symmetric Tree | Go | Easy | O(n) | O(1) | |
102. Binary Tree Level Order Traversal | Go | Medium | O(n) | O(1) | |
103. Binary Tree Zigzag Level Order Traversal | Go | Medium | O(n) | O(n) | |
107. Binary Tree Level Order Traversal II | Go | Easy | O(n) | O(1) | |
111. Minimum Depth of Binary Tree | Go | Easy | O(n) | O(1) | |
126. Word Ladder II | Go | Hard | O(n) | O(n^2) | |
127. Word Ladder | Go | Medium | O(n) | O(n) | |
199. Binary Tree Right Side View | Go | Medium | O(n) | O(1) | |
200. Number of Islands | Go | Medium | O(n^2) | O(n^2) | |
207. Course Schedule | Go | Medium | O(n^2) | O(n^2) | |
210. Course Schedule II | Go | Medium | O(n^2) | O(n^2) | |
515. Find Largest Value in Each Tree Row | Go | Medium | O(n) | O(n) | |
542. 01 Matrix | Go | Medium | O(n) | O(1) | |
993. Cousins in Binary Tree | Go | Easy | O(n) | O(1) | |
----------------------------------------------------------------- | ------------- | ------------- | -------------------------- | -------------------------- | ------------- |
Title | Solution | Difficulty | Time | Space | 收藏 |
---|---|---|---|---|---|
50. Pow(x, n) | Go | Medium | O(log n) | O(1) | |
69. Sqrt(x) | Go | Easy | O(log n) | O(1) | |
167. Two Sum II - Input array is sorted | Go | Easy | O(n) | O(1) | |
209. Minimum Size Subarray Sum | Go | Medium | O(n) | O(1) | |
222. Count Complete Tree Nodes | Go | Medium | O(n) | O(1) | |
230. Kth Smallest Element in a BST | Go | Medium | O(n) | O(1) | |
287. Find the Duplicate Number | Go | Easy | O(n) | O(1) | |
300. Longest Increasing Subsequence | Go | Medium | O(n log 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) | |
392. Is Subsequence | Go | Medium | O(n) | O(1) | |
454. 4Sum II | Go | Medium | O(n^2) | O(n) | |
710. Random Pick with Blacklist | Go | Hard | O(n) | O(n) | |
----------------------------------------------------------------- | ------------- | ------------- | -------------------------- | -------------------------- | ------------- |
Title | Solution | Difficulty | Time | Space | 收藏 |
---|---|---|---|---|---|
2. Add Two Numbers | Go | Medium | O(n) | O(1) | |
50. Pow(x, n) | Go | Medium | O(log n) | O(1) | |
60. Permutation Sequence | Go | Medium | O(n log n) | O(1) | |
69. Sqrt(x) | Go | Easy | O(log n) | O(1) | |
202. Happy Number | Go | Easy | O(log n) | O(1) | |
224. Basic Calculator | Go | Hard | O(n) | O(n) | |
231. Power of Two | Go | Easy | O(1) | O(1) | |
263. Ugly Number | Go | Easy | O(log n) | O(1) | |
326. Power of Three | Go | Easy | O(1) | O(1) | |
343. Integer Break | Go | Medium | O(n^2) | O(n) | |
357. Count Numbers with Unique Digits | Go | Medium | O(1) | O(1) | |
628. Maximum Product of Three Numbers | Go | Easy | O(n) | O(1) | |
885. Spiral Matrix III | Go | Medium | O(n^2) | O(1) | |
891. Sum of Subsequence Widths | Go | Hard | O(n) | O(1) | |
942. DI String Match | Go | Easy | O(n) | O(1) | |
976. Largest Perimeter Triangle | Go | Easy | O(n log n) | O(log n) | |
996. Number of Squareful Arrays | Go | Hard | O(n log n) | O(n) | |
1025. Divisor Game | Go | Easy | O(1) | 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) | |
----------------------------------------------------------------- | ------------- | ------------- | -------------------------- | -------------------------- | ------------- |