2022-12-15 16:41:47 +08:00
|
|
|
// File: space_complexity.go
|
|
|
|
// Created Time: 2022-12-15
|
|
|
|
// Author: cathay (cathaycchen@gmail.com)
|
|
|
|
|
|
|
|
package chapter_computational_complexity
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
2023-01-03 14:39:31 +08:00
|
|
|
/* 结构体 */
|
|
|
|
type node struct {
|
2022-12-15 16:41:47 +08:00
|
|
|
val int
|
2023-01-03 14:39:31 +08:00
|
|
|
next *node
|
2022-12-15 16:41:47 +08:00
|
|
|
}
|
|
|
|
|
2023-01-03 14:39:31 +08:00
|
|
|
/* treeNode 二叉树 */
|
|
|
|
type treeNode struct {
|
2022-12-15 16:41:47 +08:00
|
|
|
val int
|
2023-01-03 14:39:31 +08:00
|
|
|
left *treeNode
|
|
|
|
right *treeNode
|
2022-12-15 16:41:47 +08:00
|
|
|
}
|
|
|
|
|
2023-01-03 14:39:31 +08:00
|
|
|
/* 创建 node 结构体 */
|
|
|
|
func newNode(val int) *node {
|
|
|
|
return &node{val: val}
|
2022-12-15 16:41:47 +08:00
|
|
|
}
|
|
|
|
|
2023-01-03 14:39:31 +08:00
|
|
|
/* 创建 treeNode 结构体 */
|
|
|
|
func newTreeNode(val int) *treeNode {
|
|
|
|
return &treeNode{val: val}
|
2022-12-15 16:41:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 输出二叉树 */
|
2023-01-03 14:39:31 +08:00
|
|
|
func printTree(root *treeNode) {
|
2022-12-15 16:41:47 +08:00
|
|
|
if root == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Println(root.val)
|
|
|
|
printTree(root.left)
|
|
|
|
printTree(root.right)
|
|
|
|
}
|
|
|
|
|
2022-12-26 13:14:32 +08:00
|
|
|
/* 函数 */
|
2022-12-15 16:41:47 +08:00
|
|
|
func function() int {
|
|
|
|
// do something...
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 常数阶 */
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 线性阶 */
|
|
|
|
func spaceLinear(n int) {
|
|
|
|
// 长度为 n 的数组占用 O(n) 空间
|
|
|
|
_ = make([]int, n)
|
|
|
|
// 长度为 n 的列表占用 O(n) 空间
|
2023-01-03 14:39:31 +08:00
|
|
|
var nodes []*node
|
2022-12-15 16:41:47 +08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 线性阶(递归实现) */
|
|
|
|
func spaceLinearRecur(n int) {
|
|
|
|
fmt.Println("递归 n =", n)
|
|
|
|
if n == 1 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
spaceLinearRecur(n - 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 平方阶 */
|
|
|
|
func spaceQuadratic(n int) {
|
|
|
|
// 矩阵占用 O(n^2) 空间
|
|
|
|
numMatrix := make([][]int, n)
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
numMatrix[i] = make([]int, n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 平方阶(递归实现) */
|
|
|
|
func spaceQuadraticRecur(n int) int {
|
|
|
|
if n <= 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
nums := make([]int, n)
|
|
|
|
fmt.Printf("递归 n = %d 中的 nums 长度 = %d \n", n, len(nums))
|
|
|
|
return spaceQuadraticRecur(n - 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 指数阶(建立满二叉树) */
|
2023-01-03 14:39:31 +08:00
|
|
|
func buildTree(n int) *treeNode {
|
2022-12-15 16:41:47 +08:00
|
|
|
if n == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
root := newTreeNode(0)
|
|
|
|
root.left = buildTree(n - 1)
|
|
|
|
root.right = buildTree(n - 1)
|
|
|
|
return root
|
|
|
|
}
|