diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml new file mode 100644 index 000000000..6b615969d --- /dev/null +++ b/.github/workflows/go.yml @@ -0,0 +1,36 @@ +name: Go + +on: + push: + branches: [ "main" ] + paths: ["codes/go/**/*.go"] + pull_request: + branches: [ "main" ] + paths: ["codes/go/**/*.go"] + workflow_dispatch: + +jobs: + build: + name: Go ${{ matrix.go-version }} on ${{ matrix.os }} + runs-on: ${{ matrix.os }} + defaults: + run: + working-directory: codes/go/ + strategy: + matrix: + os: [ubuntu-latest, macos-latest, windows-latest] + go-version: ["1.19.x"] + + steps: + - uses: actions/checkout@v4 + + - name: Setup Go ${{ matrix.go-version }} + uses: actions/setup-go@v3 + with: + go-version: ${{ matrix.go-version }} + - name: Check out code into the Go module directory + run: go get -v -t -d ./... + - name: Build + run: go build -v ./... + - name: Test with Go + run: go test -v ./... diff --git a/codes/go/chapter_computational_complexity/space_complexity.go b/codes/go/chapter_computational_complexity/space_complexity.go index aaad21404..3639c7912 100644 --- a/codes/go/chapter_computational_complexity/space_complexity.go +++ b/codes/go/chapter_computational_complexity/space_complexity.go @@ -7,6 +7,8 @@ package chapter_computational_complexity import ( "fmt" "strconv" + + . "github.com/krahets/hello-algo/pkg" ) /* 结构体 */ @@ -15,33 +17,11 @@ type node struct { next *node } -/* treeNode 二叉树 */ -type treeNode struct { - val int - left *treeNode - right *treeNode -} - /* 创建 node 结构体 */ func newNode(val int) *node { return &node{val: val} } -/* 创建 treeNode 结构体 */ -func newTreeNode(val int) *treeNode { - return &treeNode{val: val} -} - -/* 输出二叉树 */ -func printTree(root *treeNode) { - if root == nil { - return - } - fmt.Println(root.val) - printTree(root.left) - printTree(root.right) -} - /* 函数 */ func function() int { // 执行某些操作... @@ -54,7 +34,7 @@ func spaceConstant(n int) { const a = 0 b := 0 nums := make([]int, 10000) - ListNode := newNode(0) + node := newNode(0) // 循环中的变量占用 O(1) 空间 var c int for i := 0; i < n; i++ { @@ -64,7 +44,10 @@ func spaceConstant(n int) { for i := 0; i < n; i++ { function() } - fmt.Println(a, b, nums, c, ListNode) + b += 0 + c += 0 + nums[0] = 0 + node.val = 0 } /* 线性阶 */ @@ -112,12 +95,12 @@ func spaceQuadraticRecur(n int) int { } /* 指数阶(建立满二叉树) */ -func buildTree(n int) *treeNode { +func buildTree(n int) *TreeNode { if n == 0 { return nil } - root := newTreeNode(0) - root.left = buildTree(n - 1) - root.right = buildTree(n - 1) + root := NewTreeNode(0) + root.Left = buildTree(n - 1) + root.Right = buildTree(n - 1) return root } diff --git a/codes/go/chapter_computational_complexity/space_complexity_test.go b/codes/go/chapter_computational_complexity/space_complexity_test.go index 9e8237235..2c62fdb78 100644 --- a/codes/go/chapter_computational_complexity/space_complexity_test.go +++ b/codes/go/chapter_computational_complexity/space_complexity_test.go @@ -6,25 +6,21 @@ package chapter_computational_complexity import ( "testing" + + . "github.com/krahets/hello-algo/pkg" ) func TestSpaceComplexity(t *testing.T) { - /* ======= Test Case ======= */ n := 5 - - /* ====== Driver Code ====== */ // 常数阶 spaceConstant(n) - // 线性阶 spaceLinear(n) spaceLinearRecur(n) - // 平方阶 spaceQuadratic(n) spaceQuadraticRecur(n) - // 指数阶 root := buildTree(n) - printTree(root) + PrintTree(root) } diff --git a/codes/python/test_all.py b/codes/python/test_all.py index 1a371123a..a90c773a4 100644 --- a/codes/python/test_all.py +++ b/codes/python/test_all.py @@ -18,6 +18,7 @@ if __name__ == "__main__": stderr=subprocess.PIPE, text=True, env=env, + encoding='utf-8' ) # Wait for the process to complete, and get the output and error messages stdout, stderr = process.communicate()