mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 04:31:55 +08:00
Add build script for Go and update Go codes.
This commit is contained in:
@ -600,24 +600,7 @@ $$
|
||||
=== "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)
|
||||
}
|
||||
[class]{}-[func]{spaceConstant}
|
||||
```
|
||||
|
||||
=== "JavaScript"
|
||||
@ -703,21 +686,7 @@ $$
|
||||
=== "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)
|
||||
}
|
||||
}
|
||||
[class]{}-[func]{spaceLinear}
|
||||
```
|
||||
|
||||
=== "JavaScript"
|
||||
@ -800,14 +769,7 @@ $$
|
||||
=== "Go"
|
||||
|
||||
```go title="space_complexity.go"
|
||||
/* 线性阶(递归实现) */
|
||||
func spaceLinearRecur(n int) {
|
||||
fmt.Println("递归 n =", n)
|
||||
if n == 1 {
|
||||
return
|
||||
}
|
||||
spaceLinearRecur(n - 1)
|
||||
}
|
||||
[class]{}-[func]{spaceLinearRecur}
|
||||
```
|
||||
|
||||
=== "JavaScript"
|
||||
@ -880,14 +842,7 @@ $$
|
||||
=== "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)
|
||||
}
|
||||
}
|
||||
[class]{}-[func]{spaceQuadratic}
|
||||
```
|
||||
|
||||
=== "JavaScript"
|
||||
@ -964,15 +919,7 @@ $$
|
||||
=== "Go"
|
||||
|
||||
```go title="space_complexity.go"
|
||||
/* 平方阶(递归实现) */
|
||||
func spaceQuadraticRecur(n int) int {
|
||||
if n <= 0 {
|
||||
return 0
|
||||
}
|
||||
// 数组 nums 长度为 n, n-1, ..., 2, 1
|
||||
nums := make([]int, n)
|
||||
return spaceQuadraticRecur(n - 1)
|
||||
}
|
||||
[class]{}-[func]{spaceQuadraticRecur}
|
||||
```
|
||||
|
||||
=== "JavaScript"
|
||||
@ -1046,16 +993,7 @@ $$
|
||||
=== "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
|
||||
}
|
||||
[class]{}-[func]{buildTree}
|
||||
```
|
||||
|
||||
=== "JavaScript"
|
||||
|
||||
Reference in New Issue
Block a user