Add build script for Go and update Go codes.

This commit is contained in:
krahets
2023-02-09 04:45:06 +08:00
parent 12c085a088
commit e8c78f89f0
39 changed files with 391 additions and 1468 deletions

View File

@ -135,14 +135,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
=== "Go"
```go title="array.go"
/* 随机返回一个数组元素 */
func randomAccess(nums []int) (randomNum int) {
// 在区间 [0, nums.length) 中随机抽取一个数字
randomIndex := rand.Intn(len(nums))
// 获取并返回随机元素
randomNum = nums[randomIndex]
return
}
[class]{}-[func]{randomAccess}
```
=== "JavaScript"
@ -213,17 +206,7 @@ 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
}
[class]{}-[func]{extend}
```
=== "JavaScript"
@ -308,23 +291,9 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
=== "Go"
```go title="array.go"
/* 在数组的索引 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
}
[class]{}-[func]{insert}
/* 删除索引 index 处元素 */
func remove(nums []int, index int) {
// 把索引 index 之后的所有元素向前移动一位
for i := index; i < len(nums)-1; i++ {
nums[i] = nums[i+1]
}
}
[class]{}-[func]{remove}
```
=== "JavaScript"
@ -414,18 +383,7 @@ 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++
}
}
[class]{}-[func]{traverse}
```
=== "JavaScript"
@ -500,17 +458,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
=== "Go"
```go title="array.go"
/* 在数组中查找指定元素 */
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
}
[class]{}-[func]{find}
```
=== "JavaScript"

View File

@ -333,24 +333,9 @@ comments: true
=== "C++"
```cpp title="linked_list.cpp"
/* 在链表的结点 n0 之后插入结点 P */
void insert(ListNode* n0, ListNode* P) {
ListNode* n1 = n0->next;
n0->next = P;
P->next = n1;
}
[class]{}-[func]{insert}
/* 删除链表的结点 n0 之后的首个结点 */
void remove(ListNode* n0) {
if (n0->next == nullptr)
return;
// n0 -> P -> n1
ListNode* P = n0->next;
ListNode* n1 = P->next;
n0->next = n1;
// 释放内存
delete P;
}
[class]{}-[func]{remove}
```
=== "Python"
@ -364,66 +349,25 @@ comments: true
=== "Go"
```go title="linked_list.go"
/* 在链表的结点 n0 之后插入结点 P */
func insert(n0 *ListNode, P *ListNode) {
n1 := n0.Next
n0.Next = P
P.Next = n1
}
[class]{}-[func]{insertNode}
/* 删除链表的结点 n0 之后的首个结点 */
func removeNode(n0 *ListNode) {
if n0.Next == nil {
return
}
// n0 -> P -> n1
P := n0.Next
n1 := P.Next
n0.Next = n1
}
[class]{}-[func]{removeNode}
```
=== "JavaScript"
```javascript title="linked_list.js"
/* 在链表的结点 n0 之后插入结点 P */
function insert(n0, P) {
let n1 = n0.next;
n0.next = P;
P.next = n1;
}
[class]{}-[func]{insert}
/* 删除链表的结点 n0 之后的首个结点 */
function remove(n0) {
if (!n0.next)
return;
// n0 -> P -> n1
let P = n0.next;
let n1 = P.next;
n0.next = n1;
}
[class]{}-[func]{remove}
```
=== "TypeScript"
```typescript title="linked_list.ts"
/* 在链表的结点 n0 之后插入结点 P */
function insert(n0: ListNode, P: ListNode): void {
const n1 = n0.next;
n0.next = P;
P.next = n1;
}
/* 删除链表的结点 n0 之后的首个结点 */
function remove(n0: ListNode): void {
if (!n0.next) {
return;
}
// n0 -> P -> n1
const P = n0.next;
const n1 = P.next;
n0.next = n1;
}
[class]{}-[func]{insert}
[class]{}-[func]{remove}
```
=== "C"
@ -443,24 +387,9 @@ comments: true
=== "Swift"
```swift title="linked_list.swift"
/* 在链表的结点 n0 之后插入结点 P */
func insert(n0: ListNode, P: ListNode) {
let n1 = n0.next
n0.next = P
P.next = n1
}
[class]{}-[func]{insert}
/* 删除链表的结点 n0 之后的首个结点 */
func remove(n0: ListNode) {
if n0.next == nil {
return
}
// n0 -> P -> n1
let P = n0.next
let n1 = P?.next
n0.next = n1
P?.next = nil
}
[class]{}-[func]{remove}
```
=== "Zig"
@ -508,16 +437,7 @@ comments: true
=== "Go"
```go title="linked_list.go"
/* 访问链表中索引为 index 的结点 */
func access(head *ListNode, index int) *ListNode {
for i := 0; i < index; i++ {
if head == nil {
return nil
}
head = head.Next
}
return head
}
[class]{}-[func]{access}
```
=== "JavaScript"
@ -592,18 +512,7 @@ comments: true
=== "Go"
```go title="linked_list.go"
/* 在链表中查找值为 target 的首个结点 */
func find(head *ListNode, target int) int {
index := 0
for head != nil {
if head.Val == target {
return index
}
head = head.Next
index++
}
return -1
}
[class]{}-[func]{findNode}
```
=== "JavaScript"

View File

@ -734,103 +734,7 @@ comments: true
=== "Go"
```go title="my_list.go"
/* 列表类简易实现 */
type myList struct {
numsCapacity int
nums []int
numsSize int
extendRatio int
}
/* 构造函数 */
func newMyList() *myList {
return &myList{
numsCapacity: 10, // 列表容量
nums: make([]int, 10), // 数组(存储列表元素)
numsSize: 0, // 列表长度(即当前元素数量)
extendRatio: 2, // 每次列表扩容的倍数
}
}
/* 获取列表长度(即当前元素数量) */
func (l *myList) size() int {
return l.numsSize
}
/* 获取列表容量 */
func (l *myList) capacity() int {
return l.numsCapacity
}
/* 访问元素 */
func (l *myList) get(index int) int {
// 索引如果越界则抛出异常,下同
if index < 0 || index >= l.numsSize {
panic("索引越界")
}
return l.nums[index]
}
/* 更新元素 */
func (l *myList) set(num, index int) {
if index < 0 || index >= l.numsSize {
panic("索引越界")
}
l.nums[index] = num
}
/* 尾部添加元素 */
func (l *myList) add(num int) {
// 元素数量超出容量时,触发扩容机制
if l.numsSize == l.numsCapacity {
l.extendCapacity()
}
l.nums[l.numsSize] = num
// 更新元素数量
l.numsSize++
}
/* 中间插入元素 */
func (l *myList) insert(num, index int) {
if index < 0 || index >= l.numsSize {
panic("索引越界")
}
// 元素数量超出容量时,触发扩容机制
if l.numsSize == l.numsCapacity {
l.extendCapacity()
}
// 索引 i 以及之后的元素都向后移动一位
for j := l.numsSize - 1; j >= index; j-- {
l.nums[j+1] = l.nums[j]
}
l.nums[index] = num
// 更新元素数量
l.numsSize++
}
/* 删除元素 */
func (l *myList) remove(index int) int {
if index < 0 || index >= l.numsSize {
panic("索引越界")
}
num := l.nums[index]
// 索引 i 之后的元素都向前移动一位
for j := index; j < l.numsSize-1; j++ {
l.nums[j] = l.nums[j+1]
}
// 更新元素数量
l.numsSize--
// 返回被删除元素
return num
}
/* 列表扩容 */
func (l *myList) extendCapacity() {
// 新建一个长度为 self.__size 的数组,并将原数组拷贝到新数组
l.nums = append(l.nums, make([]int, l.numsCapacity*(l.extendRatio-1))...)
// 更新列表容量
l.numsCapacity = len(l.nums)
}
[class]{myList}-[func]{}
```
=== "JavaScript"