diff --git a/codes/c/chapter_heap/my_heap.c b/codes/c/chapter_heap/my_heap.c index 04a421a6e..1ee9e1a48 100644 --- a/codes/c/chapter_heap/my_heap.c +++ b/codes/c/chapter_heap/my_heap.c @@ -28,7 +28,7 @@ maxHeap *newMaxHeap(int nums[], int size) { maxHeap *h = (maxHeap *)malloc(sizeof(maxHeap)); h->size = size; 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); } diff --git a/codes/go/chapter_computational_complexity/iteration.go b/codes/go/chapter_computational_complexity/iteration.go new file mode 100644 index 000000000..820d32648 --- /dev/null +++ b/codes/go/chapter_computational_complexity/iteration.go @@ -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 +} diff --git a/codes/go/chapter_computational_complexity/iteration_test.go b/codes/go/chapter_computational_complexity/iteration_test.go new file mode 100644 index 000000000..abde1ba46 --- /dev/null +++ b/codes/go/chapter_computational_complexity/iteration_test.go @@ -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) +} diff --git a/codes/go/chapter_computational_complexity/recursion.go b/codes/go/chapter_computational_complexity/recursion.go new file mode 100644 index 000000000..143065016 --- /dev/null +++ b/codes/go/chapter_computational_complexity/recursion.go @@ -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 +} diff --git a/codes/go/chapter_computational_complexity/recursion_test.go b/codes/go/chapter_computational_complexity/recursion_test.go new file mode 100644 index 000000000..675035559 --- /dev/null +++ b/codes/go/chapter_computational_complexity/recursion_test.go @@ -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) +} diff --git a/codes/go/chapter_heap/my_heap.go b/codes/go/chapter_heap/my_heap.go index 841ef0e1b..e34ee9d69 100644 --- a/codes/go/chapter_heap/my_heap.go +++ b/codes/go/chapter_heap/my_heap.go @@ -26,7 +26,7 @@ func newHeap() *maxHeap { func newMaxHeap(nums []any) *maxHeap { // 将列表元素原封不动添加进堆 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) }