2023-02-08 15:20:18 +08:00
|
|
|
|
---
|
|
|
|
|
comments: true
|
|
|
|
|
---
|
|
|
|
|
|
2023-02-24 18:45:27 +08:00
|
|
|
|
# 2.3. 空间复杂度
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-07 22:32:04 +08:00
|
|
|
|
「空间复杂度 Space Complexity」用于衡量算法使用内存空间随着数据量变大时的增长趋势。这个概念与时间复杂度非常类似。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-24 18:45:27 +08:00
|
|
|
|
## 2.3.1. 算法相关空间
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-07 22:32:04 +08:00
|
|
|
|
算法运行过程中使用的内存空间主要包括以下几种:
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-07-26 08:58:52 +08:00
|
|
|
|
- 「输入空间」用于存储算法的输入数据。
|
|
|
|
|
- 「暂存空间」用于存储算法运行过程中的变量、对象、函数上下文等数据。
|
|
|
|
|
- 「输出空间」用于存储算法的输出数据。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-07 22:32:04 +08:00
|
|
|
|
通常情况下,空间复杂度统计范围是「暂存空间」+「输出空间」。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-07 22:32:04 +08:00
|
|
|
|
暂存空间可以进一步划分为三个部分:
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-07 22:32:04 +08:00
|
|
|
|
- 「暂存数据」用于保存算法运行过程中的各种常量、变量、对象等。
|
|
|
|
|
- 「栈帧空间」用于保存调用函数的上下文数据。系统在每次调用函数时都会在栈顶部创建一个栈帧,函数返回后,栈帧空间会被释放。
|
|
|
|
|
- 「指令空间」用于保存编译后的程序指令,在实际统计中通常忽略不计。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-07 22:32:04 +08:00
|
|
|
|
因此,在分析一段程序的空间复杂度时,我们一般统计 **暂存数据、输出数据、栈帧空间** 三部分。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-26 18:17:57 +08:00
|
|
|
|
![算法使用的相关空间](space_complexity.assets/space_types.png)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-26 19:53:26 +08:00
|
|
|
|
<p align="center"> Fig. 算法使用的相关空间 </p>
|
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title=""
|
|
|
|
|
/* 类 */
|
|
|
|
|
class Node {
|
|
|
|
|
int val;
|
|
|
|
|
Node next;
|
|
|
|
|
Node(int x) { val = x; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 函数 */
|
|
|
|
|
int function() {
|
|
|
|
|
// do something...
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int algorithm(int n) { // 输入数据
|
|
|
|
|
final int a = 0; // 暂存数据(常量)
|
|
|
|
|
int b = 0; // 暂存数据(变量)
|
|
|
|
|
Node node = new Node(0); // 暂存数据(对象)
|
|
|
|
|
int c = function(); // 栈帧空间(调用函数)
|
|
|
|
|
return a + b + c; // 输出数据
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title=""
|
|
|
|
|
/* 结构体 */
|
|
|
|
|
struct Node {
|
|
|
|
|
int val;
|
|
|
|
|
Node *next;
|
|
|
|
|
Node(int x) : val(x), next(nullptr) {}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/* 函数 */
|
|
|
|
|
int func() {
|
|
|
|
|
// do something...
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int algorithm(int n) { // 输入数据
|
|
|
|
|
const int a = 0; // 暂存数据(常量)
|
|
|
|
|
int b = 0; // 暂存数据(变量)
|
2023-03-12 18:46:03 +08:00
|
|
|
|
Node* node = new Node(0); // 暂存数据(对象)
|
|
|
|
|
int c = func(); // 栈帧空间(调用函数)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
return a + b + c; // 输出数据
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title=""
|
|
|
|
|
class Node:
|
2023-04-09 05:30:47 +08:00
|
|
|
|
"""类"""
|
2023-03-12 18:46:03 +08:00
|
|
|
|
def __init__(self, x: int):
|
2023-04-09 04:34:58 +08:00
|
|
|
|
self.val: int = x # 节点值
|
|
|
|
|
self.next: Optional[Node] = None # 指向下一节点的指针(引用)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-03-12 18:46:03 +08:00
|
|
|
|
def function() -> int:
|
2023-04-09 05:30:47 +08:00
|
|
|
|
"""函数"""
|
2023-02-08 15:20:18 +08:00
|
|
|
|
# do something...
|
|
|
|
|
return 0
|
|
|
|
|
|
2023-03-12 18:46:03 +08:00
|
|
|
|
def algorithm(n) -> int: # 输入数据
|
2023-05-22 22:01:18 +08:00
|
|
|
|
A = 0 # 暂存数据(常量,一般用大写字母表示)
|
|
|
|
|
b = 0 # 暂存数据(变量)
|
2023-03-12 18:46:03 +08:00
|
|
|
|
node = Node(0) # 暂存数据(对象)
|
2023-05-22 22:01:18 +08:00
|
|
|
|
c = function() # 栈帧空间(调用函数)
|
2023-03-13 22:54:22 +08:00
|
|
|
|
return A + b + c # 输出数据
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title=""
|
|
|
|
|
/* 结构体 */
|
|
|
|
|
type node struct {
|
|
|
|
|
val int
|
|
|
|
|
next *node
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 创建 node 结构体 */
|
|
|
|
|
func newNode(val int) *node {
|
|
|
|
|
return &node{val: val}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 函数 */
|
|
|
|
|
func function() int {
|
|
|
|
|
// do something...
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func algorithm(n int) int { // 输入数据
|
|
|
|
|
const a = 0 // 暂存数据(常量)
|
|
|
|
|
b := 0 // 暂存数据(变量)
|
|
|
|
|
newNode(0) // 暂存数据(对象)
|
|
|
|
|
c := function() // 栈帧空间(调用函数)
|
|
|
|
|
return a + b + c // 输出数据
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:34:46 +08:00
|
|
|
|
=== "JS"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
```javascript title=""
|
|
|
|
|
/* 类 */
|
|
|
|
|
class Node {
|
|
|
|
|
val;
|
|
|
|
|
next;
|
|
|
|
|
constructor(val) {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
this.val = val === undefined ? 0 : val; // 节点值
|
|
|
|
|
this.next = null; // 指向下一节点的引用
|
2023-02-08 15:20:18 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 函数 */
|
|
|
|
|
function constFunc() {
|
|
|
|
|
// do something
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function algorithm(n) { // 输入数据
|
|
|
|
|
const a = 0; // 暂存数据(常量)
|
2023-03-13 22:54:22 +08:00
|
|
|
|
let b = 0; // 暂存数据(变量)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
const node = new Node(0); // 暂存数据(对象)
|
|
|
|
|
const c = constFunc(); // 栈帧空间(调用函数)
|
|
|
|
|
return a + b + c; // 输出数据
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:34:46 +08:00
|
|
|
|
=== "TS"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
```typescript title=""
|
|
|
|
|
/* 类 */
|
|
|
|
|
class Node {
|
|
|
|
|
val: number;
|
|
|
|
|
next: Node | null;
|
|
|
|
|
constructor(val?: number) {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
this.val = val === undefined ? 0 : val; // 节点值
|
|
|
|
|
this.next = null; // 指向下一节点的引用
|
2023-02-08 15:20:18 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 函数 */
|
|
|
|
|
function constFunc(): number {
|
|
|
|
|
// do something
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function algorithm(n: number): number { // 输入数据
|
|
|
|
|
const a = 0; // 暂存数据(常量)
|
2023-03-13 22:54:22 +08:00
|
|
|
|
let b = 0; // 暂存数据(变量)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
const node = new Node(0); // 暂存数据(对象)
|
|
|
|
|
const c = constFunc(); // 栈帧空间(调用函数)
|
|
|
|
|
return a + b + c; // 输出数据
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title=""
|
2023-04-18 20:19:07 +08:00
|
|
|
|
/* 函数 */
|
|
|
|
|
int func() {
|
|
|
|
|
// do something...
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-18 20:19:07 +08:00
|
|
|
|
int algorithm(int n) { // 输入数据
|
|
|
|
|
const int a = 0; // 暂存数据(常量)
|
|
|
|
|
int b = 0; // 暂存数据(变量)
|
|
|
|
|
int c = func(); // 栈帧空间(调用函数)
|
|
|
|
|
return a + b + c; // 输出数据
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title=""
|
|
|
|
|
/* 类 */
|
2023-07-25 16:42:55 +08:00
|
|
|
|
class Node {
|
2023-02-08 15:20:18 +08:00
|
|
|
|
int val;
|
|
|
|
|
Node next;
|
|
|
|
|
Node(int x) { val = x; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 函数 */
|
2023-07-25 16:42:55 +08:00
|
|
|
|
int function() {
|
2023-02-08 15:20:18 +08:00
|
|
|
|
// do something...
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-25 16:42:55 +08:00
|
|
|
|
int algorithm(int n) { // 输入数据
|
2023-03-13 22:54:22 +08:00
|
|
|
|
const int a = 0; // 暂存数据(常量)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
int b = 0; // 暂存数据(变量)
|
|
|
|
|
Node node = new Node(0); // 暂存数据(对象)
|
|
|
|
|
int c = function(); // 栈帧空间(调用函数)
|
|
|
|
|
return a + b + c; // 输出数据
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title=""
|
|
|
|
|
/* 类 */
|
|
|
|
|
class Node {
|
|
|
|
|
var val: Int
|
|
|
|
|
var next: Node?
|
|
|
|
|
|
|
|
|
|
init(x: Int) {
|
|
|
|
|
val = x
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 函数 */
|
|
|
|
|
func function() -> Int {
|
|
|
|
|
// do something...
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func algorithm(n: Int) -> Int { // 输入数据
|
2023-03-13 22:54:22 +08:00
|
|
|
|
let a = 0 // 暂存数据(常量)
|
|
|
|
|
var b = 0 // 暂存数据(变量)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
let node = Node(x: 0) // 暂存数据(对象)
|
2023-03-13 22:54:22 +08:00
|
|
|
|
let c = function() // 栈帧空间(调用函数)
|
|
|
|
|
return a + b + c // 输出数据
|
2023-02-08 15:20:18 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title=""
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2023-06-02 02:38:24 +08:00
|
|
|
|
=== "Dart"
|
|
|
|
|
|
|
|
|
|
```dart title=""
|
2023-06-02 15:07:33 +08:00
|
|
|
|
/* 类 */
|
|
|
|
|
class Node {
|
|
|
|
|
int val;
|
|
|
|
|
Node next;
|
|
|
|
|
Node(this.val, [this.next]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 函数 */
|
|
|
|
|
int function() {
|
|
|
|
|
// do something...
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2023-06-02 02:38:24 +08:00
|
|
|
|
|
2023-06-02 15:07:33 +08:00
|
|
|
|
int algorithm(int n) { // 输入数据
|
|
|
|
|
const int a = 0; // 暂存数据(常量)
|
|
|
|
|
int b = 0; // 暂存数据(变量)
|
|
|
|
|
Node node = Node(0); // 暂存数据(对象)
|
|
|
|
|
int c = function(); // 栈帧空间(调用函数)
|
|
|
|
|
return a + b + c; // 输出数据
|
|
|
|
|
}
|
2023-06-02 02:38:24 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 10:57:40 +08:00
|
|
|
|
=== "Rust"
|
|
|
|
|
|
|
|
|
|
```rust title=""
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2023-02-24 18:45:27 +08:00
|
|
|
|
## 2.3.2. 推算方法
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-07 22:32:04 +08:00
|
|
|
|
空间复杂度的推算方法与时间复杂度大致相同,只是将统计对象从“计算操作数量”转为“使用空间大小”。与时间复杂度不同的是,**我们通常只关注「最差空间复杂度」**,这是因为内存空间是一项硬性要求,我们必须确保在所有输入数据下都有足够的内存空间预留。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-07 22:32:04 +08:00
|
|
|
|
**最差空间复杂度中的“最差”有两层含义**,分别是输入数据的最差分布和算法运行过程中的最差时间点。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-07-26 08:58:52 +08:00
|
|
|
|
- **以最差输入数据为准**。当 $n < 10$ 时,空间复杂度为 $O(1)$ ;但当 $n > 10$ 时,初始化的数组 `nums` 占用 $O(n)$ 空间;因此最差空间复杂度为 $O(n)$ 。
|
|
|
|
|
- **以算法运行过程中的峰值内存为准**。例如,程序在执行最后一行之前,占用 $O(1)$ 空间;当初始化数组 `nums` 时,程序占用 $O(n)$ 空间;因此最差空间复杂度为 $O(n)$ 。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title=""
|
|
|
|
|
void algorithm(int n) {
|
|
|
|
|
int a = 0; // O(1)
|
|
|
|
|
int[] b = new int[10000]; // O(1)
|
|
|
|
|
if (n > 10)
|
|
|
|
|
int[] nums = new int[n]; // O(n)
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title=""
|
|
|
|
|
void algorithm(int n) {
|
|
|
|
|
int a = 0; // O(1)
|
|
|
|
|
vector<int> b(10000); // O(1)
|
|
|
|
|
if (n > 10)
|
|
|
|
|
vector<int> nums(n); // O(n)
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title=""
|
2023-07-25 16:42:55 +08:00
|
|
|
|
def algorithm(n: int):
|
2023-06-30 04:33:43 +08:00
|
|
|
|
a = 0 # O(1)
|
2023-05-22 22:01:18 +08:00
|
|
|
|
b = [0] * 10000 # O(1)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if n > 10:
|
2023-05-22 22:01:18 +08:00
|
|
|
|
nums = [0] * n # O(n)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title=""
|
|
|
|
|
func algorithm(n int) {
|
|
|
|
|
a := 0 // O(1)
|
|
|
|
|
b := make([]int, 10000) // O(1)
|
|
|
|
|
var nums []int
|
|
|
|
|
if n > 10 {
|
|
|
|
|
nums := make([]int, n) // O(n)
|
|
|
|
|
}
|
|
|
|
|
fmt.Println(a, b, nums)
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:34:46 +08:00
|
|
|
|
=== "JS"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
```javascript title=""
|
|
|
|
|
function algorithm(n) {
|
|
|
|
|
const a = 0; // O(1)
|
|
|
|
|
const b = new Array(10000); // O(1)
|
|
|
|
|
if (n > 10) {
|
|
|
|
|
const nums = new Array(n); // O(n)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:34:46 +08:00
|
|
|
|
=== "TS"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
```typescript title=""
|
|
|
|
|
function algorithm(n: number): void {
|
|
|
|
|
const a = 0; // O(1)
|
|
|
|
|
const b = new Array(10000); // O(1)
|
|
|
|
|
if (n > 10) {
|
|
|
|
|
const nums = new Array(n); // O(n)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title=""
|
2023-04-18 20:19:07 +08:00
|
|
|
|
void algorithm(int n) {
|
|
|
|
|
int a = 0; // O(1)
|
|
|
|
|
int b[10000]; // O(1)
|
|
|
|
|
if (n > 10)
|
2023-07-21 15:14:40 +08:00
|
|
|
|
int nums[n] = {0}; // O(n)
|
2023-04-18 20:19:07 +08:00
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title=""
|
2023-07-25 16:42:55 +08:00
|
|
|
|
void algorithm(int n) {
|
2023-02-08 15:20:18 +08:00
|
|
|
|
int a = 0; // O(1)
|
|
|
|
|
int[] b = new int[10000]; // O(1)
|
2023-07-25 16:42:55 +08:00
|
|
|
|
if (n > 10) {
|
2023-02-08 15:20:18 +08:00
|
|
|
|
int[] nums = new int[n]; // O(n)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title=""
|
|
|
|
|
func algorithm(n: Int) {
|
|
|
|
|
let a = 0 // O(1)
|
|
|
|
|
let b = Array(repeating: 0, count: 10000) // O(1)
|
|
|
|
|
if n > 10 {
|
|
|
|
|
let nums = Array(repeating: 0, count: n) // O(n)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title=""
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2023-06-02 02:38:24 +08:00
|
|
|
|
=== "Dart"
|
|
|
|
|
|
|
|
|
|
```dart title=""
|
2023-06-02 15:07:33 +08:00
|
|
|
|
void algorithm(int n) {
|
|
|
|
|
int a = 0; // O(1)
|
|
|
|
|
List<int> b = List.filled(10000, 0); // O(1)
|
|
|
|
|
if (n > 10) {
|
|
|
|
|
List<int> nums = List.filled(n, 0); // O(n)
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-06-02 02:38:24 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 10:57:40 +08:00
|
|
|
|
=== "Rust"
|
|
|
|
|
|
|
|
|
|
```rust title=""
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2023-04-10 03:12:02 +08:00
|
|
|
|
**在递归函数中,需要注意统计栈帧空间**。例如,函数 `loop()` 在循环中调用了 $n$ 次 `function()` ,每轮中的 `function()` 都返回并释放了栈帧空间,因此空间复杂度仍为 $O(1)$ 。而递归函数 `recur()` 在运行过程中会同时存在 $n$ 个未返回的 `recur()` ,从而占用 $O(n)$ 的栈帧空间。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title=""
|
|
|
|
|
int function() {
|
|
|
|
|
// do something
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
/* 循环 O(1) */
|
|
|
|
|
void loop(int n) {
|
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
|
function();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/* 递归 O(n) */
|
|
|
|
|
void recur(int n) {
|
|
|
|
|
if (n == 1) return;
|
|
|
|
|
return recur(n - 1);
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title=""
|
|
|
|
|
int func() {
|
|
|
|
|
// do something
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
/* 循环 O(1) */
|
|
|
|
|
void loop(int n) {
|
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
|
func();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/* 递归 O(n) */
|
|
|
|
|
void recur(int n) {
|
|
|
|
|
if (n == 1) return;
|
|
|
|
|
return recur(n - 1);
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title=""
|
2023-03-12 18:46:03 +08:00
|
|
|
|
def function() -> int:
|
2023-02-08 15:20:18 +08:00
|
|
|
|
# do something
|
|
|
|
|
return 0
|
|
|
|
|
|
2023-07-25 16:42:55 +08:00
|
|
|
|
def loop(n: int):
|
2023-04-09 05:30:47 +08:00
|
|
|
|
"""循环 O(1)"""
|
2023-02-08 15:20:18 +08:00
|
|
|
|
for _ in range(n):
|
|
|
|
|
function()
|
|
|
|
|
|
2023-03-12 18:46:03 +08:00
|
|
|
|
def recur(n: int) -> int:
|
2023-04-09 05:30:47 +08:00
|
|
|
|
"""递归 O(n)"""
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if n == 1: return
|
|
|
|
|
return recur(n - 1)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title=""
|
|
|
|
|
func function() int {
|
|
|
|
|
// do something
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 循环 O(1) */
|
|
|
|
|
func loop(n int) {
|
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
|
function()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 递归 O(n) */
|
|
|
|
|
func recur(n int) {
|
|
|
|
|
if n == 1 {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
recur(n - 1)
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:34:46 +08:00
|
|
|
|
=== "JS"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
```javascript title=""
|
|
|
|
|
function constFunc() {
|
|
|
|
|
// do something
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
/* 循环 O(1) */
|
|
|
|
|
function loop(n) {
|
|
|
|
|
for (let i = 0; i < n; i++) {
|
|
|
|
|
constFunc();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/* 递归 O(n) */
|
|
|
|
|
function recur(n) {
|
|
|
|
|
if (n === 1) return;
|
|
|
|
|
return recur(n - 1);
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:34:46 +08:00
|
|
|
|
=== "TS"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
```typescript title=""
|
|
|
|
|
function constFunc(): number {
|
|
|
|
|
// do something
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
/* 循环 O(1) */
|
|
|
|
|
function loop(n: number): void {
|
|
|
|
|
for (let i = 0; i < n; i++) {
|
|
|
|
|
constFunc();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/* 递归 O(n) */
|
|
|
|
|
function recur(n: number): void {
|
|
|
|
|
if (n === 1) return;
|
|
|
|
|
return recur(n - 1);
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title=""
|
2023-04-18 20:19:07 +08:00
|
|
|
|
int func() {
|
|
|
|
|
// do something
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
/* 循环 O(1) */
|
|
|
|
|
void loop(int n) {
|
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
|
func();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/* 递归 O(n) */
|
|
|
|
|
void recur(int n) {
|
|
|
|
|
if (n == 1) return;
|
|
|
|
|
return recur(n - 1);
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title=""
|
2023-07-25 16:42:55 +08:00
|
|
|
|
int function() {
|
2023-02-08 15:20:18 +08:00
|
|
|
|
// do something
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
/* 循环 O(1) */
|
2023-07-25 16:42:55 +08:00
|
|
|
|
void loop(int n) {
|
|
|
|
|
for (int i = 0; i < n; i++) {
|
2023-02-08 15:20:18 +08:00
|
|
|
|
function();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/* 递归 O(n) */
|
2023-07-25 16:42:55 +08:00
|
|
|
|
int recur(int n) {
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if (n == 1) return 1;
|
|
|
|
|
return recur(n - 1);
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title=""
|
|
|
|
|
@discardableResult
|
|
|
|
|
func function() -> Int {
|
|
|
|
|
// do something
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 循环 O(1) */
|
|
|
|
|
func loop(n: Int) {
|
|
|
|
|
for _ in 0 ..< n {
|
|
|
|
|
function()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 递归 O(n) */
|
|
|
|
|
func recur(n: Int) {
|
|
|
|
|
if n == 1 {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
recur(n: n - 1)
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title=""
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2023-06-02 02:38:24 +08:00
|
|
|
|
=== "Dart"
|
|
|
|
|
|
|
|
|
|
```dart title=""
|
2023-06-02 15:07:33 +08:00
|
|
|
|
int function() {
|
|
|
|
|
// do something
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
/* 循环 O(1) */
|
|
|
|
|
void loop(int n) {
|
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
|
function();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/* 递归 O(n) */
|
|
|
|
|
void recur(int n) {
|
|
|
|
|
if (n == 1) return;
|
|
|
|
|
return recur(n - 1);
|
|
|
|
|
}
|
2023-06-02 02:38:24 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 10:57:40 +08:00
|
|
|
|
=== "Rust"
|
|
|
|
|
|
|
|
|
|
```rust title=""
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2023-02-24 18:45:27 +08:00
|
|
|
|
## 2.3.3. 常见类型
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
设输入数据大小为 $n$ ,常见的空间复杂度类型有(从低到高排列)
|
|
|
|
|
|
|
|
|
|
$$
|
|
|
|
|
\begin{aligned}
|
|
|
|
|
O(1) < O(\log n) < O(n) < O(n^2) < O(2^n) \newline
|
|
|
|
|
\text{常数阶} < \text{对数阶} < \text{线性阶} < \text{平方阶} < \text{指数阶}
|
|
|
|
|
\end{aligned}
|
|
|
|
|
$$
|
|
|
|
|
|
2023-02-26 18:17:57 +08:00
|
|
|
|
![空间复杂度的常见类型](space_complexity.assets/space_complexity_common_types.png)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-26 19:53:26 +08:00
|
|
|
|
<p align="center"> Fig. 空间复杂度的常见类型 </p>
|
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
!!! tip
|
|
|
|
|
|
2023-04-07 22:32:04 +08:00
|
|
|
|
部分示例代码需要一些前置知识,包括数组、链表、二叉树、递归算法等。如果遇到看不懂的地方无需担心,可以在学习完后面章节后再来复习,现阶段我们先专注于理解空间复杂度的含义和推算方法。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
### 常数阶 $O(1)$
|
|
|
|
|
|
|
|
|
|
常数阶常见于数量与输入数据大小 $n$ 无关的常量、变量、对象。
|
|
|
|
|
|
|
|
|
|
需要注意的是,在循环中初始化变量或调用函数而占用的内存,在进入下一循环后就会被释放,即不会累积占用空间,空间复杂度仍为 $O(1)$ 。
|
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="space_complexity.java"
|
|
|
|
|
/* 常数阶 */
|
|
|
|
|
void constant(int n) {
|
|
|
|
|
// 常量、变量、对象占用 O(1) 空间
|
|
|
|
|
final int a = 0;
|
|
|
|
|
int b = 0;
|
|
|
|
|
int[] nums = new int[10000];
|
|
|
|
|
ListNode node = new ListNode(0);
|
|
|
|
|
// 循环中的变量占用 O(1) 空间
|
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
|
int c = 0;
|
|
|
|
|
}
|
|
|
|
|
// 循环中的函数占用 O(1) 空间
|
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
|
function();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="space_complexity.cpp"
|
|
|
|
|
/* 常数阶 */
|
|
|
|
|
void constant(int n) {
|
|
|
|
|
// 常量、变量、对象占用 O(1) 空间
|
|
|
|
|
const int a = 0;
|
|
|
|
|
int b = 0;
|
|
|
|
|
vector<int> nums(10000);
|
|
|
|
|
ListNode node(0);
|
|
|
|
|
// 循环中的变量占用 O(1) 空间
|
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
|
int c = 0;
|
|
|
|
|
}
|
|
|
|
|
// 循环中的函数占用 O(1) 空间
|
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
|
func();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="space_complexity.py"
|
2023-07-25 16:42:55 +08:00
|
|
|
|
def constant(n: int):
|
2023-04-09 05:12:22 +08:00
|
|
|
|
"""常数阶"""
|
2023-02-08 15:20:18 +08:00
|
|
|
|
# 常量、变量、对象占用 O(1) 空间
|
2023-05-22 22:01:18 +08:00
|
|
|
|
a = 0
|
|
|
|
|
nums = [0] * 10000
|
2023-02-08 15:20:18 +08:00
|
|
|
|
node = ListNode(0)
|
|
|
|
|
# 循环中的变量占用 O(1) 空间
|
|
|
|
|
for _ in range(n):
|
2023-05-22 22:01:18 +08:00
|
|
|
|
c = 0
|
2023-02-08 15:20:18 +08:00
|
|
|
|
# 循环中的函数占用 O(1) 空间
|
|
|
|
|
for _ in range(n):
|
|
|
|
|
function()
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="space_complexity.go"
|
|
|
|
|
/* 常数阶 */
|
|
|
|
|
func spaceConstant(n int) {
|
|
|
|
|
// 常量、变量、对象占用 O(1) 空间
|
|
|
|
|
const a = 0
|
|
|
|
|
b := 0
|
|
|
|
|
nums := make([]int, 10000)
|
|
|
|
|
ListNode := newNode(0)
|
|
|
|
|
// 循环中的变量占用 O(1) 空间
|
|
|
|
|
var c int
|
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
|
c = 0
|
|
|
|
|
}
|
|
|
|
|
// 循环中的函数占用 O(1) 空间
|
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
|
function()
|
|
|
|
|
}
|
|
|
|
|
fmt.Println(a, b, nums, c, ListNode)
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:34:46 +08:00
|
|
|
|
=== "JS"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
```javascript title="space_complexity.js"
|
|
|
|
|
/* 常数阶 */
|
|
|
|
|
function constant(n) {
|
|
|
|
|
// 常量、变量、对象占用 O(1) 空间
|
|
|
|
|
const a = 0;
|
|
|
|
|
const b = 0;
|
|
|
|
|
const nums = new Array(10000);
|
|
|
|
|
const node = new ListNode(0);
|
|
|
|
|
// 循环中的变量占用 O(1) 空间
|
|
|
|
|
for (let i = 0; i < n; i++) {
|
|
|
|
|
const c = 0;
|
|
|
|
|
}
|
|
|
|
|
// 循环中的函数占用 O(1) 空间
|
|
|
|
|
for (let i = 0; i < n; i++) {
|
|
|
|
|
constFunc();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:34:46 +08:00
|
|
|
|
=== "TS"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
```typescript title="space_complexity.ts"
|
|
|
|
|
/* 常数阶 */
|
|
|
|
|
function constant(n: number): void {
|
|
|
|
|
// 常量、变量、对象占用 O(1) 空间
|
|
|
|
|
const a = 0;
|
|
|
|
|
const b = 0;
|
|
|
|
|
const nums = new Array(10000);
|
|
|
|
|
const node = new ListNode(0);
|
|
|
|
|
// 循环中的变量占用 O(1) 空间
|
|
|
|
|
for (let i = 0; i < n; i++) {
|
|
|
|
|
const c = 0;
|
|
|
|
|
}
|
|
|
|
|
// 循环中的函数占用 O(1) 空间
|
|
|
|
|
for (let i = 0; i < n; i++) {
|
|
|
|
|
constFunc();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="space_complexity.c"
|
2023-04-18 20:19:07 +08:00
|
|
|
|
/* 常数阶 */
|
|
|
|
|
void constant(int n) {
|
|
|
|
|
// 常量、变量、对象占用 O(1) 空间
|
|
|
|
|
const int a = 0;
|
|
|
|
|
int b = 0;
|
|
|
|
|
int nums[1000];
|
|
|
|
|
ListNode *node = newListNode(0);
|
|
|
|
|
free(node);
|
|
|
|
|
// 循环中的变量占用 O(1) 空间
|
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
|
int c = 0;
|
|
|
|
|
}
|
|
|
|
|
// 循环中的函数占用 O(1) 空间
|
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
|
func();
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="space_complexity.cs"
|
|
|
|
|
/* 常数阶 */
|
2023-04-23 14:58:03 +08:00
|
|
|
|
void constant(int n) {
|
2023-02-08 15:20:18 +08:00
|
|
|
|
// 常量、变量、对象占用 O(1) 空间
|
|
|
|
|
int a = 0;
|
|
|
|
|
int b = 0;
|
|
|
|
|
int[] nums = new int[10000];
|
|
|
|
|
ListNode node = new ListNode(0);
|
|
|
|
|
// 循环中的变量占用 O(1) 空间
|
2023-04-23 14:58:03 +08:00
|
|
|
|
for (int i = 0; i < n; i++) {
|
2023-02-08 15:20:18 +08:00
|
|
|
|
int c = 0;
|
|
|
|
|
}
|
|
|
|
|
// 循环中的函数占用 O(1) 空间
|
2023-04-23 14:58:03 +08:00
|
|
|
|
for (int i = 0; i < n; i++) {
|
2023-02-08 15:20:18 +08:00
|
|
|
|
function();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="space_complexity.swift"
|
|
|
|
|
/* 常数阶 */
|
|
|
|
|
func constant(n: Int) {
|
|
|
|
|
// 常量、变量、对象占用 O(1) 空间
|
|
|
|
|
let a = 0
|
|
|
|
|
var b = 0
|
|
|
|
|
let nums = Array(repeating: 0, count: 10000)
|
|
|
|
|
let node = ListNode(x: 0)
|
|
|
|
|
// 循环中的变量占用 O(1) 空间
|
|
|
|
|
for _ in 0 ..< n {
|
|
|
|
|
let c = 0
|
|
|
|
|
}
|
|
|
|
|
// 循环中的函数占用 O(1) 空间
|
|
|
|
|
for _ in 0 ..< n {
|
|
|
|
|
function()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="space_complexity.zig"
|
|
|
|
|
// 常数阶
|
|
|
|
|
fn constant(n: i32) void {
|
|
|
|
|
// 常量、变量、对象占用 O(1) 空间
|
|
|
|
|
const a: i32 = 0;
|
|
|
|
|
var b: i32 = 0;
|
|
|
|
|
var nums = [_]i32{0}**10000;
|
|
|
|
|
var node = inc.ListNode(i32){.val = 0};
|
|
|
|
|
var i: i32 = 0;
|
|
|
|
|
// 循环中的变量占用 O(1) 空间
|
|
|
|
|
while (i < n) : (i += 1) {
|
|
|
|
|
var c: i32 = 0;
|
|
|
|
|
_ = c;
|
|
|
|
|
}
|
|
|
|
|
// 循环中的函数占用 O(1) 空间
|
|
|
|
|
i = 0;
|
|
|
|
|
while (i < n) : (i += 1) {
|
|
|
|
|
_ = function();
|
|
|
|
|
}
|
|
|
|
|
_ = a;
|
|
|
|
|
_ = b;
|
|
|
|
|
_ = nums;
|
|
|
|
|
_ = node;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-06-02 02:38:24 +08:00
|
|
|
|
=== "Dart"
|
|
|
|
|
|
|
|
|
|
```dart title="space_complexity.dart"
|
|
|
|
|
/* 常数阶 */
|
|
|
|
|
void constant(int n) {
|
|
|
|
|
// 常量、变量、对象占用 O(1) 空间
|
|
|
|
|
final int a = 0;
|
|
|
|
|
int b = 0;
|
|
|
|
|
|
|
|
|
|
List<int> nums = List.filled(10000, 0);
|
|
|
|
|
// 循环中的变量占用 O(1) 空间
|
|
|
|
|
for (var i = 0; i < n; i++) {
|
|
|
|
|
int c = 0;
|
|
|
|
|
}
|
|
|
|
|
// 循环中的函数占用 O(1) 空间
|
|
|
|
|
for (var i = 0; i < n; i++) {
|
|
|
|
|
function();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 10:57:40 +08:00
|
|
|
|
=== "Rust"
|
|
|
|
|
|
|
|
|
|
```rust title="space_complexity.rs"
|
|
|
|
|
/* 常数阶 */
|
|
|
|
|
#[allow(unused)]
|
|
|
|
|
fn constant(n: i32) {
|
|
|
|
|
// 常量、变量、对象占用 O(1) 空间
|
|
|
|
|
const A: i32 = 0;
|
|
|
|
|
let b = 0;
|
|
|
|
|
let nums = vec![0; 10000];
|
|
|
|
|
let node = ListNode::new(0);
|
|
|
|
|
// 循环中的变量占用 O(1) 空间
|
|
|
|
|
for i in 0..n {
|
|
|
|
|
let c = 0;
|
|
|
|
|
}
|
|
|
|
|
// 循环中的函数占用 O(1) 空间
|
|
|
|
|
for i in 0..n {
|
|
|
|
|
function();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
### 线性阶 $O(n)$
|
|
|
|
|
|
|
|
|
|
线性阶常见于元素数量与 $n$ 成正比的数组、链表、栈、队列等。
|
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="space_complexity.java"
|
|
|
|
|
/* 线性阶 */
|
|
|
|
|
void linear(int n) {
|
|
|
|
|
// 长度为 n 的数组占用 O(n) 空间
|
|
|
|
|
int[] nums = new int[n];
|
|
|
|
|
// 长度为 n 的列表占用 O(n) 空间
|
|
|
|
|
List<ListNode> nodes = new ArrayList<>();
|
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
|
nodes.add(new ListNode(i));
|
|
|
|
|
}
|
|
|
|
|
// 长度为 n 的哈希表占用 O(n) 空间
|
|
|
|
|
Map<Integer, String> map = new HashMap<>();
|
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
|
map.put(i, String.valueOf(i));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="space_complexity.cpp"
|
|
|
|
|
/* 线性阶 */
|
|
|
|
|
void linear(int n) {
|
|
|
|
|
// 长度为 n 的数组占用 O(n) 空间
|
|
|
|
|
vector<int> nums(n);
|
|
|
|
|
// 长度为 n 的列表占用 O(n) 空间
|
|
|
|
|
vector<ListNode> nodes;
|
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
|
nodes.push_back(ListNode(i));
|
|
|
|
|
}
|
|
|
|
|
// 长度为 n 的哈希表占用 O(n) 空间
|
|
|
|
|
unordered_map<int, string> map;
|
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
|
map[i] = to_string(i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="space_complexity.py"
|
2023-07-25 16:42:55 +08:00
|
|
|
|
def linear(n: int):
|
2023-04-09 05:12:22 +08:00
|
|
|
|
"""线性阶"""
|
2023-02-08 15:20:18 +08:00
|
|
|
|
# 长度为 n 的列表占用 O(n) 空间
|
2023-05-22 22:01:18 +08:00
|
|
|
|
nums = [0] * n
|
2023-02-08 15:20:18 +08:00
|
|
|
|
# 长度为 n 的哈希表占用 O(n) 空间
|
2023-03-23 18:56:56 +08:00
|
|
|
|
mapp = dict[int, str]()
|
2023-02-08 15:20:18 +08:00
|
|
|
|
for i in range(n):
|
|
|
|
|
mapp[i] = str(i)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="space_complexity.go"
|
|
|
|
|
/* 线性阶 */
|
|
|
|
|
func spaceLinear(n int) {
|
|
|
|
|
// 长度为 n 的数组占用 O(n) 空间
|
|
|
|
|
_ = make([]int, n)
|
|
|
|
|
// 长度为 n 的列表占用 O(n) 空间
|
|
|
|
|
var nodes []*node
|
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
|
nodes = append(nodes, newNode(i))
|
|
|
|
|
}
|
|
|
|
|
// 长度为 n 的哈希表占用 O(n) 空间
|
|
|
|
|
m := make(map[int]string, n)
|
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
|
m[i] = strconv.Itoa(i)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:34:46 +08:00
|
|
|
|
=== "JS"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
```javascript title="space_complexity.js"
|
|
|
|
|
/* 线性阶 */
|
|
|
|
|
function linear(n) {
|
|
|
|
|
// 长度为 n 的数组占用 O(n) 空间
|
|
|
|
|
const nums = new Array(n);
|
|
|
|
|
// 长度为 n 的列表占用 O(n) 空间
|
|
|
|
|
const nodes = [];
|
|
|
|
|
for (let i = 0; i < n; i++) {
|
|
|
|
|
nodes.push(new ListNode(i));
|
|
|
|
|
}
|
|
|
|
|
// 长度为 n 的哈希表占用 O(n) 空间
|
|
|
|
|
const map = new Map();
|
|
|
|
|
for (let i = 0; i < n; i++) {
|
|
|
|
|
map.set(i, i.toString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:34:46 +08:00
|
|
|
|
=== "TS"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
```typescript title="space_complexity.ts"
|
|
|
|
|
/* 线性阶 */
|
|
|
|
|
function linear(n: number): void {
|
|
|
|
|
// 长度为 n 的数组占用 O(n) 空间
|
|
|
|
|
const nums = new Array(n);
|
|
|
|
|
// 长度为 n 的列表占用 O(n) 空间
|
|
|
|
|
const nodes: ListNode[] = [];
|
|
|
|
|
for (let i = 0; i < n; i++) {
|
|
|
|
|
nodes.push(new ListNode(i));
|
|
|
|
|
}
|
|
|
|
|
// 长度为 n 的哈希表占用 O(n) 空间
|
|
|
|
|
const map = new Map();
|
|
|
|
|
for (let i = 0; i < n; i++) {
|
|
|
|
|
map.set(i, i.toString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="space_complexity.c"
|
2023-04-18 21:18:58 +08:00
|
|
|
|
/* 哈希表 */
|
|
|
|
|
struct hashTable {
|
|
|
|
|
int key;
|
|
|
|
|
int val;
|
|
|
|
|
UT_hash_handle hh; // 基于 uthash.h 实现
|
|
|
|
|
};
|
|
|
|
|
|
2023-04-22 01:35:51 +08:00
|
|
|
|
typedef struct hashTable hashTable;
|
|
|
|
|
|
2023-04-18 20:19:07 +08:00
|
|
|
|
/* 线性阶 */
|
|
|
|
|
void linear(int n) {
|
|
|
|
|
// 长度为 n 的数组占用 O(n) 空间
|
|
|
|
|
int *nums = malloc(sizeof(int) * n);
|
|
|
|
|
free(nums);
|
|
|
|
|
|
|
|
|
|
// 长度为 n 的列表占用 O(n) 空间
|
|
|
|
|
ListNode **nodes = malloc(sizeof(ListNode *) * n);
|
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
|
nodes[i] = newListNode(i);
|
|
|
|
|
}
|
|
|
|
|
// 内存释放
|
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
|
free(nodes[i]);
|
|
|
|
|
}
|
|
|
|
|
free(nodes);
|
|
|
|
|
|
|
|
|
|
// 长度为 n 的哈希表占用 O(n) 空间
|
|
|
|
|
hashTable *h = NULL;
|
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
|
hashTable *tmp = malloc(sizeof(hashTable));
|
|
|
|
|
tmp->key = i;
|
|
|
|
|
tmp->val = i;
|
|
|
|
|
HASH_ADD_INT(h, key, tmp);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 内存释放
|
|
|
|
|
hashTable *curr, *tmp;
|
|
|
|
|
HASH_ITER(hh, h, curr, tmp) {
|
|
|
|
|
HASH_DEL(h, curr);
|
|
|
|
|
free(curr);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="space_complexity.cs"
|
|
|
|
|
/* 线性阶 */
|
2023-04-23 14:58:03 +08:00
|
|
|
|
void linear(int n) {
|
2023-02-08 15:20:18 +08:00
|
|
|
|
// 长度为 n 的数组占用 O(n) 空间
|
|
|
|
|
int[] nums = new int[n];
|
|
|
|
|
// 长度为 n 的列表占用 O(n) 空间
|
|
|
|
|
List<ListNode> nodes = new();
|
2023-04-23 14:58:03 +08:00
|
|
|
|
for (int i = 0; i < n; i++) {
|
2023-02-08 15:20:18 +08:00
|
|
|
|
nodes.Add(new ListNode(i));
|
|
|
|
|
}
|
|
|
|
|
// 长度为 n 的哈希表占用 O(n) 空间
|
2023-04-22 01:35:51 +08:00
|
|
|
|
Dictionary<int, string> map = new();
|
2023-04-23 14:58:03 +08:00
|
|
|
|
for (int i = 0; i < n; i++) {
|
2023-02-08 15:20:18 +08:00
|
|
|
|
map.Add(i, i.ToString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="space_complexity.swift"
|
|
|
|
|
/* 线性阶 */
|
|
|
|
|
func linear(n: Int) {
|
|
|
|
|
// 长度为 n 的数组占用 O(n) 空间
|
|
|
|
|
let nums = Array(repeating: 0, count: n)
|
|
|
|
|
// 长度为 n 的列表占用 O(n) 空间
|
|
|
|
|
let nodes = (0 ..< n).map { ListNode(x: $0) }
|
|
|
|
|
// 长度为 n 的哈希表占用 O(n) 空间
|
|
|
|
|
let map = Dictionary(uniqueKeysWithValues: (0 ..< n).map { ($0, "\($0)") })
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="space_complexity.zig"
|
|
|
|
|
// 线性阶
|
|
|
|
|
fn linear(comptime n: i32) !void {
|
|
|
|
|
// 长度为 n 的数组占用 O(n) 空间
|
|
|
|
|
var nums = [_]i32{0}**n;
|
|
|
|
|
// 长度为 n 的列表占用 O(n) 空间
|
|
|
|
|
var nodes = std.ArrayList(i32).init(std.heap.page_allocator);
|
|
|
|
|
defer nodes.deinit();
|
|
|
|
|
var i: i32 = 0;
|
|
|
|
|
while (i < n) : (i += 1) {
|
|
|
|
|
try nodes.append(i);
|
|
|
|
|
}
|
|
|
|
|
// 长度为 n 的哈希表占用 O(n) 空间
|
|
|
|
|
var map = std.AutoArrayHashMap(i32, []const u8).init(std.heap.page_allocator);
|
|
|
|
|
defer map.deinit();
|
|
|
|
|
var j: i32 = 0;
|
|
|
|
|
while (j < n) : (j += 1) {
|
|
|
|
|
const string = try std.fmt.allocPrint(std.heap.page_allocator, "{d}", .{j});
|
|
|
|
|
defer std.heap.page_allocator.free(string);
|
|
|
|
|
try map.put(i, string);
|
|
|
|
|
}
|
|
|
|
|
_ = nums;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-06-02 02:38:24 +08:00
|
|
|
|
=== "Dart"
|
|
|
|
|
|
|
|
|
|
```dart title="space_complexity.dart"
|
|
|
|
|
/* 线性阶 */
|
|
|
|
|
void linear(int n) {
|
|
|
|
|
// 长度为 n 的数组占用 O(n) 空间
|
|
|
|
|
List<int> nums = List.filled(n, 0);
|
|
|
|
|
// 长度为 n 的列表占用 O(n) 空间
|
|
|
|
|
List<ListNode> nodes = [];
|
|
|
|
|
for (var i = 0; i < n; i++) {
|
|
|
|
|
nodes.add(ListNode(i));
|
|
|
|
|
}
|
|
|
|
|
// 长度为 n 的哈希表占用 O(n) 空间
|
|
|
|
|
Map<int, String> map = HashMap();
|
|
|
|
|
for (var i = 0; i < n; i++) {
|
|
|
|
|
map.putIfAbsent(i, () => i.toString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 10:57:40 +08:00
|
|
|
|
=== "Rust"
|
|
|
|
|
|
|
|
|
|
```rust title="space_complexity.rs"
|
|
|
|
|
/* 线性阶 */
|
|
|
|
|
#[allow(unused)]
|
|
|
|
|
fn linear(n: i32) {
|
|
|
|
|
// 长度为 n 的数组占用 O(n) 空间
|
|
|
|
|
let mut nums = vec![0; n as usize];
|
|
|
|
|
// 长度为 n 的列表占用 O(n) 空间
|
|
|
|
|
let mut nodes = Vec::new();
|
|
|
|
|
for i in 0..n {
|
|
|
|
|
nodes.push(ListNode::new(i))
|
|
|
|
|
}
|
|
|
|
|
// 长度为 n 的哈希表占用 O(n) 空间
|
|
|
|
|
let mut map = HashMap::new();
|
|
|
|
|
for i in 0..n {
|
|
|
|
|
map.insert(i, i.to_string());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
以下递归函数会同时存在 $n$ 个未返回的 `algorithm()` 函数,使用 $O(n)$ 大小的栈帧空间。
|
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="space_complexity.java"
|
|
|
|
|
/* 线性阶(递归实现) */
|
|
|
|
|
void linearRecur(int n) {
|
|
|
|
|
System.out.println("递归 n = " + n);
|
2023-04-14 04:01:38 +08:00
|
|
|
|
if (n == 1)
|
|
|
|
|
return;
|
2023-02-08 15:20:18 +08:00
|
|
|
|
linearRecur(n - 1);
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="space_complexity.cpp"
|
|
|
|
|
/* 线性阶(递归实现) */
|
|
|
|
|
void linearRecur(int n) {
|
|
|
|
|
cout << "递归 n = " << n << endl;
|
2023-04-14 04:01:38 +08:00
|
|
|
|
if (n == 1)
|
|
|
|
|
return;
|
2023-02-08 15:20:18 +08:00
|
|
|
|
linearRecur(n - 1);
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="space_complexity.py"
|
2023-07-25 16:42:55 +08:00
|
|
|
|
def linear_recur(n: int):
|
2023-04-09 05:12:22 +08:00
|
|
|
|
"""线性阶(递归实现)"""
|
2023-02-08 15:20:18 +08:00
|
|
|
|
print("递归 n =", n)
|
2023-04-09 05:12:22 +08:00
|
|
|
|
if n == 1:
|
|
|
|
|
return
|
2023-02-08 15:20:18 +08:00
|
|
|
|
linear_recur(n - 1)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="space_complexity.go"
|
|
|
|
|
/* 线性阶(递归实现) */
|
|
|
|
|
func spaceLinearRecur(n int) {
|
|
|
|
|
fmt.Println("递归 n =", n)
|
|
|
|
|
if n == 1 {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
spaceLinearRecur(n - 1)
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:34:46 +08:00
|
|
|
|
=== "JS"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
```javascript title="space_complexity.js"
|
|
|
|
|
/* 线性阶(递归实现) */
|
|
|
|
|
function linearRecur(n) {
|
|
|
|
|
console.log(`递归 n = ${n}`);
|
|
|
|
|
if (n === 1) return;
|
|
|
|
|
linearRecur(n - 1);
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:34:46 +08:00
|
|
|
|
=== "TS"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
```typescript title="space_complexity.ts"
|
|
|
|
|
/* 线性阶(递归实现) */
|
|
|
|
|
function linearRecur(n: number): void {
|
|
|
|
|
console.log(`递归 n = ${n}`);
|
|
|
|
|
if (n === 1) return;
|
|
|
|
|
linearRecur(n - 1);
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="space_complexity.c"
|
2023-04-18 20:19:07 +08:00
|
|
|
|
/* 线性阶(递归实现) */
|
|
|
|
|
void linearRecur(int n) {
|
|
|
|
|
printf("递归 n = %d\r\n", n);
|
|
|
|
|
if (n == 1)
|
|
|
|
|
return;
|
|
|
|
|
linearRecur(n - 1);
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="space_complexity.cs"
|
|
|
|
|
/* 线性阶(递归实现) */
|
2023-04-23 14:58:03 +08:00
|
|
|
|
void linearRecur(int n) {
|
2023-02-08 15:20:18 +08:00
|
|
|
|
Console.WriteLine("递归 n = " + n);
|
|
|
|
|
if (n == 1) return;
|
|
|
|
|
linearRecur(n - 1);
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="space_complexity.swift"
|
|
|
|
|
/* 线性阶(递归实现) */
|
|
|
|
|
func linearRecur(n: Int) {
|
|
|
|
|
print("递归 n = \(n)")
|
|
|
|
|
if n == 1 {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
linearRecur(n: n - 1)
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="space_complexity.zig"
|
|
|
|
|
// 线性阶(递归实现)
|
|
|
|
|
fn linearRecur(comptime n: i32) void {
|
|
|
|
|
std.debug.print("递归 n = {}\n", .{n});
|
|
|
|
|
if (n == 1) return;
|
|
|
|
|
linearRecur(n - 1);
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-06-02 02:38:24 +08:00
|
|
|
|
=== "Dart"
|
|
|
|
|
|
|
|
|
|
```dart title="space_complexity.dart"
|
|
|
|
|
/* 线性阶(递归实现) */
|
|
|
|
|
void linearRecur(int n) {
|
|
|
|
|
print('递归 n = $n');
|
|
|
|
|
if (n == 1) return;
|
|
|
|
|
linearRecur(n - 1);
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 10:57:40 +08:00
|
|
|
|
=== "Rust"
|
|
|
|
|
|
|
|
|
|
```rust title="space_complexity.rs"
|
|
|
|
|
/* 线性阶(递归实现) */
|
|
|
|
|
fn linear_recur(n: i32) {
|
|
|
|
|
println!("递归 n = {}", n);
|
|
|
|
|
if n == 1 {return};
|
|
|
|
|
linear_recur(n - 1);
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-02-26 18:17:57 +08:00
|
|
|
|
![递归函数产生的线性阶空间复杂度](space_complexity.assets/space_complexity_recursive_linear.png)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-26 19:53:26 +08:00
|
|
|
|
<p align="center"> Fig. 递归函数产生的线性阶空间复杂度 </p>
|
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
### 平方阶 $O(n^2)$
|
|
|
|
|
|
2023-04-07 22:32:04 +08:00
|
|
|
|
平方阶常见于矩阵和图,元素数量与 $n$ 成平方关系。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="space_complexity.java"
|
|
|
|
|
/* 平方阶 */
|
|
|
|
|
void quadratic(int n) {
|
|
|
|
|
// 矩阵占用 O(n^2) 空间
|
|
|
|
|
int[][] numMatrix = new int[n][n];
|
|
|
|
|
// 二维列表占用 O(n^2) 空间
|
|
|
|
|
List<List<Integer>> numList = new ArrayList<>();
|
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
|
List<Integer> tmp = new ArrayList<>();
|
|
|
|
|
for (int j = 0; j < n; j++) {
|
|
|
|
|
tmp.add(0);
|
|
|
|
|
}
|
|
|
|
|
numList.add(tmp);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="space_complexity.cpp"
|
|
|
|
|
/* 平方阶 */
|
|
|
|
|
void quadratic(int n) {
|
|
|
|
|
// 二维列表占用 O(n^2) 空间
|
|
|
|
|
vector<vector<int>> numMatrix;
|
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
|
vector<int> tmp;
|
|
|
|
|
for (int j = 0; j < n; j++) {
|
|
|
|
|
tmp.push_back(0);
|
|
|
|
|
}
|
|
|
|
|
numMatrix.push_back(tmp);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="space_complexity.py"
|
2023-07-25 16:42:55 +08:00
|
|
|
|
def quadratic(n: int):
|
2023-04-09 05:12:22 +08:00
|
|
|
|
"""平方阶"""
|
2023-02-08 15:20:18 +08:00
|
|
|
|
# 二维列表占用 O(n^2) 空间
|
2023-05-22 22:01:18 +08:00
|
|
|
|
num_matrix = [[0] * n for _ in range(n)]
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="space_complexity.go"
|
|
|
|
|
/* 平方阶 */
|
|
|
|
|
func spaceQuadratic(n int) {
|
|
|
|
|
// 矩阵占用 O(n^2) 空间
|
|
|
|
|
numMatrix := make([][]int, n)
|
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
|
numMatrix[i] = make([]int, n)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:34:46 +08:00
|
|
|
|
=== "JS"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
```javascript title="space_complexity.js"
|
|
|
|
|
/* 平方阶 */
|
|
|
|
|
function quadratic(n) {
|
|
|
|
|
// 矩阵占用 O(n^2) 空间
|
2023-04-17 21:57:42 +08:00
|
|
|
|
const numMatrix = Array(n)
|
|
|
|
|
.fill(null)
|
|
|
|
|
.map(() => Array(n).fill(null));
|
2023-02-08 15:20:18 +08:00
|
|
|
|
// 二维列表占用 O(n^2) 空间
|
|
|
|
|
const numList = [];
|
|
|
|
|
for (let i = 0; i < n; i++) {
|
|
|
|
|
const tmp = [];
|
|
|
|
|
for (let j = 0; j < n; j++) {
|
|
|
|
|
tmp.push(0);
|
|
|
|
|
}
|
|
|
|
|
numList.push(tmp);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:34:46 +08:00
|
|
|
|
=== "TS"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
```typescript title="space_complexity.ts"
|
|
|
|
|
/* 平方阶 */
|
|
|
|
|
function quadratic(n: number): void {
|
|
|
|
|
// 矩阵占用 O(n^2) 空间
|
2023-03-03 02:46:12 +08:00
|
|
|
|
const numMatrix = Array(n)
|
|
|
|
|
.fill(null)
|
|
|
|
|
.map(() => Array(n).fill(null));
|
2023-02-08 15:20:18 +08:00
|
|
|
|
// 二维列表占用 O(n^2) 空间
|
|
|
|
|
const numList = [];
|
|
|
|
|
for (let i = 0; i < n; i++) {
|
|
|
|
|
const tmp = [];
|
|
|
|
|
for (let j = 0; j < n; j++) {
|
|
|
|
|
tmp.push(0);
|
|
|
|
|
}
|
|
|
|
|
numList.push(tmp);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="space_complexity.c"
|
2023-04-18 20:19:07 +08:00
|
|
|
|
/* 平方阶 */
|
|
|
|
|
void quadratic(int n) {
|
|
|
|
|
// 二维列表占用 O(n^2) 空间
|
|
|
|
|
int **numMatrix = malloc(sizeof(int *) * n);
|
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
|
int *tmp = malloc(sizeof(int) * n);
|
|
|
|
|
for (int j = 0; j < n; j++) {
|
|
|
|
|
tmp[j] = 0;
|
|
|
|
|
}
|
|
|
|
|
numMatrix[i] = tmp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 内存释放
|
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
|
free(numMatrix[i]);
|
|
|
|
|
}
|
|
|
|
|
free(numMatrix);
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="space_complexity.cs"
|
|
|
|
|
/* 平方阶 */
|
2023-04-23 14:58:03 +08:00
|
|
|
|
void quadratic(int n) {
|
2023-02-08 15:20:18 +08:00
|
|
|
|
// 矩阵占用 O(n^2) 空间
|
|
|
|
|
int[,] numMatrix = new int[n, n];
|
|
|
|
|
// 二维列表占用 O(n^2) 空间
|
|
|
|
|
List<List<int>> numList = new();
|
2023-04-23 14:58:03 +08:00
|
|
|
|
for (int i = 0; i < n; i++) {
|
2023-02-08 15:20:18 +08:00
|
|
|
|
List<int> tmp = new();
|
2023-04-23 14:58:03 +08:00
|
|
|
|
for (int j = 0; j < n; j++) {
|
2023-02-08 15:20:18 +08:00
|
|
|
|
tmp.Add(0);
|
|
|
|
|
}
|
|
|
|
|
numList.Add(tmp);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="space_complexity.swift"
|
|
|
|
|
/* 平方阶 */
|
|
|
|
|
func quadratic(n: Int) {
|
|
|
|
|
// 二维列表占用 O(n^2) 空间
|
|
|
|
|
let numList = Array(repeating: Array(repeating: 0, count: n), count: n)
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="space_complexity.zig"
|
|
|
|
|
// 平方阶
|
|
|
|
|
fn quadratic(n: i32) !void {
|
|
|
|
|
// 二维列表占用 O(n^2) 空间
|
|
|
|
|
var nodes = std.ArrayList(std.ArrayList(i32)).init(std.heap.page_allocator);
|
|
|
|
|
defer nodes.deinit();
|
|
|
|
|
var i: i32 = 0;
|
|
|
|
|
while (i < n) : (i += 1) {
|
|
|
|
|
var tmp = std.ArrayList(i32).init(std.heap.page_allocator);
|
|
|
|
|
defer tmp.deinit();
|
|
|
|
|
var j: i32 = 0;
|
|
|
|
|
while (j < n) : (j += 1) {
|
|
|
|
|
try tmp.append(0);
|
|
|
|
|
}
|
|
|
|
|
try nodes.append(tmp);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-06-02 02:38:24 +08:00
|
|
|
|
=== "Dart"
|
|
|
|
|
|
|
|
|
|
```dart title="space_complexity.dart"
|
|
|
|
|
/* 平方阶 */
|
|
|
|
|
void quadratic(int n) {
|
|
|
|
|
// 矩阵占用 O(n^2) 空间
|
|
|
|
|
List<List<int>> numMatrix = List.generate(n, (_) => List.filled(n, 0));
|
|
|
|
|
// 二维列表占用 O(n^2) 空间
|
|
|
|
|
List<List<int>> numList = [];
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < n; i++) {
|
|
|
|
|
List<int> tmp = [];
|
|
|
|
|
for (int j = 0; j < n; j++) {
|
|
|
|
|
tmp.add(0);
|
|
|
|
|
}
|
|
|
|
|
numList.add(tmp);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 10:57:40 +08:00
|
|
|
|
=== "Rust"
|
|
|
|
|
|
|
|
|
|
```rust title="space_complexity.rs"
|
|
|
|
|
/* 平方阶 */
|
|
|
|
|
#[allow(unused)]
|
|
|
|
|
fn quadratic(n: i32) {
|
|
|
|
|
// 矩阵占用 O(n^2) 空间
|
|
|
|
|
let num_matrix = vec![vec![0; n as usize]; n as usize];
|
|
|
|
|
// 二维列表占用 O(n^2) 空间
|
|
|
|
|
let mut num_list = Vec::new();
|
|
|
|
|
for i in 0..n {
|
|
|
|
|
let mut tmp = Vec::new();
|
|
|
|
|
for j in 0..n {
|
|
|
|
|
tmp.push(0);
|
|
|
|
|
}
|
|
|
|
|
num_list.push(tmp);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-04-10 03:12:02 +08:00
|
|
|
|
在以下递归函数中,同时存在 $n$ 个未返回的 `algorithm()` ,并且每个函数中都初始化了一个数组,长度分别为 $n, n-1, n-2, ..., 2, 1$ ,平均长度为 $\frac{n}{2}$ ,因此总体占用 $O(n^2)$ 空间。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="space_complexity.java"
|
|
|
|
|
/* 平方阶(递归实现) */
|
|
|
|
|
int quadraticRecur(int n) {
|
2023-04-14 04:01:38 +08:00
|
|
|
|
if (n <= 0)
|
|
|
|
|
return 0;
|
2023-02-08 15:20:18 +08:00
|
|
|
|
// 数组 nums 长度为 n, n-1, ..., 2, 1
|
|
|
|
|
int[] nums = new int[n];
|
|
|
|
|
System.out.println("递归 n = " + n + " 中的 nums 长度 = " + nums.length);
|
|
|
|
|
return quadraticRecur(n - 1);
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="space_complexity.cpp"
|
|
|
|
|
/* 平方阶(递归实现) */
|
|
|
|
|
int quadraticRecur(int n) {
|
2023-04-14 04:01:38 +08:00
|
|
|
|
if (n <= 0)
|
|
|
|
|
return 0;
|
2023-02-08 15:20:18 +08:00
|
|
|
|
vector<int> nums(n);
|
|
|
|
|
cout << "递归 n = " << n << " 中的 nums 长度 = " << nums.size() << endl;
|
|
|
|
|
return quadraticRecur(n - 1);
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="space_complexity.py"
|
2023-03-12 18:46:03 +08:00
|
|
|
|
def quadratic_recur(n: int) -> int:
|
2023-04-09 05:12:22 +08:00
|
|
|
|
"""平方阶(递归实现)"""
|
|
|
|
|
if n <= 0:
|
|
|
|
|
return 0
|
2023-02-08 15:20:18 +08:00
|
|
|
|
# 数组 nums 长度为 n, n-1, ..., 2, 1
|
2023-05-22 22:01:18 +08:00
|
|
|
|
nums = [0] * n
|
2023-02-08 15:20:18 +08:00
|
|
|
|
return quadratic_recur(n - 1)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="space_complexity.go"
|
|
|
|
|
/* 平方阶(递归实现) */
|
|
|
|
|
func spaceQuadraticRecur(n int) int {
|
|
|
|
|
if n <= 0 {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
nums := make([]int, n)
|
2023-02-09 04:43:12 +08:00
|
|
|
|
fmt.Printf("递归 n = %d 中的 nums 长度 = %d \n", n, len(nums))
|
2023-02-08 15:20:18 +08:00
|
|
|
|
return spaceQuadraticRecur(n - 1)
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:34:46 +08:00
|
|
|
|
=== "JS"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
```javascript title="space_complexity.js"
|
|
|
|
|
/* 平方阶(递归实现) */
|
|
|
|
|
function quadraticRecur(n) {
|
|
|
|
|
if (n <= 0) return 0;
|
|
|
|
|
const nums = new Array(n);
|
|
|
|
|
console.log(`递归 n = ${n} 中的 nums 长度 = ${nums.length}`);
|
|
|
|
|
return quadraticRecur(n - 1);
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:34:46 +08:00
|
|
|
|
=== "TS"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
```typescript title="space_complexity.ts"
|
|
|
|
|
/* 平方阶(递归实现) */
|
|
|
|
|
function quadraticRecur(n: number): number {
|
|
|
|
|
if (n <= 0) return 0;
|
|
|
|
|
const nums = new Array(n);
|
|
|
|
|
console.log(`递归 n = ${n} 中的 nums 长度 = ${nums.length}`);
|
|
|
|
|
return quadraticRecur(n - 1);
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="space_complexity.c"
|
2023-04-18 20:19:07 +08:00
|
|
|
|
/* 平方阶(递归实现) */
|
|
|
|
|
int quadraticRecur(int n) {
|
|
|
|
|
if (n <= 0)
|
|
|
|
|
return 0;
|
|
|
|
|
int *nums = malloc(sizeof(int) * n);
|
|
|
|
|
printf("递归 n = %d 中的 nums 长度 = %d\r\n", n, n);
|
2023-07-25 16:42:55 +08:00
|
|
|
|
int res = quadraticRecur(n - 1)
|
2023-04-18 20:19:07 +08:00
|
|
|
|
free(nums);
|
2023-07-25 16:42:55 +08:00
|
|
|
|
return res;
|
2023-04-18 20:19:07 +08:00
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="space_complexity.cs"
|
|
|
|
|
/* 平方阶(递归实现) */
|
2023-04-23 14:58:03 +08:00
|
|
|
|
int quadraticRecur(int n) {
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if (n <= 0) return 0;
|
|
|
|
|
int[] nums = new int[n];
|
2023-02-08 22:16:25 +08:00
|
|
|
|
Console.WriteLine("递归 n = " + n + " 中的 nums 长度 = " + nums.Length);
|
2023-02-08 15:20:18 +08:00
|
|
|
|
return quadraticRecur(n - 1);
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="space_complexity.swift"
|
|
|
|
|
/* 平方阶(递归实现) */
|
2023-02-08 20:30:14 +08:00
|
|
|
|
@discardableResult
|
2023-02-08 15:20:18 +08:00
|
|
|
|
func quadraticRecur(n: Int) -> Int {
|
|
|
|
|
if n <= 0 {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
// 数组 nums 长度为 n, n-1, ..., 2, 1
|
|
|
|
|
let nums = Array(repeating: 0, count: n)
|
2023-02-08 20:30:14 +08:00
|
|
|
|
print("递归 n = \(n) 中的 nums 长度 = \(nums.count)")
|
2023-02-08 15:20:18 +08:00
|
|
|
|
return quadraticRecur(n: n - 1)
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="space_complexity.zig"
|
|
|
|
|
// 平方阶(递归实现)
|
|
|
|
|
fn quadraticRecur(comptime n: i32) i32 {
|
|
|
|
|
if (n <= 0) return 0;
|
|
|
|
|
var nums = [_]i32{0}**n;
|
|
|
|
|
std.debug.print("递归 n = {} 中的 nums 长度 = {}\n", .{n, nums.len});
|
|
|
|
|
return quadraticRecur(n - 1);
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-06-02 02:38:24 +08:00
|
|
|
|
=== "Dart"
|
|
|
|
|
|
|
|
|
|
```dart title="space_complexity.dart"
|
|
|
|
|
/* 平方阶(递归实现) */
|
|
|
|
|
int quadraticRecur(int n) {
|
|
|
|
|
if (n <= 0) return 0;
|
|
|
|
|
List<int> nums = List.filled(n, 0);
|
|
|
|
|
print('递归 n = $n 中的 nums 长度 = ${nums.length}');
|
|
|
|
|
return quadraticRecur(n - 1);
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 10:57:40 +08:00
|
|
|
|
=== "Rust"
|
|
|
|
|
|
|
|
|
|
```rust title="space_complexity.rs"
|
|
|
|
|
/* 平方阶(递归实现) */
|
|
|
|
|
fn quadratic_recur(n: i32) -> i32 {
|
|
|
|
|
if n <= 0 {return 0};
|
|
|
|
|
// 数组 nums 长度为 n, n-1, ..., 2, 1
|
|
|
|
|
let nums = vec![0; n as usize];
|
|
|
|
|
println!("递归 n = {} 中的 nums 长度 = {}", n, nums.len());
|
|
|
|
|
return quadratic_recur(n - 1);
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-02-26 18:17:57 +08:00
|
|
|
|
![递归函数产生的平方阶空间复杂度](space_complexity.assets/space_complexity_recursive_quadratic.png)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-26 19:53:26 +08:00
|
|
|
|
<p align="center"> Fig. 递归函数产生的平方阶空间复杂度 </p>
|
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
### 指数阶 $O(2^n)$
|
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
指数阶常见于二叉树。高度为 $n$ 的「满二叉树」的节点数量为 $2^n - 1$ ,占用 $O(2^n)$ 空间。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="space_complexity.java"
|
|
|
|
|
/* 指数阶(建立满二叉树) */
|
|
|
|
|
TreeNode buildTree(int n) {
|
2023-04-14 04:01:38 +08:00
|
|
|
|
if (n == 0)
|
|
|
|
|
return null;
|
2023-02-08 15:20:18 +08:00
|
|
|
|
TreeNode root = new TreeNode(0);
|
|
|
|
|
root.left = buildTree(n - 1);
|
|
|
|
|
root.right = buildTree(n - 1);
|
|
|
|
|
return root;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="space_complexity.cpp"
|
|
|
|
|
/* 指数阶(建立满二叉树) */
|
2023-04-14 04:01:38 +08:00
|
|
|
|
TreeNode *buildTree(int n) {
|
|
|
|
|
if (n == 0)
|
|
|
|
|
return nullptr;
|
|
|
|
|
TreeNode *root = new TreeNode(0);
|
2023-02-08 15:20:18 +08:00
|
|
|
|
root->left = buildTree(n - 1);
|
|
|
|
|
root->right = buildTree(n - 1);
|
|
|
|
|
return root;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="space_complexity.py"
|
2023-03-23 18:56:56 +08:00
|
|
|
|
def build_tree(n: int) -> TreeNode | None:
|
2023-04-09 05:12:22 +08:00
|
|
|
|
"""指数阶(建立满二叉树)"""
|
|
|
|
|
if n == 0:
|
|
|
|
|
return None
|
2023-02-08 15:20:18 +08:00
|
|
|
|
root = TreeNode(0)
|
|
|
|
|
root.left = build_tree(n - 1)
|
|
|
|
|
root.right = build_tree(n - 1)
|
|
|
|
|
return root
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="space_complexity.go"
|
|
|
|
|
/* 指数阶(建立满二叉树) */
|
|
|
|
|
func buildTree(n int) *treeNode {
|
|
|
|
|
if n == 0 {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
root := newTreeNode(0)
|
|
|
|
|
root.left = buildTree(n - 1)
|
|
|
|
|
root.right = buildTree(n - 1)
|
|
|
|
|
return root
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:34:46 +08:00
|
|
|
|
=== "JS"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
```javascript title="space_complexity.js"
|
|
|
|
|
/* 指数阶(建立满二叉树) */
|
|
|
|
|
function buildTree(n) {
|
|
|
|
|
if (n === 0) return null;
|
|
|
|
|
const root = new TreeNode(0);
|
|
|
|
|
root.left = buildTree(n - 1);
|
|
|
|
|
root.right = buildTree(n - 1);
|
|
|
|
|
return root;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:34:46 +08:00
|
|
|
|
=== "TS"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
```typescript title="space_complexity.ts"
|
|
|
|
|
/* 指数阶(建立满二叉树) */
|
|
|
|
|
function buildTree(n: number): TreeNode | null {
|
|
|
|
|
if (n === 0) return null;
|
|
|
|
|
const root = new TreeNode(0);
|
|
|
|
|
root.left = buildTree(n - 1);
|
|
|
|
|
root.right = buildTree(n - 1);
|
|
|
|
|
return root;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="space_complexity.c"
|
2023-04-18 20:19:07 +08:00
|
|
|
|
/* 指数阶(建立满二叉树) */
|
|
|
|
|
TreeNode *buildTree(int n) {
|
|
|
|
|
if (n == 0)
|
|
|
|
|
return NULL;
|
|
|
|
|
TreeNode *root = newTreeNode(0);
|
|
|
|
|
root->left = buildTree(n - 1);
|
|
|
|
|
root->right = buildTree(n - 1);
|
|
|
|
|
return root;
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="space_complexity.cs"
|
|
|
|
|
/* 指数阶(建立满二叉树) */
|
2023-04-23 14:58:03 +08:00
|
|
|
|
TreeNode? buildTree(int n) {
|
2023-02-08 15:20:18 +08:00
|
|
|
|
if (n == 0) return null;
|
|
|
|
|
TreeNode root = new TreeNode(0);
|
|
|
|
|
root.left = buildTree(n - 1);
|
|
|
|
|
root.right = buildTree(n - 1);
|
|
|
|
|
return root;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="space_complexity.swift"
|
|
|
|
|
/* 指数阶(建立满二叉树) */
|
|
|
|
|
func buildTree(n: Int) -> TreeNode? {
|
|
|
|
|
if n == 0 {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
let root = TreeNode(x: 0)
|
|
|
|
|
root.left = buildTree(n: n - 1)
|
|
|
|
|
root.right = buildTree(n: n - 1)
|
|
|
|
|
return root
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="space_complexity.zig"
|
|
|
|
|
// 指数阶(建立满二叉树)
|
|
|
|
|
fn buildTree(mem_allocator: std.mem.Allocator, n: i32) !?*inc.TreeNode(i32) {
|
|
|
|
|
if (n == 0) return null;
|
|
|
|
|
const root = try mem_allocator.create(inc.TreeNode(i32));
|
|
|
|
|
root.init(0);
|
|
|
|
|
root.left = try buildTree(mem_allocator, n - 1);
|
|
|
|
|
root.right = try buildTree(mem_allocator, n - 1);
|
|
|
|
|
return root;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-06-02 02:38:24 +08:00
|
|
|
|
=== "Dart"
|
|
|
|
|
|
|
|
|
|
```dart title="space_complexity.dart"
|
|
|
|
|
/* 指数阶(建立满二叉树) */
|
|
|
|
|
TreeNode? buildTree(int n) {
|
|
|
|
|
if (n == 0) return null;
|
|
|
|
|
TreeNode root = TreeNode(0);
|
|
|
|
|
root.left = buildTree(n - 1);
|
|
|
|
|
root.right = buildTree(n - 1);
|
|
|
|
|
return root;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 10:57:40 +08:00
|
|
|
|
=== "Rust"
|
|
|
|
|
|
|
|
|
|
```rust title="space_complexity.rs"
|
|
|
|
|
/* 指数阶(建立满二叉树) */
|
|
|
|
|
fn build_tree(n: i32) -> Option<Rc<RefCell<TreeNode>>> {
|
|
|
|
|
if n == 0 {return None};
|
|
|
|
|
let root = TreeNode::new(0);
|
|
|
|
|
root.borrow_mut().left = build_tree(n - 1);
|
|
|
|
|
root.borrow_mut().right = build_tree(n - 1);
|
|
|
|
|
return Some(root);
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-02-26 18:17:57 +08:00
|
|
|
|
![满二叉树产生的指数阶空间复杂度](space_complexity.assets/space_complexity_exponential.png)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-26 19:53:26 +08:00
|
|
|
|
<p align="center"> Fig. 满二叉树产生的指数阶空间复杂度 </p>
|
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
### 对数阶 $O(\log n)$
|
|
|
|
|
|
2023-04-07 22:32:04 +08:00
|
|
|
|
对数阶常见于分治算法和数据类型转换等。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-07 22:32:04 +08:00
|
|
|
|
例如“归并排序”算法,输入长度为 $n$ 的数组,每轮递归将数组从中点划分为两半,形成高度为 $\log n$ 的递归树,使用 $O(\log n)$ 栈帧空间。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-07 22:32:04 +08:00
|
|
|
|
再例如“数字转化为字符串”,输入任意正整数 $n$ ,它的位数为 $\log_{10} n$ ,即对应字符串长度为 $\log_{10} n$ ,因此空间复杂度为 $O(\log_{10} n) = O(\log n)$ 。
|
2023-04-17 18:24:20 +08:00
|
|
|
|
|
|
|
|
|
## 2.3.4. 权衡时间与空间
|
|
|
|
|
|
|
|
|
|
理想情况下,我们希望算法的时间复杂度和空间复杂度都能达到最优。然而在实际情况中,同时优化时间复杂度和空间复杂度通常是非常困难的。
|
|
|
|
|
|
|
|
|
|
**降低时间复杂度通常需要以提升空间复杂度为代价,反之亦然**。我们将牺牲内存空间来提升算法运行速度的思路称为“以空间换时间”;反之,则称为“以时间换空间”。
|
|
|
|
|
|
|
|
|
|
选择哪种思路取决于我们更看重哪个方面。在大多数情况下,时间比空间更宝贵,因此以空间换时间通常是更常用的策略。当然,在数据量很大的情况下,控制空间复杂度也是非常重要的。
|