diff --git a/codes/go/chapter_array_and_linkedlist/array.go b/codes/go/chapter_array_and_linkedlist/array.go new file mode 100644 index 00000000..416957ff --- /dev/null +++ b/codes/go/chapter_array_and_linkedlist/array.go @@ -0,0 +1,78 @@ +// File: array.go +// Created Time: 2022-12-29 +// Author: cathay (cathaycchen@gmail.com) + +package chapter_array_and_linkedlist + +import ( + "fmt" + "math/rand" +) + +/* 初始化数组 */ +var arr = [5]int{} +var nums = []int{1, 3, 2, 5, 4} + +/* 随机返回一个数组元素 */ +func randomAccess(nums []int) int { + randomIndex := rand.Intn(len(nums)) + randomNum := nums[randomIndex] + return randomNum +} + +/* 扩展数组长度 */ +func extend(nums []int, enlarge int) []int { + // 初始化一个扩展长度后的数组 + res := make([]int, len(nums)+enlarge) + // 将原数组中的所有元素复制到新数组 + for i, num := range nums { + res[i] = num + } + // 返回扩展后的新数组 + return res +} + +/* 在数组的索引 index 处插入元素 num */ +func insert(nums []int, size int, num int, index int) { + // 把索引 index 以及之后的所有元素向后移动一位 + for i := size - 1; i > index; i-- { + nums[i] = nums[i-1] + } + // 将 num 赋给 index 处元素 + nums[index] = num +} + +/* 删除索引 index 处元素 */ +func remove(nums []int, size int, index int) { + // 把索引 index 之后的所有元素向前移动一位 + for i := index; i < size-1; i++ { + nums[i] = nums[i+1] + } +} + +/* 遍历数组 */ +func traverse(nums []int) { + count := 0 + // 通过索引遍历数组 + for i := 0; i < len(nums); i++ { + count++ + } + fmt.Println(count) + + count = 0 + // 直接遍历数组 + for range nums { + count++ + } + fmt.Println(count) +} + +/* 在数组中查找指定元素,返回第一个索引位置,未查找到则返回 -1 */ +func find(nums []int, target int) int { + for i := 0; i < len(nums); i++ { + if nums[i] == target { + return i + } + } + return -1 +} diff --git a/codes/go/chapter_array_and_linkedlist/array_test.go b/codes/go/chapter_array_and_linkedlist/array_test.go new file mode 100644 index 00000000..37f75a55 --- /dev/null +++ b/codes/go/chapter_array_and_linkedlist/array_test.go @@ -0,0 +1,28 @@ +// File: array_test.go +// Created Time: 2022-12-29 +// Author: cathay (cathaycchen@gmail.com) + +package chapter_array_and_linkedlist + +import ( + "fmt" + "testing" +) + +func TestArray(t *testing.T) { + nums := make([]int, 5) + fmt.Println("randomAccess:", randomAccess(nums)) + + fmt.Println("extend:", extend(nums, 5)) + + insert(nums, 5, 2, 2) + fmt.Println("after insert:", nums) + + remove(nums, 5, 2) + fmt.Println("after remove:", nums) + + fmt.Println("traverse nums:") + traverse(nums) + + fmt.Println("find value 2 key:", find(nums, 2)) +} diff --git a/docs/chapter_array_and_linkedlist/array.md b/docs/chapter_array_and_linkedlist/array.md index 70e30129..bae5c277 100644 --- a/docs/chapter_array_and_linkedlist/array.md +++ b/docs/chapter_array_and_linkedlist/array.md @@ -43,7 +43,8 @@ comments: true === "Go" ```go title="array.go" - + var arr = [5]int{} + var nums = [5]int{1, 3, 2, 5, 4} ``` === "JavaScript" @@ -136,7 +137,12 @@ elementAddr = firtstElementAddr + elementLength * elementIndex === "Go" ```go title="array.go" - + /* 随机返回一个数组元素 */ + func randomAccess(nums []int) int { + randomIndex := rand.Intn(len(nums)) + randomNum := nums[randomIndex] + return randomNum + } ``` === "JavaScript" @@ -239,7 +245,17 @@ elementAddr = firtstElementAddr + elementLength * elementIndex === "Go" ```go title="array.go" - + /* 扩展数组长度 */ + func extend(nums []int, enlarge int) []int { + // 初始化一个扩展长度后的数组 + res := make([]int, len(nums)+enlarge) + // 将原数组中的所有元素复制到新数组 + for i, num := range nums { + res[i] = num + } + // 返回扩展后的新数组 + return res + } ``` === "JavaScript" @@ -373,7 +389,23 @@ elementAddr = firtstElementAddr + elementLength * elementIndex === "Go" ```go title="array.go" - + /* 在数组的索引 index 处插入元素 num */ + func insert(nums []int, size int, num int, index int) { + // 把索引 index 以及之后的所有元素向后移动一位 + for i := size - 1; i > index; i-- { + nums[i] = nums[i-1] + } + // 将 num 赋给 index 处元素 + nums[index] = num + } + + /* 删除索引 index 处元素 */ + func remove(nums []int, size int, index int) { + // 把索引 index 之后的所有元素向前移动一位 + for i := index; i < size-1; i++ { + nums[i] = nums[i+1] + } + } ``` === "JavaScript" @@ -502,7 +534,18 @@ elementAddr = firtstElementAddr + elementLength * elementIndex === "Go" ```go title="array.go" - + /* 遍历数组 */ + func traverse(nums []int) { + count := 0 + // 通过索引遍历数组 + for i := 0; i < len(nums); i++ { + count++ + } + // 直接遍历数组 + for range nums { + count++ + } + } ``` === "JavaScript" @@ -607,7 +650,15 @@ elementAddr = firtstElementAddr + elementLength * elementIndex === "Go" ```go title="array.go" - + /* 在数组中查找指定元素,返回第一个索引位置,未查找到则返回 -1 */ + func find(nums []int, target int) int { + for i := 0; i < len(nums); i++ { + if nums[i] == target { + return i + } + } + return -1 + } ``` === "JavaScript"