Skip to content

Commit d3b0589

Browse files
committed
添加复杂度信息
1 parent f536e9e commit d3b0589

File tree

3 files changed

+37
-33
lines changed

3 files changed

+37
-33
lines changed

C++/chapSearching.tex

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ \subsubsection{分析}
2424
\subsubsection{代码}
2525
\begin{Code}
2626
// LeetCode, Search for a Range
27-
// 偷懒的做法
27+
// 偷懒的做法,使用STL
28+
// 时间复杂度O(logn),空间复杂度O(1)
2829
class Solution {
2930
public:
3031
vector<int> searchRange(int A[], int n, int target) {
@@ -41,6 +42,7 @@ \subsubsection{代码}
4142
\begin{Code}
4243
// LeetCode, Search for a Range
4344
// 重新实现 lower_bound 和 upper_bound
45+
// 时间复杂度O(logn),空间复杂度O(1)
4446
class Solution {
4547
public:
4648
vector<int> searchRange (int A[], int n, int target) {
@@ -113,6 +115,7 @@ \subsubsection{代码}
113115
\begin{Code}
114116
// LeetCode, Search Insert Position
115117
// 重新实现 lower_bound
118+
// 时间复杂度O(logn),空间复杂度O(1)
116119
class Solution {
117120
public:
118121
int searchInsert(int A[], int n, int target) {
@@ -170,6 +173,7 @@ \subsubsection{分析}
170173
\subsubsection{代码}
171174
\begin{Code}
172175
// LeetCode, Search a 2D Matrix
176+
// 时间复杂度O(logn),空间复杂度O(1)
173177
class Solution {
174178
public:
175179
bool searchMatrix(const vector<vector<int>>& matrix, int target) {

C++/chapSorting.tex

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ \subsubsection{分析}
1818
\subsubsection{代码}
1919
\begin{Code}
2020
//LeetCode, Merge Sorted Array
21+
// 时间复杂度O(m+n),空间复杂度O(1)
2122
class Solution {
2223
public:
2324
void merge(int A[], int m, int B[], int n) {
@@ -55,6 +56,7 @@ \subsubsection{分析}
5556
\subsubsection{代码}
5657
\begin{Code}
5758
//LeetCode, Merge Two Sorted Lists
59+
// 时间复杂度O(m+n),空间复杂度O(1)
5860
class Solution {
5961
public:
6062
ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
@@ -98,6 +100,7 @@ \subsubsection{分析}
98100
\subsubsection{代码}
99101
\begin{Code}
100102
//LeetCode, Merge k Sorted Lists
103+
// 时间复杂度O(n1+n2+...),空间复杂度O(1)
101104
class Solution {
102105
public:
103106
ListNode *mergeKLists(vector<ListNode *> &lists) {
@@ -158,6 +161,7 @@ \subsubsection{分析}
158161
\subsubsection{代码}
159162
\begin{Code}
160163
// LeetCode, First Missing Positive
164+
// 时间复杂度O(n),空间复杂度O(1)
161165
class Solution {
162166
public:
163167
int firstMissingPositive(int A[], int n) {
@@ -221,6 +225,7 @@ \subsubsection{代码}
221225
\begin{Code}
222226
// LeetCode, Sort Colors
223227
// Counting Sort
228+
// 时间复杂度O(n),空间复杂度O(1)
224229
class Solution {
225230
public:
226231
void sortColors(int A[], int n) {
@@ -239,7 +244,7 @@ \subsubsection{代码}
239244

240245
\begin{Code}
241246
// LeetCode, Sort Colors
242-
// 双指针
247+
// 双指针,时间复杂度O(n),空间复杂度O(1)
243248
class Solution {
244249
public:
245250
void sortColors(int A[], int n) {
@@ -261,6 +266,7 @@ \subsubsection{代码}
261266
\begin{Code}
262267
// LeetCode, Sort Colors
263268
// use partition()
269+
// 时间复杂度O(n),空间复杂度O(1)
264270
class Solution {
265271
public:
266272
void sortColors(int A[], int n) {
@@ -273,6 +279,7 @@ \subsubsection{代码}
273279
\begin{Code}
274280
// LeetCode, Sort Colors
275281
// 重新实现 partition()
282+
// 时间复杂度O(n),空间复杂度O(1)
276283
class Solution {
277284
public:
278285
void sortColors(int A[], int n) {

C++/chapTree.tex

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,30 @@ \subsubsection{代码}
724724
递归版
725725
\begin{Code}
726726
// LeetCode, Flatten Binary Tree to Linked List
727-
// 递归版1
727+
// 递归版1,时间复杂度O(n),空间复杂度O(logn)
728+
class Solution {
729+
public:
730+
void flatten(TreeNode *root) {
731+
if (root == nullptr) return; // 终止条件
732+
733+
flatten(root->left);
734+
flatten(root->right);
735+
736+
if (nullptr == root->left) return;
737+
738+
// 三方合并,将左子树所形成的链表插入到root和root->right之间
739+
TreeNode *p = root->left;
740+
while(p->right) p = p->right; //寻找左链表最后一个节点
741+
p->right = root->right;
742+
root->right = root->left;
743+
root->left = nullptr;
744+
}
745+
};
746+
\end{Code}
747+
748+
\begin{Code}
749+
// LeetCode, Flatten Binary Tree to Linked List
750+
// 递归版2
728751
// @author 王顺达(http://weibo.com/u/1234984145)
729752
// 时间复杂度O(n),空间复杂度O(logn)
730753
class Solution {
@@ -744,36 +767,6 @@ \subsubsection{代码}
744767
};
745768
\end{Code}
746769

747-
\begin{Code}
748-
// LeetCode, Flatten Binary Tree to Linked List
749-
// 递归版2,时间复杂度O(n),空间复杂度O(logn)
750-
class Solution {
751-
public:
752-
void flatten(TreeNode *root) {
753-
if (root == nullptr) return; // 终止条件
754-
//1.flat the left subtree
755-
if (root->left) flatten(root->left);
756-
757-
//2.flatten the right subtree
758-
if (root->right) flatten(root->right);
759-
760-
//3.if no left return
761-
if (nullptr == root->left) return;
762-
763-
//4.insert left sub tree between root and root->right
764-
//4.1.find the last node in left
765-
TreeNode *p = root->left;
766-
while(p->right) p = p->right;
767-
p->right = root->right;
768-
//4.2.connect right sub tree after left sub tree
769-
p->right = root->right;
770-
//4.3.move left sub tree to the root's right sub tree
771-
root->right = root->left;
772-
root->left = nullptr;
773-
}
774-
};
775-
\end{Code}
776-
777770
迭代版
778771
\begin{Code}
779772
// LeetCode, Flatten Binary Tree to Linked List

0 commit comments

Comments
 (0)