Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #178 from guowei-gong/gongguowei
docs(array): sample code for golang
- Loading branch information
Showing
3 changed files
with
190 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// File: array.go | ||
// Created Time: 2022-12-29 | ||
// Author: GuoWei (gongguowei01@gmail.com) | ||
|
||
package chapter_array_and_linkedlist | ||
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
) | ||
|
||
/** | ||
我们将 Go 中的 Slice 切片看作 Array 数组,降低理解成本, | ||
有利于我们将关注点放在数据结构与算法上。 | ||
*/ | ||
|
||
/* 随机返回一个数组元素 */ | ||
func randomAccess(nums []int) (randomNum int) { | ||
// 在区间 [0, nums.length) 中随机抽取一个数字 | ||
randomIndex := rand.Intn(len(nums)) | ||
// 获取并返回随机元素 | ||
randomNum = nums[randomIndex] | ||
return | ||
} | ||
|
||
/* 扩展数组长度 */ | ||
func extend(nums []int, enlarge int) []int { | ||
// 初始化一个扩展长度后的数组 | ||
res := make([]int, len(nums)+enlarge) | ||
// 将原数组中的所有元素复制到新数组 | ||
for i := 0; i < len(nums); i++ { | ||
res[i] = nums[i] | ||
} | ||
// 返回扩展后的新数组 | ||
return res | ||
} | ||
|
||
/* 在数组的索引 index 处插入元素 num */ | ||
func insert(nums []int, num int, index int) { | ||
// 把索引 index 以及之后的所有元素向后移动一位 | ||
// 如果超出了数组长度,会被直接舍弃 | ||
for i := len(nums) - 1; i > index; i-- { | ||
nums[i] = nums[i-1] | ||
} | ||
// 将 num 赋给 index 处元素 | ||
nums[index] = num | ||
} | ||
|
||
/* 删除索引 index 处元素 */ | ||
func remove(nums []int, index int) { | ||
// 把索引 index 之后的所有元素向前移动一位 | ||
for i := index; i < len(nums) - 1; i++ { | ||
nums[i] = nums[i+1] | ||
} | ||
} | ||
|
||
/* 遍历数组 */ | ||
func traverse(nums []int) { | ||
var count int | ||
// 通过索引遍历数组 | ||
for i := 0; i < len(nums); i++ { | ||
count++ | ||
} | ||
// 直接遍历数组 | ||
for index, val := range nums { | ||
fmt.Printf("index:%v value:%v\n", index, val) | ||
} | ||
} | ||
|
||
/* 在数组中查找指定元素 */ | ||
func find(nums []int, target int) (index int) { | ||
index = -1 | ||
for i := 0; i < len(nums); i++ { | ||
if nums[i] == target { | ||
index = i | ||
break | ||
} | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// File: array_test.go | ||
// Created Time: 2022-12-29 | ||
// Author: GuoWei (gongguowei01@gmail.com) | ||
|
||
package chapter_array_and_linkedlist | ||
|
||
/** | ||
我们将 Go 中的 Slice 切片看作 Array 数组。因为这样可以 | ||
降低理解成本,利于我们将关注点放在数据结构与算法上。 | ||
*/ | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
/* Driver Code */ | ||
func TestArray(t *testing.T) { | ||
/* 初始化数组 */ | ||
var arr []int | ||
fmt.Println("数组 arr = ", arr) | ||
nums := []int{1, 3, 2, 5, 4} | ||
fmt.Println("数组 nums = ", nums) | ||
|
||
/* 随机访问 */ | ||
randomNum := randomAccess(nums) | ||
fmt.Println("在 nums 中获取随机元素 ", randomNum) | ||
|
||
/* 长度扩展 */ | ||
nums = extend(nums, 3) | ||
fmt.Println("将数组长度扩展至 8 ,得到 nums = ", nums) | ||
|
||
/* 插入元素 */ | ||
insert(nums, 6, 3) | ||
fmt.Println("在索引 3 处插入数字 6 ,得到 nums = ", nums) | ||
|
||
/* 删除元素 */ | ||
remove(nums, 2) | ||
fmt.Println("删除索引 2 处的元素,得到 nums = ", nums) | ||
|
||
/* 遍历数组 */ | ||
traverse(nums) | ||
|
||
/* 查找元素 */ | ||
index := find(nums, 3) | ||
fmt.Println("在 nums 中查找元素 3 ,得到索引 = ", index) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters