LeetCode 代码和题解集合
# | 题目 | 代码 | 时间复杂度 | 空间复杂度 | 难度 | 题解 | 备注 |
---|---|---|---|---|---|---|---|
001 | Two Sum | C++ Go | O(n) | O(n) | Easy | 🚩 | |
003 | Longest Substring Without Repeating Characters | C++ Go | O(n) | O(1) | Medium | 🚩 | |
036 | Valid Sudoku | C++ | O(9^2) | O(9) | Easy | ||
049 | Group Anagrams | C++ Go | O(n * glogg) | O(n) | Medium | 🚩 | |
076 | Minimum Window Substring | C++ | O(n) | O(k) | Hard | ||
128 | Longest Consecutive Sequence | C++ Go | O(n) | O(n) | Hard | 🚩 | |
149 | Max Points on a Line | C++ Go | O(n^2) | O(n) | Hard | 🚩 | |
187 | Repeated DNA Sequences | C++ | O(n) | O(n) | Medium | ||
205 | Isomorphic Strings | C++ | O(n) | O(1) | Easy | ||
217 | Contains Duplicate | C++ | O(n) | O(n) | Easy | ||
560 | Subarray Sum Equals K | C++ Go | O(n) | O(n) | Medium | 🚩 | |
771 | Jewels and Stones | C++ | O(m + n) | O(n) | Easy |
# | 题目 | 代码 | 时间复杂度 | 空间复杂度 | 难度 | 题解 | 备注 |
---|---|---|---|---|---|---|---|
002 | Add Two Numbers | C++ Go | O(n) | O(1) | Medium | 加法模拟 | |
019 | Remove Nth Node From End of List | C++ | O(n) | O(1) | Easy | 双指针 | |
021 | Merge Two Sorted Lists | C++ | O(n) | O(1) | Easy | ||
023 | Merge k Sorted Lists | C++ Go | O(nlogk) | O(1) | Hard | here | 🚩 |
024 | Swap Nodes in Pairs | C++ | O(n) | O(1) | Easy | ||
025 | Reverse Nodes in k-Group | C++ Go | O(n) | O(1) | Hard | 🚩 | |
061 | Rotate List | C++ Go | O(n) | O(1) | Medium | 🚩 | |
082 | Remove Duplicates from Sorted List II | C++ Go | O(n) | O(1) | Medium | 🚩 | |
083 | Remove Duplicates from Sorted List | C++ | O(n) | O(1) | Easy | ||
086 | Partition List | C++ | O(n) | O(1) | Medium | ||
092 | Reverse Linked List II | C++ | O(n) | O(1) | Medium | ||
138 | Copy List with Random Pointer | C++ Go | O(n) | O(1) | Hard | 🚩 | |
141 | Linked List Cycle | C++ | O(n) | O(1) | Easy | ||
142 | Linked List Cycle II | C++ Go | O(n) | O(1) | Medium | 🚩 | |
143 | Reorder List | C++ | O(n) | O(1) | Medium | ||
147 | Insertion Sort List | C++ | O(n^2) | O(1) | Medium | ||
148 | Sort List | C++ Go | O(nlogn) | O(logn) | Medium | 链表的归并排序实现 | 🚩 |
160 | Intersection of Two Linked Lists | C++ Go | O(m + n) | O(1) | Easy | 🚩 | |
162 | Find Peak Element | C++ | O(logn) | O(1) | Medium | ||
203 | Remove Linked List Elements | C++ | O(n) | O(1) | Easy | ||
206 | Reverse Linked List | C++ | O(n) | O(1) | Easy | ||
599 | Minimum Index Sum of Two Lists | C++ | O((m + n) * l) | O(m * l) | Easy |
# | 题目 | 代码 | 时间复杂度 | 空间复杂度 | 难度 | 题解 | 备注 |
---|---|---|---|---|---|---|---|
007 | Reverse Integer | C++ | O(1) | O(1) | Easy | ||
009 | Palindrome Number | C++ | O(1) | O(1) | Easy | ||
012 | Integer to Roman | C++ | O(n) | O(1) | Medium | ||
013 | Roman to Integer | C++ | O(n) | O(1) | Easy | ||
029 | Divide Two Integers | C++ | O(1) | O(1) | Medium | 位运算 | |
050 | Pow(x, n) | C++ Go | O(1) | O(1) | Medium | 快速幂运算 | 🚩 |
060 | Permutation Sequence | C++ | O(n^2) | O(n) | Medium | ||
065 | Valid Number | C++ | O(n) | O(1) | Hard | 匹配 | |
070 | Climbing Stairs | C++ | O(n) | O(1) | Easy | 斐波那契数列 | |
089 | Gray Code | C++ | O(2^n) | O(1) | Medium | ||
136 | Single Number | C++ | O(n) | O(1) | Easy | 异或操作 | |
137 | Single Number II | C++ | O(n) | O(1) | Medium | ||
166 | Fraction to Recurring Decimal | C++ | O(logn) | O(1) | Medium | ||
168 | Excel Sheet Column Title | C++ Go | O(logn) | O(1) | Easy | 🚩 | |
171 | Excel Sheet Column Number | C++ | O(n) | O(1) | Easy | ||
172 | Factorial Trailing Zeroes | C++ | O(1) | O(1) | Easy | ||
190 | Reverse Bits | C++ | O(1) | O(1) | Easy | 位操作 | |
191 | Number of 1 Bits | C++ | O(1) | O(1) | Easy | 按位与 | |
201 | Bitwise AND of Numbers Range | C++ | O(1) | O(1) | Medium | ||
202 | Happy Number | C++ | O(k) | O(k) | Easy | ||
204 | Count Primes | C++ | O(n) | O(n) | Easy | ||
231 | Power of Two | C++ | O(1) | O(1) | Easy | 位运算巧妙用法 | |
461 | Hamming Distance | C++ | O(1) | O(1) | Easy |
# | 题目 | 代码 | 时间复杂度 | 空间复杂度 | 难度 | 题解 | 备注 |
---|---|---|---|---|---|---|---|
005 | Longest Palindromic Substring | C++ Go | O(n) | O(n) | Medium | Manacher算法 | 🚩 |
006 | ZigZag Conversion | C++ | O(n) | O(1) | Easy | 模拟法 | |
008 | String to Integer (atoi) | C++ | O(n) | O(1) | Medium | 模拟法 | |
014 | Longest Common Prefix | C++ | O(n * k) | O(1) | Easy | ||
028 | Implement strStr() | C++ | O(n + k) | O(k) | Easy | KMP | |
030 | Substring with Concatenation of All Words | C++ | O((m + n) * k) | O(n * k) | Hard | here | |
038 | Count and Say | C++ | O(n * 2^n) | O(2^n) | Easy | ||
043 | Multiply Strings | C++ | O(m * n) | O(m + n) | Medium | 大数乘法 | |
058 | Length of Last Word | C++ | O(n) | O(1) | Easy | ||
067 | Add Binary | C++ | O(n) | O(1) | Easy | ||
068 | Text Justification | C++ | O(n) | O(1) | Hard | ||
071 | Simplify Path | C++ | O(n) | O(n) | Medium | ||
179 | Largest Number | C++ | O(nlogn) | O(1) | Medium | ||
125 | Valid Palindrome | C++ | O(n) | O(1) | Easy | ||
151 | Reverse Words in a String | C++ | O(n) | O(1) | Medium | ||
165 | Compare Version Numbers | C++ | O(n) | O(1) | Easy | ||
709 | To Lower Case | C++ | O(n) | O(1) | Easy |
# | 题目 | 代码 | 时间复杂度 | 空间复杂度 | 难度 | 题解 | 备注 |
---|---|---|---|---|---|---|---|
020 | Valid Parentheses | C++ | O(n) | O(n) | Easy | ||
032 | Longest Valid Parentheses | C++ | O(n) | O(1) | Hard | here | |
084 | Largest Rectangle in Histogram | C++ Go | O(n) | O(n) | Hard | 递增栈 | 🚩 |
085 | Maximal Rectangle | C++ Go | O(m * n) | O(n) | Hard | 递增栈 | 🚩 |
101 | Symmetric Tree | C++ | O(n) | O(h) | Easy | ||
150 | Evaluate Reverse Polish Notation | C++ | O(n) | O(n) | Medium | ||
155 | Min Stack | C++ | O(n) | O(1) | Easy | ||
173 | Binary Search Tree Iterator | C++ Go | O(1) | O(h) | Medium | 🚩 | |
227 | Basic Calculator II | C++ Go | O(n) | O(n) | Medium | 🚩 | |
456 | 132 Pattern | C++ Go | O(n) | O(n) | Medium | 单调栈 | 🚩 |
# | 题目 | 代码 | 时间复杂度 | 空间复杂度 | 难度 | 题解 | 备注 |
---|---|---|---|---|---|---|---|
015 | 3 Sum | C++ | O(n^2) | O(1) | Medium | here | |
016 | 3 Sum Closest | C++ | O(n^2) | O(1) | Medium | here | |
018 | 4 Sum | C++ | O(n^3) | O(1) | Medium | ||
026 | Remove Duplicates from Sorted Array | C++ | O(n) | O(1) | Easy | ||
027 | Remove Element | C++ | O(n) | O(1) | Easy | ||
031 | Next Permutation | C++ | O(n) | O(1) | Medium | here | |
035 | Search Insert Position | C++ | O(n) | O(1) | Easy | ||
041 | First Missing Positive | C++ | O(n) | O(1) | Hard | ||
048 | Rotate Image | C++ | O(n^2) | O(1) | Medium | ||
054 | Spiral Matrix | C++ | O(m * n) | O(1) | Medium | ||
059 | Spiral Matrix II | C++ | O(n^2) | O(1) | Medium | ||
066 | Plus One | C++ | O(n) | O(1) | Easy | ||
073 | Set Matrix Zeroes | C++ | O(m * n) | O(1) | Medium | ||
080 | Remove Duplicates from Sorted Array II | C++ Go | O(n) | O(1) | Medium | 双指针 | |
088 | Merge Sorted Array | C++ | O(n) | O(1) | Easy | ||
118 | Pascal's Triangle | C++ | O(n^2) | O(1) | Easy | ||
119 | Pascal's Triangle II | C++ | O(n^2) | O(1) | Easy | ||
121 | Best Time to Buy and Sell Stock | C++ Go | O(n) | O(1) | Easy | 逆向思维 | 🚩 |
167 | Two Sum II - Input array is sorted | C++ | O(n) | O(1) | Medium | 双指针 | |
169 | Majority Element | C++ | O(n) | O(1) | Easy | ||
189 | Rotate Array | C++ | O(n) | O(1) | Easy |
# | 题目 | 代码 | 时间复杂度 | 空间复杂度 | 难度 | 题解 | 备注 |
---|---|---|---|---|---|---|---|
004 | Median of Two Sorted Arrays | C++ | O(log(min(m, n))) | O(1) | Hard | here | |
033 | Search in Rotated Sorted Array | C++ | O(logn) | O(1) | Hard | 坐标转换+二分法 | |
034 | Search for a Range | C++ | O(logn) | O(1) | Medium | ||
069 | Sqrt(x) | C++ | O(logn) | O(1) | Medium | ||
081 | Search in Rotated Sorted Array II | C++ Go | O(logn) | O(1) | Medium | 🚩 | |
153 | Find Minimum in Rotated Sorted Array | C++ | O(logn) | O(1) | Medium | ||
154 | Find Minimum in Rotated Sorted Array II | C++ Go | O(logn) ~ O(n) | O(1) | Hard | 🚩 |
# | 题目 | 代码 | 时间复杂度 | 空间复杂度 | 难度 | 题解 | 备注 |
---|---|---|---|---|---|---|---|
010 | Regular Expression Matching | C++ C++ | O(m * n) | O(n) | Hard | 动态规划 | |
032 | Longest Valid Parentheses | C++ Go | O(n) | O(1) | Hard | here | |
044 | Wildcard Matching | C++ | O(m + n) | O(1) | Hard | DP | |
053 | Maximum Subarray | C++ 数学 | O(n) | O(1) | Medium | ||
062 | Unique Paths | C++ | O(m * n) | O(m + n) | Medium | ||
063 | Unique Paths II | C++ | O(m * n) | O(m + n) | Medium | ||
064 | Minimum Path Sum | C++ | O(m * n) | O(m + n) | Medium | ||
072 | Edit Distance | C++ | O(m * n) | O(m + n) | Hard | 空间待优化 | |
087 | Scramble String | C++ Go | O(n^4) | O(n^3) | Hard | 🚩 | |
095 | Unique Binary Search Trees II | C++ | O(4^n / n^(3/2) | O(4^n / n^(3/2) | Medium | 待完善 | |
096 | Unique Binary Search Trees | C++ | O(n) | O(1) | Medium | ||
097 | Interleaving String | C++ Go | O(m * n) | O(m + n) | Hard | 🚩 | |
115 | Distinct Subsequences | C++ Go | O(n^2) | O(n) | Hard | 🚩 | |
120 | Triangle | C++ | O(m * n) | O(n) | Medium | ||
132 | Palindrome Partitioning II | C++ Go | O(n^2) | O(n^2) | Hard | 🚩 | |
139 | Word Break | C++ Go | O(n * l^2) | O(n) | Medium | 🚩 | |
140 | Word Break II | C++ Go | O(n * l^2 + n * r) | O(n^2) | Hard | 🚩 | |
152 | Maximum Product Subarray | C++ Go | O(n) | O(1) | Medium | 🚩 | |
174 | Dungeon Game | C++ | O(m * n) | O(m + n) | Hard | ||
198 | House Robber | C++ | O(n) | O(1) | Easy | ||
300 | Longest Increasing Subsequence | Go | O(n^2) | O(n) | Medium | 🚩 | |
322 | Coin Change | C++ Go | O(n * k) | O(k) | Medium | 🚩 | |
375 | Guess Number Higher or Lower II | C++ | O(n^2) | O(n^2) | Medium | 还可以优化 | |
416 | Partition Equal Subset Sum | C++ | O(n * s) | O(s) | Medium | ||
486 | Predict the Winner | C++ Go | O(n^2) | O(n) | Medium | 🚩 | |
718 | Maximum Length of Repeated Subarray | Go | O(n*m) | O(m) | Medium | 最长公共子串 | 🚩 |
1143 | Longest Common Subsequence | Go | O(m*n) | O(m*n) | Medium |
# | 题目 | 代码 | 时间复杂度 | 空间复杂度 | 难度 | 题解 | 备注 |
---|---|---|---|---|---|---|---|
011 | Container With Most Water | C++ | O(n) | O(1) | Medium | 贪心算法 | |
042 | Trapping Rain Water | C++ Go | O(n) | O(1) | Hard | 双指针,巧妙方法 | 🚩 |
045 | Jump Game II | C++ | O(n) | O(1) | Hard | ||
055 | Jump Game | C++ | O(n) | O(1) | Medium | ||
091 | Decode Ways | C++ Go | O(n) | O(n) | Medium | 🚩 | |
122 | Best Time to Buy and Sell Stock II | C++ | O(n) | O(1) | Easy | ||
123 | Best Time to Buy and Sell Stock III | C++ | O(n) | O(1) | Hard | ||
134 | Gas Station | C++ Go | O(n) | O(1) | Medium | 🚩 | |
188 | Best Time to Buy and Sell Stock IV | C++ Go | O(k * n) | O(k) | Hard | 🚩 | |
407 | Trapping Rain Water II | Go | Hard | 🚩 |
# | 题目 | 代码 | 时间复杂度 | 空间复杂度 | 难度 | 题解 | 备注 |
---|---|---|---|---|---|---|---|
017 | Letter Combinations of a Phone Number | C++ 循环 C++ 回溯 | O(n * 4^n) | O(n) | Medium | ||
022 | Generate Parentheses | C++ 回溯 | O(4^n / n^(3/2)) | O(n) | Medium | ||
037 | Sudoku Solver | C++ | O((9!)^9) | O(1) | Hard | bfs | |
039 | Combination Sum | C++ | O(k * n^k) | O(k) | Medium | ||
040 | Combination Sum II | C++ | O(k * C(n, k)) | O(k) | Medium | ||
046 | Permutations | C++ | O(n * n!) | O(n) | Medium | ||
047 | Permutations II | C++ | O(n * n!) | O(n) | Medium | ||
051 | N-Queens | C++ | O(n!) | O(n) | Hard | 值得优化 | |
052 | N-Queens-II | C++ | O(n!) | O(n) | Hard | 值得优化 | |
077 | Combinations | C++ | O(O(k * C(n, k))) | O(k) | Medium | ||
078 | Subsets | C++ | O(n * 2^n) | O(1) | Medium | ||
079 | Word Search | C++ | O(m * n * l) | O(l) | Medium | ||
093 | Restore IP Addresses | O(1) | O(1) | Medium | 待完善 | ||
113 | Path Sum II | C++ | O(n) | O(h) | Medium | ||
126 | Word Ladder II | C++ Go | O(n * d) | O(d) | Hard | BFS | 🚩 |
127 | Word Ladder | C++ | O(n * d) | O(d) | Medium | ||
131 | Palindrome Partitioning | C++ | O(n^2) ~ O(2^n) | O(n^2) | Medium | ||
357 | Count Numbers with Unique Digits | C++ | O(n!) | O(1) | Medium | DP实现 |
# | 题目 | 代码 | 时间复杂度 | 空间复杂度 | 难度 | 题解 | 备注 |
---|---|---|---|---|---|---|---|
094 | Binary Tree Inorder Traversal | C++ | O(n) | O(1) | Medium | ||
099 | Recover Binary Search Tree | C++ Go | O(n) | O(n) | Hard | 🚩 | |
100 | Same Tree | C+ | O(n) | O(h) | Easy | ||
116 | Populating Next Right Pointers in Each Node | C++ | O(n) | O(logn) | Medium | ||
117 | Populating Next Right Pointers in Each Node II | C++ | O(n) | O(1) | Hard | ||
124 | Binary Tree Maximum Path Sum | C++ Go | O(n) | O(h) | Hard | 🚩 | |
145 | Binary Tree Postorder Traversal | C++ Go | O(n) | O(1) | Hard | 后序遍历 | 🚩 |
653 | Two Sum IV - Input is a BST | C++ | O(n) | O(h) | Easy | 双指针 | |
684 | Redundant Connection | C++ | O(n) | O(n) | Medium | 并查集 | |
685 | Redundant Connection II | C++ | O(n) | O(n) | Hard |
# | 题目 | 代码 | 时间复杂度 | 空间复杂度 | 难度 | 题解 | 备注 |
---|---|---|---|---|---|---|---|
056 | Merge Intervals | C++ | O(nlogn) | O(1) | Medium | ||
057 | Insert Interval | C++ | O(n) | O(1) | Hard | ||
075 | Sort Colors | C++ | O(n) | O(1) | Medium | 三指针 | |
164 | Maximum Gap | C++ Go | O(n) | O(n) | Hard | 桶排序应用 | 🚩 |
307 | Range Sum Query - Mutable | C++ | ctor: O(n), update: O(logn), query: O(logn) | O(n) | Medium | 树状数组 线段树 | |
912 | Sort an Array | Go | O(nlogn) | O(n) | Medium | 所有排序算法都写一遍 | 🚩 |
# | 题目 | 代码 | 时间复杂度 | 空间复杂度 | 难度 | 题解 | 备注 |
---|---|---|---|---|---|---|---|
090 | Subsets II | C++ | O(n * 2^n) | O(1) | Medium | ||
130 | Surrounded Regions | C++ | O(m * n) | O(m + n) | Medium | ||
199 | Binary Tree Right Side View | C++ | O(n) | O(h) | Medium | 待完善 | |
200 | Number of Islands | C++ | O(m * n) | O(m * n) | Medium |
# | 题目 | 代码 | 时间复杂度 | 空间复杂度 | 难度 | 题解 | 备注 |
---|---|---|---|---|---|---|---|
102 | Binary Tree Level Order Traversal | C++ Go | O(n) | O(n) | Easy | 🚩 | |
103 | Binary Tree Zigzag Level Order Traversal | C++ | O(n) | O(n) | Medium | ||
107 | Binary Tree Level Order Traversal II | C++ | O(n) | O(n) | Easy | ||
133 | Clone Graph | C++ | O(n) | O(n) | Medium | ||
207 | Course Schedule | C++ | O(|V| + |E|) | O(|E|) | Medium | Topological Sort |
# | 题目 | 代码 | 时间复杂度 | 空间复杂度 | 难度 | 题解 | 备注 |
---|---|---|---|---|---|---|---|
146 | LRU Cache | C++ Go | O(1) | O(k) | Hard | 🚩 |
# | 题目 | 代码 | 时间复杂度 | 空间复杂度 | 难度 | 题解 | 备注 |
---|---|---|---|---|---|---|---|
098 | Validate Binary Search Tree | C++ | O(n) | O(1) | Medium | ||
104 | Maximum Depth of Binary Tree | C++ | O(n) | O(h) | Easy | ||
108 | Convert Sorted Array to Binary Search Tree | C++ | O(n) | O(logn) | Medium | ||
105 | Construct Binary Tree from Preorder and Inorder Traversal | C++ Go | O(n) | O(n) | Medium | 🚩 | |
106 | Construct Binary Tree from Inorder and Postorder Traversal | C++ | O(n) | O(n) | Medium | ||
109 | Convert Sorted List to Binary Search Tree | C++ Go | O(n) | O(logn) | Medium | 🚩 | |
110 | Balanced Binary Tree | C++ | O(n) | O(h) | Easy | ||
111 | Minimum Depth of Binary Tree | C++ | O(n) | O(h) | Easy | ||
112 | Path Sum | C++ | O(n) | O(h) | Easy | ||
114 | Flatten Binary Tree to Linked List | C++ Go | O(n) | O(h) | Medium | 🚩 | |
129 | Sum Root to Leaf Numbers | C++ | O(n) | O(h) | Medium |
# | 题目 | 代码 | 时间复杂度 | 空间复杂度 | 难度 | 题解 | 备注 |
---|---|---|---|---|---|---|---|
175 | Combine Two Tables | MySQL | O(m + n) | O(m + n) | Easy | ||
176 | Second Highest Salary | MySQL | O(n) | O(1) | Easy | ||
177 | Nth Highest Salary | MySQL | O(n^2) | O(n) | Medium | ||
178 | Rank Scores | MySQL | O(n^2) | O(n) | Medium | ||
180 | Consecutive Numbers | MySQL | O(n) | O(n) | Medium | ||
181 | Employees Earning More Than Their Managers | MySQL | O(n^2) | O(1) | Easy | ||
182 | Duplicate Emails | MySQL | O(n^2) | O(1) | Easy |