feat(go): add iterration/recursion & fix bugs (#698)

pull/699/head
Reanon 1 year ago committed by GitHub
parent 8347c2da36
commit 20f53e9fc4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -28,7 +28,7 @@ maxHeap *newMaxHeap(int nums[], int size) {
maxHeap *h = (maxHeap *)malloc(sizeof(maxHeap)); maxHeap *h = (maxHeap *)malloc(sizeof(maxHeap));
h->size = size; h->size = size;
memcpy(h->data, nums, size * sizeof(int)); memcpy(h->data, nums, size * sizeof(int));
for (int i = size - 1; i >= 0; i--) { for (int i = parent(size - 1); i >= 0; i--) {
// 堆化除叶节点以外的其他所有节点 // 堆化除叶节点以外的其他所有节点
siftDown(h, i); siftDown(h, i);
} }

@ -0,0 +1,59 @@
// File: iteration.go
// Created Time: 2023-08-28
// Author: Reanon (793584285@qq.com)
package chapter_computational_complexity
import "fmt"
/* for 循环 */
func forLoop(n int) int {
res := 0
// 循环求和 1, 2, ..., n-1, n
for i := 1; i <= n; i++ {
res += i
}
return res
}
/* while 循环 */
func whileLoop(n int) int {
res := 0
// 初始化条件变量
i := 1
// 循环求和 1, 2, ..., n-1, n
for i <= n {
res += i
// 更新条件变量
i++
}
return res
}
/* while 循环(两次更新) */
func whileLoopII(n int) int {
res := 0
// 初始化条件变量
i := 1
// 循环求和 1, 4, ...
for i <= n {
res += i
// 更新条件变量
i++
i *= 2
}
return res
}
/* 双层 for 循环 */
func nestedForLoop(n int) string {
res := ""
// 循环 i = 1, 2, ..., n-1, n
for i := 1; i <= n; i++ {
for j := 1; j <= n; j++ {
// 循环 j = 1, 2, ..., n-1, n
res += fmt.Sprintf("(%d, %d), ", i, j)
}
}
return res
}

@ -0,0 +1,26 @@
// File: iteration_test.go
// Created Time: 2023-08-28
// Author: Reanon (793584285@qq.com)
package chapter_computational_complexity
import (
"fmt"
"testing"
)
/* Driver Code */
func TestIteration(t *testing.T) {
n := 5
res := forLoop(n)
fmt.Println("\nfor 循环的求和结果 res = ", res)
res = whileLoop(n)
fmt.Println("\nwhile 循环的求和结果 res = ", res)
res = whileLoopII(n)
fmt.Println("\nwhile 循环(两次更新)求和结果 res = ", res)
resStr := nestedForLoop(n)
fmt.Println("\n双层 for 循环的遍历结果 ", resStr)
}

@ -0,0 +1,39 @@
// File: recursion.go
// Created Time: 2023-08-28
// Author: Reanon (793584285@qq.com)
package chapter_computational_complexity
/* 递归 */
func recur(n int) int {
// 终止条件
if n == 1 {
return 1
}
// 递:递归调用
res := recur(n - 1)
// 归:返回结果
return n + res
}
/* 尾递归 */
func tailRecur(n int, res int) int {
// 终止条件
if n == 0 {
return res
}
// 尾递归调用
return tailRecur(n-1, res+n)
}
/* 斐波那契数列:递归 */
func fib(n int) int {
// 终止条件 f(1) = 0, f(2) = 1
if n == 1 || n == 2 {
return n - 1
}
// 递归调用 f(n) = f(n-1) + f(n-2)
res := fib(n-1) + fib(n-2)
// 返回结果 f(n)
return res
}

@ -0,0 +1,23 @@
// File: recursion_test.go
// Created Time: 2023-08-28
// Author: Reanon (793584285@qq.com)
package chapter_computational_complexity
import (
"fmt"
"testing"
)
/* Driver Code */
func TestRecursion(t *testing.T) {
n := 5
res := recur(n)
fmt.Println("\n递归函数的求和结果 res = ", res)
res = tailRecur(n, 0)
fmt.Println("\n尾递归函数的求和结果 res = ", res)
res = fib(n)
fmt.Println("\n斐波那契数列的第", n, "项为", res)
}

@ -26,7 +26,7 @@ func newHeap() *maxHeap {
func newMaxHeap(nums []any) *maxHeap { func newMaxHeap(nums []any) *maxHeap {
// 将列表元素原封不动添加进堆 // 将列表元素原封不动添加进堆
h := &maxHeap{data: nums} h := &maxHeap{data: nums}
for i := len(h.data) - 1; i >= 0; i-- { for i := h.parent(len(h.data) - 1); i >= 0; i-- {
// 堆化除叶节点以外的其他所有节点 // 堆化除叶节点以外的其他所有节点
h.siftDown(i) h.siftDown(i)
} }

Loading…
Cancel
Save