Merge pull request #2003 from juguagua/leetcode-add-complexity-analysis-StackAndQueue

添加复杂度分析: 栈与队列
This commit is contained in:
程序员Carl
2023-04-04 10:47:46 +08:00
committed by GitHub
7 changed files with 27 additions and 2 deletions

View File

@ -135,6 +135,9 @@ public:
};
```
* 时间复杂度: O(n)
* 空间复杂度: O(n)
技巧性的东西没有固定的学习方法,还是要多看多练,自己灵活运用了。

View File

@ -113,6 +113,9 @@ public:
};
```
* 时间复杂度: O(n)
* 空间复杂度: O(n)
## 题外话

View File

@ -111,6 +111,8 @@ public:
}
};
```
* 时间复杂度: push为O(n)其他为O(1)
* 空间复杂度: O(n)
# 优化
@ -156,6 +158,9 @@ public:
}
};
```
* 时间复杂度: push为O(n)其他为O(1)
* 空间复杂度: O(n)
# 其他语言版本

View File

@ -112,6 +112,10 @@ public:
```
* 时间复杂度: push和empty为O(1), pop和peek为O(n)
* 空间复杂度: O(n)
## 拓展
可以看出peek()的实现直接复用了pop() 要不然对stOut判空的逻辑又要重写一遍。

View File

@ -184,6 +184,9 @@ public:
}
};
```
* 时间复杂度: O(n)
* 空间复杂度: O(k)
再来看一下时间复杂度,使用单调队列的时间复杂度是 O(n)。

View File

@ -79,8 +79,6 @@
```CPP
// 时间复杂度O(nlogk)
// 空间复杂度O(n)
class Solution {
public:
// 小顶堆
@ -120,6 +118,10 @@ public:
}
};
```
* 时间复杂度: O(nlogk)
* 空间复杂度: O(n)
# 拓展
大家对这个比较运算在建堆时是如何应用的,为什么左大于右就会建立小顶堆,反而建立大顶堆比较困惑。

View File

@ -77,6 +77,9 @@ public:
}
};
```
* 时间复杂度: O(n)
* 空间复杂度: O(n)
当然可以拿字符串直接作为栈,这样省去了栈还要转为字符串的操作。
@ -99,6 +102,8 @@ public:
}
};
```
* 时间复杂度: O(n)
* 空间复杂度: O(1),返回值不计空间复杂度
## 题外话