diff --git a/codes/go/chapter_array_and_linkedlist/my_list.go b/codes/go/chapter_array_and_linkedlist/my_list.go index 8f13630be..134096d0a 100644 --- a/codes/go/chapter_array_and_linkedlist/my_list.go +++ b/codes/go/chapter_array_and_linkedlist/my_list.go @@ -5,7 +5,7 @@ package chapter_array_and_linkedlist /* 列表类简易实现 */ -type MyList struct { +type myList struct { numsCapacity int nums []int numsSize int @@ -13,8 +13,8 @@ type MyList struct { } /* 构造函数 */ -func newMyList() *MyList { - return &MyList{ +func newMyList() *myList { + return &myList{ numsCapacity: 10, // 列表容量 nums: make([]int, 10), // 数组(存储列表元素) numsSize: 0, // 列表长度(即当前元素数量) @@ -23,17 +23,17 @@ func newMyList() *MyList { } /* 获取列表长度(即当前元素数量) */ -func (l *MyList) size() int { +func (l *myList) size() int { return l.numsSize } /* 获取列表容量 */ -func (l *MyList) capacity() int { +func (l *myList) capacity() int { return l.numsCapacity } /* 访问元素 */ -func (l *MyList) get(index int) int { +func (l *myList) get(index int) int { // 索引如果越界则抛出异常,下同 if index >= l.numsSize { panic("索引越界") @@ -42,7 +42,7 @@ func (l *MyList) get(index int) int { } /* 更新元素 */ -func (l *MyList) set(num, index int) { +func (l *myList) set(num, index int) { if index >= l.numsSize { panic("索引越界") } @@ -50,7 +50,7 @@ func (l *MyList) set(num, index int) { } /* 尾部添加元素 */ -func (l *MyList) add(num int) { +func (l *myList) add(num int) { // 元素数量超出容量时,触发扩容机制 if l.numsSize == l.numsCapacity { l.extendCapacity() @@ -61,7 +61,7 @@ func (l *MyList) add(num int) { } /* 中间插入元素 */ -func (l *MyList) insert(num, index int) { +func (l *myList) insert(num, index int) { if index >= l.numsSize { panic("索引越界") } @@ -79,7 +79,7 @@ func (l *MyList) insert(num, index int) { } /* 删除元素 */ -func (l *MyList) remove(index int) int { +func (l *myList) remove(index int) int { if index >= l.numsSize { panic("索引越界") } @@ -95,7 +95,7 @@ func (l *MyList) remove(index int) int { } /* 列表扩容 */ -func (l *MyList) extendCapacity() { +func (l *myList) extendCapacity() { // 新建一个长度为 self.__size 的数组,并将原数组拷贝到新数组 l.nums = append(l.nums, make([]int, l.numsCapacity*(l.extendRatio-1))...) // 更新列表容量 @@ -103,7 +103,7 @@ func (l *MyList) extendCapacity() { } /* 返回有效长度的列表 */ -func (l *MyList) toArray() []int { +func (l *myList) toArray() []int { // 仅转换有效长度范围内的列表元素 return l.nums[:l.numsSize] } diff --git a/codes/go/chapter_computational_complexity/space_complexity.go b/codes/go/chapter_computational_complexity/space_complexity.go index eb3ebec97..93f5b3380 100644 --- a/codes/go/chapter_computational_complexity/space_complexity.go +++ b/codes/go/chapter_computational_complexity/space_complexity.go @@ -9,31 +9,31 @@ import ( "strconv" ) -/* Node 结构体 */ -type Node struct { +/* 结构体 */ +type node struct { val int - next *Node + next *node } -/* TreeNode 二叉树 */ -type TreeNode struct { +/* treeNode 二叉树 */ +type treeNode struct { val int - left *TreeNode - right *TreeNode + left *treeNode + right *treeNode } -/* 创建 Node 结构体 */ -func newNode(val int) *Node { - return &Node{val: val} +/* 创建 node 结构体 */ +func newNode(val int) *node { + return &node{val: val} } -/* 创建 TreeNode 结构体 */ -func newTreeNode(val int) *TreeNode { - return &TreeNode{val: val} +/* 创建 treeNode 结构体 */ +func newTreeNode(val int) *treeNode { + return &treeNode{val: val} } /* 输出二叉树 */ -func printTree(root *TreeNode) { +func printTree(root *treeNode) { if root == nil { return } @@ -72,7 +72,7 @@ func spaceLinear(n int) { // 长度为 n 的数组占用 O(n) 空间 _ = make([]int, n) // 长度为 n 的列表占用 O(n) 空间 - var nodes []*Node + var nodes []*node for i := 0; i < n; i++ { nodes = append(nodes, newNode(i)) } @@ -112,7 +112,7 @@ func spaceQuadraticRecur(n int) int { } /* 指数阶(建立满二叉树) */ -func buildTree(n int) *TreeNode { +func buildTree(n int) *treeNode { if n == 0 { return nil } diff --git a/codes/go/chapter_hashing/array_hash_map.go b/codes/go/chapter_hashing/array_hash_map.go index a962b6a4d..5fb1480cc 100644 --- a/codes/go/chapter_hashing/array_hash_map.go +++ b/codes/go/chapter_hashing/array_hash_map.go @@ -7,30 +7,30 @@ package chapter_hashing import "fmt" /* 键值对 int->String */ -type Entry struct { +type entry struct { key int val string } /* 基于数组简易实现的哈希表 */ -type ArrayHashMap struct { - bucket []*Entry +type arrayHashMap struct { + bucket []*entry } -func newArrayHashMap() *ArrayHashMap { +func newArrayHashMap() *arrayHashMap { // 初始化一个长度为 100 的桶(数组) - bucket := make([]*Entry, 100) - return &ArrayHashMap{bucket: bucket} + bucket := make([]*entry, 100) + return &arrayHashMap{bucket: bucket} } /* 哈希函数 */ -func (a *ArrayHashMap) hashFunc(key int) int { +func (a *arrayHashMap) hashFunc(key int) int { index := key % 100 return index } /* 查询操作 */ -func (a *ArrayHashMap) get(key int) string { +func (a *arrayHashMap) get(key int) string { index := a.hashFunc(key) pair := a.bucket[index] if pair == nil { @@ -40,22 +40,22 @@ func (a *ArrayHashMap) get(key int) string { } /* 添加操作 */ -func (a *ArrayHashMap) put(key int, val string) { - pair := &Entry{key: key, val: val} +func (a *arrayHashMap) put(key int, val string) { + pair := &entry{key: key, val: val} index := a.hashFunc(key) a.bucket[index] = pair } /* 删除操作 */ -func (a *ArrayHashMap) remove(key int) { +func (a *arrayHashMap) remove(key int) { index := a.hashFunc(key) // 置为 nil ,代表删除 a.bucket[index] = nil } /* 获取所有键对 */ -func (a *ArrayHashMap) entrySet() []*Entry { - var pairs []*Entry +func (a *arrayHashMap) entrySet() []*entry { + var pairs []*entry for _, pair := range a.bucket { if pair != nil { pairs = append(pairs, pair) @@ -65,7 +65,7 @@ func (a *ArrayHashMap) entrySet() []*Entry { } /* 获取所有键 */ -func (a *ArrayHashMap) keySet() []int { +func (a *arrayHashMap) keySet() []int { var keys []int for _, pair := range a.bucket { if pair != nil { @@ -76,7 +76,7 @@ func (a *ArrayHashMap) keySet() []int { } /* 获取所有值 */ -func (a *ArrayHashMap) valueSet() []string { +func (a *arrayHashMap) valueSet() []string { var values []string for _, pair := range a.bucket { if pair != nil { @@ -87,7 +87,7 @@ func (a *ArrayHashMap) valueSet() []string { } /* 打印哈希表 */ -func (a *ArrayHashMap) print() { +func (a *arrayHashMap) print() { for _, pair := range a.bucket { if pair != nil { fmt.Println(pair.key, "->", pair.val) diff --git a/codes/go/chapter_searching/hashing_search_test.go b/codes/go/chapter_searching/hashing_search_test.go index 4bccd038f..18cf5c651 100644 --- a/codes/go/chapter_searching/hashing_search_test.go +++ b/codes/go/chapter_searching/hashing_search_test.go @@ -6,8 +6,9 @@ package chapter_searching import ( "fmt" - . "github.com/krahets/hello-algo/pkg" "testing" + + . "github.com/krahets/hello-algo/pkg" ) func TestHashingSearch(t *testing.T) { diff --git a/codes/go/chapter_sorting/merge_sort.go b/codes/go/chapter_sorting/merge_sort.go index 1b3a3932d..43aff01a7 100644 --- a/codes/go/chapter_sorting/merge_sort.go +++ b/codes/go/chapter_sorting/merge_sort.go @@ -8,25 +8,25 @@ package chapter_sorting // 左子数组区间 [left, mid] // 右子数组区间 [mid + 1, right] func merge(nums []int, left, mid, right int) { - // 初始化辅助数组 借助 copy模块 + // 初始化辅助数组 借助 copy 模块 tmp := make([]int, right-left+1) for i := left; i <= right; i++ { tmp[i-left] = nums[i] } // 左子数组的起始索引和结束索引 - left_start, left_end := left-left, mid-left + leftStart, leftEnd := left-left, mid-left // 右子数组的起始索引和结束索引 - right_start, right_end := mid+1-left, right-left + rightStart, rightEnd := mid+1-left, right-left // i, j 分别指向左子数组、右子数组的首元素 - i, j := left_start, right_start + i, j := leftStart, rightStart // 通过覆盖原数组 nums 来合并左子数组和右子数组 for k := left; k <= right; k++ { // 若“左子数组已全部合并完”,则选取右子数组元素,并且 j++ - if i > left_end { + if i > leftEnd { nums[k] = tmp[j] j++ // 否则,若“右子数组已全部合并完”或“左子数组元素 <= 右子数组元素”,则选取左子数组元素,并且 i++ - } else if j > right_end || tmp[i] <= tmp[j] { + } else if j > rightEnd || tmp[i] <= tmp[j] { nums[k] = tmp[i] i++ // 否则,若“左右子数组都未全部合并完”且“左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++ diff --git a/codes/go/chapter_sorting/quick_sort.go b/codes/go/chapter_sorting/quick_sort.go index 88aa14264..853bce452 100644 --- a/codes/go/chapter_sorting/quick_sort.go +++ b/codes/go/chapter_sorting/quick_sort.go @@ -5,16 +5,16 @@ package chapter_sorting // 快速排序 -type QuickSort struct{} +type quickSort struct{} // 快速排序(中位基准数优化) -type QuickSortMedian struct{} +type quickSortMedian struct{} // 快速排序(尾递归优化) -type QuickSortTailCall struct{} +type quickSortTailCall struct{} /* 哨兵划分 */ -func (q *QuickSort) partition(nums []int, left, right int) int { +func (q *quickSort) partition(nums []int, left, right int) int { // 以 nums[left] 作为基准数 i, j := left, right for i < j { @@ -33,7 +33,7 @@ func (q *QuickSort) partition(nums []int, left, right int) int { } /* 快速排序 */ -func (q *QuickSort) quickSort(nums []int, left, right int) { +func (q *quickSort) quickSort(nums []int, left, right int) { // 子数组长度为 1 时终止递归 if left >= right { return @@ -46,7 +46,7 @@ func (q *QuickSort) quickSort(nums []int, left, right int) { } /* 选取三个元素的中位数 */ -func (q *QuickSortMedian) medianThree(nums []int, left, mid, right int) int { +func (q *quickSortMedian) medianThree(nums []int, left, mid, right int) int { if (nums[left] > nums[mid]) != (nums[left] > nums[right]) { return left } else if (nums[mid] < nums[left]) != (nums[mid] > nums[right]) { @@ -56,7 +56,7 @@ func (q *QuickSortMedian) medianThree(nums []int, left, mid, right int) int { } /* 哨兵划分(三数取中值)*/ -func (q *QuickSortMedian) partition(nums []int, left, right int) int { +func (q *quickSortMedian) partition(nums []int, left, right int) int { // 以 nums[left] 作为基准数 med := q.medianThree(nums, left, (left+right)/2, right) // 将中位数交换至数组最左端 @@ -79,7 +79,7 @@ func (q *QuickSortMedian) partition(nums []int, left, right int) int { } /* 快速排序 */ -func (q *QuickSortMedian) quickSort(nums []int, left, right int) { +func (q *quickSortMedian) quickSort(nums []int, left, right int) { // 子数组长度为 1 时终止递归 if left >= right { return @@ -92,7 +92,7 @@ func (q *QuickSortMedian) quickSort(nums []int, left, right int) { } /* 哨兵划分 */ -func (q *QuickSortTailCall) partition(nums []int, left, right int) int { +func (q *quickSortTailCall) partition(nums []int, left, right int) int { // 以 nums[left] 作为基准数 i, j := left, right for i < j { @@ -111,7 +111,7 @@ func (q *QuickSortTailCall) partition(nums []int, left, right int) int { } /* 快速排序(尾递归优化)*/ -func (q *QuickSortTailCall) quickSort(nums []int, left, right int) { +func (q *quickSortTailCall) quickSort(nums []int, left, right int) { // 子数组长度为 1 时终止 for left < right { // 哨兵划分操作 diff --git a/codes/go/chapter_sorting/quick_sort_test.go b/codes/go/chapter_sorting/quick_sort_test.go index 86ae0115a..d780663e1 100644 --- a/codes/go/chapter_sorting/quick_sort_test.go +++ b/codes/go/chapter_sorting/quick_sort_test.go @@ -11,7 +11,7 @@ import ( // 快速排序 func TestQuickSort(t *testing.T) { - q := QuickSort{} + q := quickSort{} nums := []int{4, 1, 3, 1, 5, 2} q.quickSort(nums, 0, len(nums)-1) fmt.Println("快速排序完成后 nums = ", nums) @@ -19,7 +19,7 @@ func TestQuickSort(t *testing.T) { // 快速排序(中位基准数优化) func TestQuickSortMedian(t *testing.T) { - q := QuickSortMedian{} + q := quickSortMedian{} nums := []int{4, 1, 3, 1, 5, 2} q.quickSort(nums, 0, len(nums)-1) fmt.Println("快速排序(中位基准数优化)完成后 nums = ", nums) @@ -27,7 +27,7 @@ func TestQuickSortMedian(t *testing.T) { // 快速排序(尾递归优化) func TestQuickSortTailCall(t *testing.T) { - q := QuickSortTailCall{} + q := quickSortTailCall{} nums := []int{4, 1, 3, 1, 5, 2} q.quickSort(nums, 0, len(nums)-1) fmt.Println("快速排序(尾递归优化)完成后 nums = ", nums) diff --git a/codes/go/chapter_stack_and_queue/array_queue.go b/codes/go/chapter_stack_and_queue/array_queue.go index 863768ba8..7d0367df8 100644 --- a/codes/go/chapter_stack_and_queue/array_queue.go +++ b/codes/go/chapter_stack_and_queue/array_queue.go @@ -5,16 +5,16 @@ package chapter_stack_and_queue /* 基于环形数组实现的队列 */ -type ArrayQueue struct { +type arrayQueue struct { data []int // 用于存储队列元素的数组 capacity int // 队列容量(即最多容量的元素个数) front int // 头指针,指向队首 rear int // 尾指针,指向队尾 + 1 } -// NewArrayQueue 基于环形数组实现的队列 -func NewArrayQueue(capacity int) *ArrayQueue { - return &ArrayQueue{ +// newArrayQueue 基于环形数组实现的队列 +func newArrayQueue(capacity int) *arrayQueue { + return &arrayQueue{ data: make([]int, capacity), capacity: capacity, front: 0, @@ -22,21 +22,21 @@ func NewArrayQueue(capacity int) *ArrayQueue { } } -// Size 获取队列的长度 -func (q *ArrayQueue) Size() int { +// size 获取队列的长度 +func (q *arrayQueue) size() int { size := (q.capacity + q.rear - q.front) % q.capacity return size } -// IsEmpty 判断队列是否为空 -func (q *ArrayQueue) IsEmpty() bool { +// isEmpty 判断队列是否为空 +func (q *arrayQueue) isEmpty() bool { return q.rear-q.front == 0 } -// Offer 入队 -func (q *ArrayQueue) Offer(v int) { +// offer 入队 +func (q *arrayQueue) offer(v int) { // 当 rear == capacity 表示队列已满 - if q.Size() == q.capacity { + if q.size() == q.capacity { return } // 尾结点后添加 @@ -45,9 +45,9 @@ func (q *ArrayQueue) Offer(v int) { q.rear = (q.rear + 1) % q.capacity } -// Poll 出队 -func (q *ArrayQueue) Poll() any { - if q.IsEmpty() { +// poll 出队 +func (q *arrayQueue) poll() any { + if q.isEmpty() { return nil } v := q.data[q.front] @@ -56,9 +56,9 @@ func (q *ArrayQueue) Poll() any { return v } -// Peek 访问队首元素 -func (q *ArrayQueue) Peek() any { - if q.IsEmpty() { +// peek 访问队首元素 +func (q *arrayQueue) peek() any { + if q.isEmpty() { return nil } v := q.data[q.front] @@ -66,6 +66,6 @@ func (q *ArrayQueue) Peek() any { } // 获取 Slice 用于打印 -func (s *ArrayQueue) toSlice() []int { - return s.data[s.front:s.rear] +func (q *arrayQueue) toSlice() []int { + return q.data[q.front:q.rear] } diff --git a/codes/go/chapter_stack_and_queue/array_stack.go b/codes/go/chapter_stack_and_queue/array_stack.go index dca97404d..ef32ebe4c 100644 --- a/codes/go/chapter_stack_and_queue/array_stack.go +++ b/codes/go/chapter_stack_and_queue/array_stack.go @@ -5,47 +5,47 @@ package chapter_stack_and_queue /* 基于数组实现的栈 */ -type ArrayStack struct { +type arrayStack struct { data []int // 数据 } -func NewArrayStack() *ArrayStack { - return &ArrayStack{ +func newArrayStack() *arrayStack { + return &arrayStack{ // 设置栈的长度为 0,容量为 16 data: make([]int, 0, 16), } } -// Size 栈的长度 -func (s *ArrayStack) Size() int { +// size 栈的长度 +func (s *arrayStack) size() int { return len(s.data) } -// IsEmpty 栈是否为空 -func (s *ArrayStack) IsEmpty() bool { - return s.Size() == 0 +// isEmpty 栈是否为空 +func (s *arrayStack) isEmpty() bool { + return s.size() == 0 } -// Push 入栈 -func (s *ArrayStack) Push(v int) { +// push 入栈 +func (s *arrayStack) push(v int) { // 切片会自动扩容 s.data = append(s.data, v) } -// Pop 出栈 -func (s *ArrayStack) Pop() any { +// pop 出栈 +func (s *arrayStack) pop() any { // 弹出栈前,先判断是否为空 - if s.IsEmpty() { + if s.isEmpty() { return nil } - val := s.Peek() + val := s.peek() s.data = s.data[:len(s.data)-1] return val } -// Peek 获取栈顶元素 -func (s *ArrayStack) Peek() any { - if s.IsEmpty() { +// peek 获取栈顶元素 +func (s *arrayStack) peek() any { + if s.isEmpty() { return nil } val := s.data[len(s.data)-1] @@ -53,6 +53,6 @@ func (s *ArrayStack) Peek() any { } // 获取 Slice 用于打印 -func (s *ArrayStack) toSlice() []int { +func (s *arrayStack) toSlice() []int { return s.data } diff --git a/codes/go/chapter_stack_and_queue/deque_test.go b/codes/go/chapter_stack_and_queue/deque_test.go index 647ac642e..4c0f2f533 100644 --- a/codes/go/chapter_stack_and_queue/deque_test.go +++ b/codes/go/chapter_stack_and_queue/deque_test.go @@ -51,48 +51,48 @@ func TestDeque(t *testing.T) { func TestLinkedListDeque(t *testing.T) { // 初始化队列 - deque := NewLinkedListDeque() + deque := newLinkedListDeque() // 元素入队 - deque.OfferLast(2) - deque.OfferLast(5) - deque.OfferLast(4) - deque.OfferFirst(3) - deque.OfferFirst(1) + deque.offerLast(2) + deque.offerLast(5) + deque.offerLast(4) + deque.offerFirst(3) + deque.offerFirst(1) fmt.Print("队列 deque = ") PrintList(deque.toList()) // 访问队首元素 - front := deque.PeekFirst() + front := deque.peekFirst() fmt.Println("队首元素 front =", front) - rear := deque.PeekLast() + rear := deque.peekLast() fmt.Println("队尾元素 rear =", rear) // 元素出队 - pollFirst := deque.PollFirst() + pollFirst := deque.pollFirst() fmt.Print("队首出队元素 pollFirst = ", pollFirst, ",队首出队后 deque = ") PrintList(deque.toList()) - pollLast := deque.PollLast() + pollLast := deque.pollLast() fmt.Print("队尾出队元素 pollLast = ", pollLast, ",队尾出队后 deque = ") PrintList(deque.toList()) // 获取队的长度 - size := deque.Size() + size := deque.size() fmt.Println("队的长度 size =", size) // 判断是否为空 - isEmpty := deque.IsEmpty() + isEmpty := deque.isEmpty() fmt.Println("队是否为空 =", isEmpty) } // BenchmarkArrayQueue 67.92 ns/op in Mac M1 Pro func BenchmarkLinkedListDeque(b *testing.B) { - stack := NewLinkedListDeque() + stack := newLinkedListDeque() // use b.N for looping for i := 0; i < b.N; i++ { - stack.OfferLast(777) + stack.offerLast(777) } for i := 0; i < b.N; i++ { - stack.PollFirst() + stack.pollFirst() } } diff --git a/codes/go/chapter_stack_and_queue/linkedlist_deque.go b/codes/go/chapter_stack_and_queue/linkedlist_deque.go index 019aa778b..b1db77ca6 100644 --- a/codes/go/chapter_stack_and_queue/linkedlist_deque.go +++ b/codes/go/chapter_stack_and_queue/linkedlist_deque.go @@ -8,31 +8,31 @@ import ( "container/list" ) -// LinkedListDeque 基于链表实现的双端队列, 使用内置包 list 来实现栈 -type LinkedListDeque struct { +// linkedListDeque 基于链表实现的双端队列, 使用内置包 list 来实现栈 +type linkedListDeque struct { data *list.List } -// NewLinkedListDeque 初始化双端队列 -func NewLinkedListDeque() *LinkedListDeque { - return &LinkedListDeque{ +// newLinkedListDeque 初始化双端队列 +func newLinkedListDeque() *linkedListDeque { + return &linkedListDeque{ data: list.New(), } } -// OfferFirst 队首元素入队 -func (s *LinkedListDeque) OfferFirst(value any) { +// offerFirst 队首元素入队 +func (s *linkedListDeque) offerFirst(value any) { s.data.PushFront(value) } -// OfferLast 队尾元素入队 -func (s *LinkedListDeque) OfferLast(value any) { +// offerLast 队尾元素入队 +func (s *linkedListDeque) offerLast(value any) { s.data.PushBack(value) } -// PollFirst 队首元素出队 -func (s *LinkedListDeque) PollFirst() any { - if s.IsEmpty() { +// pollFirst 队首元素出队 +func (s *linkedListDeque) pollFirst() any { + if s.isEmpty() { return nil } e := s.data.Front() @@ -40,9 +40,9 @@ func (s *LinkedListDeque) PollFirst() any { return e.Value } -// PollLast 队尾元素出队 -func (s *LinkedListDeque) PollLast() any { - if s.IsEmpty() { +// pollLast 队尾元素出队 +func (s *linkedListDeque) pollLast() any { + if s.isEmpty() { return nil } e := s.data.Back() @@ -50,35 +50,35 @@ func (s *LinkedListDeque) PollLast() any { return e.Value } -// PeekFirst 访问队首元素 -func (s *LinkedListDeque) PeekFirst() any { - if s.IsEmpty() { +// peekFirst 访问队首元素 +func (s *linkedListDeque) peekFirst() any { + if s.isEmpty() { return nil } e := s.data.Front() return e.Value } -// PeekLast 访问队尾元素 -func (s *LinkedListDeque) PeekLast() any { - if s.IsEmpty() { +// peekLast 访问队尾元素 +func (s *linkedListDeque) peekLast() any { + if s.isEmpty() { return nil } e := s.data.Back() return e.Value } -// Size 获取队列的长度 -func (s *LinkedListDeque) Size() int { +// size 获取队列的长度 +func (s *linkedListDeque) size() int { return s.data.Len() } -// IsEmpty 判断队列是否为空 -func (s *LinkedListDeque) IsEmpty() bool { +// isEmpty 判断队列是否为空 +func (s *linkedListDeque) isEmpty() bool { return s.data.Len() == 0 } // 获取 List 用于打印 -func (s *LinkedListDeque) toList() *list.List { +func (s *linkedListDeque) toList() *list.List { return s.data } diff --git a/codes/go/chapter_stack_and_queue/linkedlist_queue.go b/codes/go/chapter_stack_and_queue/linkedlist_queue.go index d30461805..f5d164a82 100644 --- a/codes/go/chapter_stack_and_queue/linkedlist_queue.go +++ b/codes/go/chapter_stack_and_queue/linkedlist_queue.go @@ -9,26 +9,26 @@ import ( ) /* 基于链表实现的队列 */ -type LinkedListQueue struct { +type linkedListQueue struct { // 使用内置包 list 来实现队列 data *list.List } -// NewLinkedListQueue 初始化链表 -func NewLinkedListQueue() *LinkedListQueue { - return &LinkedListQueue{ +// newLinkedListQueue 初始化链表 +func newLinkedListQueue() *linkedListQueue { + return &linkedListQueue{ data: list.New(), } } -// Offer 入队 -func (s *LinkedListQueue) Offer(value any) { +// offer 入队 +func (s *linkedListQueue) offer(value any) { s.data.PushBack(value) } -// Poll 出队 -func (s *LinkedListQueue) Poll() any { - if s.IsEmpty() { +// poll 出队 +func (s *linkedListQueue) poll() any { + if s.isEmpty() { return nil } e := s.data.Front() @@ -36,26 +36,26 @@ func (s *LinkedListQueue) Poll() any { return e.Value } -// Peek 访问队首元素 -func (s *LinkedListQueue) Peek() any { - if s.IsEmpty() { +// peek 访问队首元素 +func (s *linkedListQueue) peek() any { + if s.isEmpty() { return nil } e := s.data.Front() return e.Value } -// Size 获取队列的长度 -func (s *LinkedListQueue) Size() int { +// size 获取队列的长度 +func (s *linkedListQueue) size() int { return s.data.Len() } -// IsEmpty 判断队列是否为空 -func (s *LinkedListQueue) IsEmpty() bool { +// isEmpty 判断队列是否为空 +func (s *linkedListQueue) isEmpty() bool { return s.data.Len() == 0 } // 获取 List 用于打印 -func (s *LinkedListQueue) toList() *list.List { +func (s *linkedListQueue) toList() *list.List { return s.data } diff --git a/codes/go/chapter_stack_and_queue/linkedlist_stack.go b/codes/go/chapter_stack_and_queue/linkedlist_stack.go index ab8a06284..8509c2b83 100644 --- a/codes/go/chapter_stack_and_queue/linkedlist_stack.go +++ b/codes/go/chapter_stack_and_queue/linkedlist_stack.go @@ -9,26 +9,26 @@ import ( ) /* 基于链表实现的栈 */ -type LinkedListStack struct { +type linkedListStack struct { // 使用内置包 list 来实现栈 data *list.List } -// NewLinkedListStack 初始化链表 -func NewLinkedListStack() *LinkedListStack { - return &LinkedListStack{ +// newLinkedListStack 初始化链表 +func newLinkedListStack() *linkedListStack { + return &linkedListStack{ data: list.New(), } } -// Push 入栈 -func (s *LinkedListStack) Push(value int) { +// push 入栈 +func (s *linkedListStack) push(value int) { s.data.PushBack(value) } -// Pop 出栈 -func (s *LinkedListStack) Pop() any { - if s.IsEmpty() { +// pop 出栈 +func (s *linkedListStack) pop() any { + if s.isEmpty() { return nil } e := s.data.Back() @@ -36,26 +36,26 @@ func (s *LinkedListStack) Pop() any { return e.Value } -// Peek 访问栈顶元素 -func (s *LinkedListStack) Peek() any { - if s.IsEmpty() { +// peek 访问栈顶元素 +func (s *linkedListStack) peek() any { + if s.isEmpty() { return nil } e := s.data.Back() return e.Value } -// Size 获取栈的长度 -func (s *LinkedListStack) Size() int { +// size 获取栈的长度 +func (s *linkedListStack) size() int { return s.data.Len() } -// IsEmpty 判断栈是否为空 -func (s *LinkedListStack) IsEmpty() bool { +// isEmpty 判断栈是否为空 +func (s *linkedListStack) isEmpty() bool { return s.data.Len() == 0 } // 获取 List 用于打印 -func (s *LinkedListStack) toList() *list.List { +func (s *linkedListStack) toList() *list.List { return s.data } diff --git a/codes/go/chapter_stack_and_queue/queue_test.go b/codes/go/chapter_stack_and_queue/queue_test.go index 368becbc7..b9ec79df2 100644 --- a/codes/go/chapter_stack_and_queue/queue_test.go +++ b/codes/go/chapter_stack_and_queue/queue_test.go @@ -48,87 +48,87 @@ func TestQueue(t *testing.T) { func TestArrayQueue(t *testing.T) { // 初始化队列,使用队列的通用接口 capacity := 10 - queue := NewArrayQueue(capacity) + queue := newArrayQueue(capacity) // 元素入队 - queue.Offer(1) - queue.Offer(3) - queue.Offer(2) - queue.Offer(5) - queue.Offer(4) + queue.offer(1) + queue.offer(3) + queue.offer(2) + queue.offer(5) + queue.offer(4) fmt.Print("队列 queue = ") PrintSlice(queue.toSlice()) // 访问队首元素 - peek := queue.Peek() + peek := queue.peek() fmt.Println("队首元素 peek =", peek) // 元素出队 - poll := queue.Poll() + poll := queue.poll() fmt.Print("出队元素 poll = ", poll, ", 出队后 queue = ") PrintSlice(queue.toSlice()) // 获取队的长度 - size := queue.Size() + size := queue.size() fmt.Println("队的长度 size =", size) // 判断是否为空 - isEmpty := queue.IsEmpty() + isEmpty := queue.isEmpty() fmt.Println("队是否为空 =", isEmpty) } func TestLinkedListQueue(t *testing.T) { // 初始化队 - queue := NewLinkedListQueue() + queue := newLinkedListQueue() // 元素入队 - queue.Offer(1) - queue.Offer(3) - queue.Offer(2) - queue.Offer(5) - queue.Offer(4) + queue.offer(1) + queue.offer(3) + queue.offer(2) + queue.offer(5) + queue.offer(4) fmt.Print("队列 queue = ") PrintList(queue.toList()) // 访问队首元素 - peek := queue.Peek() + peek := queue.peek() fmt.Println("队首元素 peek =", peek) // 元素出队 - poll := queue.Poll() + poll := queue.poll() fmt.Print("出队元素 poll = ", poll, ", 出队后 queue = ") PrintList(queue.toList()) // 获取队的长度 - size := queue.Size() + size := queue.size() fmt.Println("队的长度 size =", size) // 判断是否为空 - isEmpty := queue.IsEmpty() + isEmpty := queue.isEmpty() fmt.Println("队是否为空 =", isEmpty) } // BenchmarkArrayQueue 8 ns/op in Mac M1 Pro func BenchmarkArrayQueue(b *testing.B) { capacity := 1000 - stack := NewArrayQueue(capacity) + stack := newArrayQueue(capacity) // use b.N for looping for i := 0; i < b.N; i++ { - stack.Offer(777) + stack.offer(777) } for i := 0; i < b.N; i++ { - stack.Poll() + stack.poll() } } // BenchmarkLinkedQueue 62.66 ns/op in Mac M1 Pro func BenchmarkLinkedQueue(b *testing.B) { - stack := NewLinkedListQueue() + stack := newLinkedListQueue() // use b.N for looping for i := 0; i < b.N; i++ { - stack.Offer(777) + stack.offer(777) } for i := 0; i < b.N; i++ { - stack.Poll() + stack.poll() } } diff --git a/codes/go/chapter_stack_and_queue/stack_test.go b/codes/go/chapter_stack_and_queue/stack_test.go index 9dc97e697..a4cf338b7 100644 --- a/codes/go/chapter_stack_and_queue/stack_test.go +++ b/codes/go/chapter_stack_and_queue/stack_test.go @@ -46,85 +46,85 @@ func TestStack(t *testing.T) { func TestArrayStack(t *testing.T) { // 初始化栈, 使用接口承接 - stack := NewArrayStack() + stack := newArrayStack() // 元素入栈 - stack.Push(1) - stack.Push(3) - stack.Push(2) - stack.Push(5) - stack.Push(4) + stack.push(1) + stack.push(3) + stack.push(2) + stack.push(5) + stack.push(4) fmt.Print("栈 stack = ") PrintSlice(stack.toSlice()) // 访问栈顶元素 - peek := stack.Peek() + peek := stack.peek() fmt.Println("栈顶元素 peek =", peek) // 元素出栈 - pop := stack.Pop() + pop := stack.pop() fmt.Print("出栈元素 pop = ", pop, ", 出栈后 stack = ") PrintSlice(stack.toSlice()) // 获取栈的长度 - size := stack.Size() + size := stack.size() fmt.Println("栈的长度 size =", size) // 判断是否为空 - isEmpty := stack.IsEmpty() + isEmpty := stack.isEmpty() fmt.Println("栈是否为空 =", isEmpty) } func TestLinkedListStack(t *testing.T) { // 初始化栈 - stack := NewLinkedListStack() + stack := newLinkedListStack() // 元素入栈 - stack.Push(1) - stack.Push(3) - stack.Push(2) - stack.Push(5) - stack.Push(4) + stack.push(1) + stack.push(3) + stack.push(2) + stack.push(5) + stack.push(4) fmt.Print("栈 stack = ") PrintList(stack.toList()) // 访问栈顶元素 - peek := stack.Peek() + peek := stack.peek() fmt.Println("栈顶元素 peek =", peek) // 元素出栈 - pop := stack.Pop() + pop := stack.pop() fmt.Print("出栈元素 pop = ", pop, ", 出栈后 stack = ") PrintList(stack.toList()) // 获取栈的长度 - size := stack.Size() + size := stack.size() fmt.Println("栈的长度 size =", size) // 判断是否为空 - isEmpty := stack.IsEmpty() + isEmpty := stack.isEmpty() fmt.Println("栈是否为空 =", isEmpty) } // BenchmarkArrayStack 8 ns/op in Mac M1 Pro func BenchmarkArrayStack(b *testing.B) { - stack := NewArrayStack() + stack := newArrayStack() // use b.N for looping for i := 0; i < b.N; i++ { - stack.Push(777) + stack.push(777) } for i := 0; i < b.N; i++ { - stack.Pop() + stack.pop() } } // BenchmarkLinkedListStack 65.02 ns/op in Mac M1 Pro func BenchmarkLinkedListStack(b *testing.B) { - stack := NewLinkedListStack() + stack := newLinkedListStack() // use b.N for looping for i := 0; i < b.N; i++ { - stack.Push(777) + stack.push(777) } for i := 0; i < b.N; i++ { - stack.Pop() + stack.pop() } } diff --git a/codes/go/chapter_tree/binary_search_tree.go b/codes/go/chapter_tree/binary_search_tree.go index c8fc5d62c..61ed400d5 100644 --- a/codes/go/chapter_tree/binary_search_tree.go +++ b/codes/go/chapter_tree/binary_search_tree.go @@ -10,26 +10,26 @@ import ( . "github.com/krahets/hello-algo/pkg" ) -type BinarySearchTree struct { +type binarySearchTree struct { root *TreeNode } -func NewBinarySearchTree(nums []int) *BinarySearchTree { +func newBinarySearchTree(nums []int) *binarySearchTree { // sorting array sort.Ints(nums) root := buildBinarySearchTree(nums, 0, len(nums)-1) - return &BinarySearchTree{ + return &binarySearchTree{ root: root, } } /* 获取根结点 */ -func (bst *BinarySearchTree) GetRoot() *TreeNode { +func (bst *binarySearchTree) getRoot() *TreeNode { return bst.root } /* 获取中序遍历的下一个结点 */ -func (bst *BinarySearchTree) GetInOrderNext(node *TreeNode) *TreeNode { +func (bst *binarySearchTree) getInOrderNext(node *TreeNode) *TreeNode { if node == nil { return node } @@ -41,7 +41,7 @@ func (bst *BinarySearchTree) GetInOrderNext(node *TreeNode) *TreeNode { } /* 查找结点 */ -func (bst *BinarySearchTree) Search(num int) *TreeNode { +func (bst *binarySearchTree) search(num int) *TreeNode { node := bst.root // 循环查找,越过叶结点后跳出 for node != nil { @@ -61,7 +61,7 @@ func (bst *BinarySearchTree) Search(num int) *TreeNode { } /* 插入结点 */ -func (bst *BinarySearchTree) Insert(num int) *TreeNode { +func (bst *binarySearchTree) insert(num int) *TreeNode { cur := bst.root // 若树为空,直接提前返回 if cur == nil { @@ -92,7 +92,7 @@ func (bst *BinarySearchTree) Insert(num int) *TreeNode { } /* 删除结点 */ -func (bst *BinarySearchTree) Remove(num int) *TreeNode { +func (bst *binarySearchTree) remove(num int) *TreeNode { cur := bst.root // 若树为空,直接提前返回 if cur == nil { @@ -136,10 +136,10 @@ func (bst *BinarySearchTree) Remove(num int) *TreeNode { // 子结点数为 2 } else { // 获取中序遍历中待删除结点 cur 的下一个结点 - next := bst.GetInOrderNext(cur) + next := bst.getInOrderNext(cur) temp := next.Val // 递归删除结点 next - bst.Remove(next.Val) + bst.remove(next.Val) // 将 next 的值复制给 cur cur.Val = temp } @@ -160,7 +160,7 @@ func buildBinarySearchTree(nums []int, left, right int) *TreeNode { return root } -// Print binary search tree -func (bst *BinarySearchTree) Print() { +// print binary search tree +func (bst *binarySearchTree) print() { PrintTree(bst.root) } diff --git a/codes/go/chapter_tree/binary_search_tree_test.go b/codes/go/chapter_tree/binary_search_tree_test.go index cdf7d8d48..877f1b541 100644 --- a/codes/go/chapter_tree/binary_search_tree_test.go +++ b/codes/go/chapter_tree/binary_search_tree_test.go @@ -11,31 +11,31 @@ import ( func TestBinarySearchTree(t *testing.T) { nums := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15} - bst := NewBinarySearchTree(nums) + bst := newBinarySearchTree(nums) fmt.Println("\n初始化的二叉树为:") - bst.Print() + bst.print() // 获取根结点 - node := bst.GetRoot() + node := bst.getRoot() fmt.Println("\n二叉树的根结点为:", node.Val) // 查找结点 - node = bst.Search(5) + node = bst.search(5) fmt.Println("\n查找到的结点对象为", node, ",结点值 =", node.Val) // 插入结点 - node = bst.Insert(16) + node = bst.insert(16) fmt.Println("\n插入结点后 16 的二叉树为:") - bst.Print() + bst.print() // 删除结点 - bst.Remove(1) + bst.remove(1) fmt.Println("\n删除结点 1 后的二叉树为:") - bst.Print() - bst.Remove(2) + bst.print() + bst.remove(2) fmt.Println("\n删除结点 2 后的二叉树为:") - bst.Print() - bst.Remove(4) + bst.print() + bst.remove(4) fmt.Println("\n删除结点 4 后的二叉树为:") - bst.Print() + bst.print() } diff --git a/codes/go/chapter_tree/binary_tree_bfs_test.go b/codes/go/chapter_tree/binary_tree_bfs_test.go index d32dbdca9..7b1bbf1f5 100644 --- a/codes/go/chapter_tree/binary_tree_bfs_test.go +++ b/codes/go/chapter_tree/binary_tree_bfs_test.go @@ -14,7 +14,7 @@ import ( func TestLevelOrder(t *testing.T) { /* 初始化二叉树 */ // 这里借助了一个从数组直接生成二叉树的函数 - root := ArrToTree([]int{1, 2, 3, 4, 5, 6, 7}) + root := ArrToTree([]any{1, 2, 3, 4, 5, 6, 7}) fmt.Println("\n初始化二叉树: ") PrintTree(root) diff --git a/codes/go/chapter_tree/binary_tree_dfs_test.go b/codes/go/chapter_tree/binary_tree_dfs_test.go index b0db8086c..9e0dfe22f 100644 --- a/codes/go/chapter_tree/binary_tree_dfs_test.go +++ b/codes/go/chapter_tree/binary_tree_dfs_test.go @@ -14,7 +14,7 @@ import ( func TestPreInPostOrderTraversal(t *testing.T) { /* 初始化二叉树 */ // 这里借助了一个从数组直接生成二叉树的函数 - root := ArrToTree([]int{1, 2, 3, 4, 5, 6, 7}) + root := ArrToTree([]any{1, 2, 3, 4, 5, 6, 7}) fmt.Println("\n初始化二叉树: ") PrintTree(root) diff --git a/codes/go/pkg/print_utils.go b/codes/go/pkg/print_utils.go index a42d48b2f..575ab6b94 100644 --- a/codes/go/pkg/print_utils.go +++ b/codes/go/pkg/print_utils.go @@ -76,7 +76,7 @@ func printTreeHelper(root *TreeNode, prev *trunk, isLeft bool) { printTreeHelper(root.Left, trunk, false) } -// trunk Help to Print tree structure +// trunk Help to print tree structure type trunk struct { prev *trunk str string @@ -103,4 +103,4 @@ func PrintMap[K comparable, V any](m map[K]V) { for key, value := range m { fmt.Println(key, "->", value) } -} \ No newline at end of file +} diff --git a/codes/go/pkg/tree_node.go b/codes/go/pkg/tree_node.go index 2ac2e22d1..6baed50eb 100644 --- a/codes/go/pkg/tree_node.go +++ b/codes/go/pkg/tree_node.go @@ -25,11 +25,12 @@ func NewTreeNode(v int) *TreeNode { } // ArrToTree Generate a binary tree given an array -func ArrToTree(arr []int) *TreeNode { +func ArrToTree(arr []any) *TreeNode { if len(arr) <= 0 { return nil } - root := NewTreeNode(arr[0]) + // TreeNode only accept integer value for now. + root := NewTreeNode(arr[0].(int)) // Let container.list as queue queue := list.New() queue.PushBack(root) @@ -39,13 +40,17 @@ func ArrToTree(arr []int) *TreeNode { node := queue.Remove(queue.Front()).(*TreeNode) i++ if i < len(arr) { - node.Left = NewTreeNode(arr[i]) - queue.PushBack(node.Left) + if arr[i] != nil { + node.Left = NewTreeNode(arr[i].(int)) + queue.PushBack(node.Left) + } } i++ if i < len(arr) { - node.Right = NewTreeNode(arr[i]) - queue.PushBack(node.Right) + if arr[i] != nil { + node.Right = NewTreeNode(arr[i].(int)) + queue.PushBack(node.Right) + } } } return root diff --git a/codes/go/pkg/tree_node_test.go b/codes/go/pkg/tree_node_test.go index bb1885ee1..ec7831bf7 100644 --- a/codes/go/pkg/tree_node_test.go +++ b/codes/go/pkg/tree_node_test.go @@ -10,7 +10,7 @@ import ( ) func TestTreeNode(t *testing.T) { - arr := []int{2, 3, 5, 6, 7} + arr := []any{1, 2, 3, nil, 5, 6, nil} node := ArrToTree(arr) // print tree diff --git a/docs/chapter_array_and_linkedlist/list.md b/docs/chapter_array_and_linkedlist/list.md index d1109d65b..ca4774a04 100644 --- a/docs/chapter_array_and_linkedlist/list.md +++ b/docs/chapter_array_and_linkedlist/list.md @@ -864,7 +864,7 @@ comments: true ```go title="my_list.go" /* 列表类简易实现 */ - type MyList struct { + type myList struct { numsCapacity int nums []int numsSize int @@ -872,8 +872,8 @@ comments: true } /* 构造函数 */ - func newMyList() *MyList { - return &MyList{ + func newMyList() *myList { + return &myList{ numsCapacity: 10, // 列表容量 nums: make([]int, 10), // 数组(存储列表元素) numsSize: 0, // 列表长度(即当前元素数量) @@ -882,17 +882,17 @@ comments: true } /* 获取列表长度(即当前元素数量) */ - func (l *MyList) size() int { + func (l *myList) size() int { return l.numsSize } /* 获取列表容量 */ - func (l *MyList) capacity() int { + func (l *myList) capacity() int { return l.numsCapacity } /* 访问元素 */ - func (l *MyList) get(index int) int { + func (l *myList) get(index int) int { // 索引如果越界则抛出异常,下同 if index >= l.numsSize { panic("索引越界") @@ -901,7 +901,7 @@ comments: true } /* 更新元素 */ - func (l *MyList) set(num, index int) { + func (l *myList) set(num, index int) { if index >= l.numsSize { panic("索引越界") } @@ -909,7 +909,7 @@ comments: true } /* 尾部添加元素 */ - func (l *MyList) add(num int) { + func (l *myList) add(num int) { // 元素数量超出容量时,触发扩容机制 if l.numsSize == l.numsCapacity { l.extendCapacity() @@ -920,7 +920,7 @@ comments: true } /* 中间插入元素 */ - func (l *MyList) insert(num, index int) { + func (l *myList) insert(num, index int) { if index >= l.numsSize { panic("索引越界") } @@ -938,20 +938,23 @@ comments: true } /* 删除元素 */ - func (l *MyList) Remove(index int) { + func (l *myList) remove(index int) int { if 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() { + func (l *myList) extendCapacity() { // 新建一个长度为 self.__size 的数组,并将原数组拷贝到新数组 l.nums = append(l.nums, make([]int, l.numsCapacity*(l.extendRatio-1))...) // 更新列表容量 diff --git a/docs/chapter_computational_complexity/space_complexity.md b/docs/chapter_computational_complexity/space_complexity.md index 939b61ecf..6686aac50 100644 --- a/docs/chapter_computational_complexity/space_complexity.md +++ b/docs/chapter_computational_complexity/space_complexity.md @@ -103,14 +103,14 @@ comments: true ```go title="" /* 结构体 */ - type Node struct { + type node struct { val int - next *Node + next *node } - - /* 创建 Node 结构体 */ - func newNode(val int) *Node { - return &Node{val: val} + + /* 创建 node 结构体 */ + func newNode(val int) *node { + return &node{val: val} } /* 函数 */ @@ -687,7 +687,7 @@ $$ // 长度为 n 的数组占用 O(n) 空间 _ = make([]int, n) // 长度为 n 的列表占用 O(n) 空间 - var nodes []*Node + var nodes []*node for i := 0; i < n; i++ { nodes = append(nodes, newNode(i)) } @@ -1108,7 +1108,7 @@ $$ ```go title="space_complexity.go" /* 指数阶(建立满二叉树) */ - func buildTree(n int) *TreeNode { + func buildTree(n int) *treeNode { if n == 0 { return nil } diff --git a/docs/chapter_hashing/hash_map.md b/docs/chapter_hashing/hash_map.md index 3a69d88fc..2d8a66a5d 100644 --- a/docs/chapter_hashing/hash_map.md +++ b/docs/chapter_hashing/hash_map.md @@ -524,30 +524,30 @@ $$ ```go title="array_hash_map.go" /* 键值对 int->String */ - type Entry struct { + type entry struct { key int val string } /* 基于数组简易实现的哈希表 */ - type ArrayHashMap struct { - bucket []*Entry + type arrayHashMap struct { + bucket []*entry } - func newArrayHashMap() *ArrayHashMap { + func newArrayHashMap() *arrayHashMap { // 初始化一个长度为 100 的桶(数组) - bucket := make([]*Entry, 100) - return &ArrayHashMap{bucket: bucket} + bucket := make([]*entry, 100) + return &arrayHashMap{bucket: bucket} } /* 哈希函数 */ - func (a *ArrayHashMap) hashFunc(key int) int { + func (a *arrayHashMap) hashFunc(key int) int { index := key % 100 return index } /* 查询操作 */ - func (a *ArrayHashMap) get(key int) string { + func (a *arrayHashMap) get(key int) string { index := a.hashFunc(key) pair := a.bucket[index] if pair == nil { @@ -557,16 +557,16 @@ $$ } /* 添加操作 */ - func (a *ArrayHashMap) put(key int, val string) { - pair := &Entry{key: key, val: val} + func (a *arrayHashMap) put(key int, val string) { + pair := &entry{key: key, val: val} index := a.hashFunc(key) a.bucket[index] = pair } /* 删除操作 */ - func (a *ArrayHashMap) remove(key int) { + func (a *arrayHashMap) remove(key int) { index := a.hashFunc(key) - // 置为空字符,代表删除 + // 置为 nil ,代表删除 a.bucket[index] = nil } ``` diff --git a/docs/chapter_sorting/merge_sort.md b/docs/chapter_sorting/merge_sort.md index 396baa089..ae0cfc7c2 100644 --- a/docs/chapter_sorting/merge_sort.md +++ b/docs/chapter_sorting/merge_sort.md @@ -201,36 +201,35 @@ comments: true 右子数组区间 [mid + 1, right] */ func merge(nums []int, left, mid, right int) { - // 初始化辅助数组 借助 copy模块 + // 初始化辅助数组 借助 copy 模块 tmp := make([]int, right-left+1) for i := left; i <= right; i++ { tmp[i-left] = nums[i] } // 左子数组的起始索引和结束索引 - left_start, left_end := left-left, mid-left + leftStart, leftEnd := left-left, mid-left // 右子数组的起始索引和结束索引 - right_start, right_end := mid+1-left, right-left + rightStart, rightEnd := mid+1-left, right-left // i, j 分别指向左子数组、右子数组的首元素 - i, j := left_start, right_start + i, j := leftStart, rightStart // 通过覆盖原数组 nums 来合并左子数组和右子数组 for k := left; k <= right; k++ { // 若“左子数组已全部合并完”,则选取右子数组元素,并且 j++ - if i > left_end { + if i > leftEnd { nums[k] = tmp[j] j++ - // 否则,若“右子数组已全部合并完”或“左子数组元素 <= 右子数组元素”,则选取左子数组元素,并且 i++ - } else if j > right_end || tmp[i] <= tmp[j] { + // 否则,若“右子数组已全部合并完”或“左子数组元素 <= 右子数组元素”,则选取左子数组元素,并且 i++ + } else if j > rightEnd || tmp[i] <= tmp[j] { nums[k] = tmp[i] i++ - // 否则,若“左右子数组都未全部合并完”且“左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++ + // 否则,若“左右子数组都未全部合并完”且“左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++ } else { nums[k] = tmp[j] j++ } } } - - /* 归并排序 */ + func mergeSort(nums []int, left, right int) { // 终止条件 if left >= right { diff --git a/docs/chapter_sorting/quick_sort.md b/docs/chapter_sorting/quick_sort.md index f8dcc3238..3eeac9c65 100644 --- a/docs/chapter_sorting/quick_sort.md +++ b/docs/chapter_sorting/quick_sort.md @@ -111,21 +111,21 @@ comments: true ```go title="quick_sort.go" /* 哨兵划分 */ func partition(nums []int, left, right int) int { - //以 nums[left] 作为基准数 + // 以 nums[left] 作为基准数 i, j := left, right for i < j { for i < j && nums[j] >= nums[left] { - j-- //从右向左找首个小于基准数的元素 + j-- // 从右向左找首个小于基准数的元素 } for i < j && nums[i] <= nums[left] { - i++ //从左向右找首个大于基准数的元素 + i++ // 从左向右找首个大于基准数的元素 } //元素交换 nums[i], nums[j] = nums[j], nums[i] } - //将基准数交换至两子数组的分界线 + // 将基准数交换至两子数组的分界线 nums[i], nums[left] = nums[left], nums[i] - return i //返回基准数的索引 + return i // 返回基准数的索引 } ``` diff --git a/docs/chapter_stack_and_queue/queue.md b/docs/chapter_stack_and_queue/queue.md index ce47edb38..0c589fa69 100644 --- a/docs/chapter_stack_and_queue/queue.md +++ b/docs/chapter_stack_and_queue/queue.md @@ -404,43 +404,49 @@ comments: true ```go title="linkedlist_queue.go" /* 基于链表实现的队列 */ - type LinkedListQueue struct { + type linkedListQueue struct { // 使用内置包 list 来实现队列 data *list.List } - // NewLinkedListQueue 初始化链表 - func NewLinkedListQueue() *LinkedListQueue { - return &LinkedListQueue{ + + // newLinkedListQueue 初始化链表 + func newLinkedListQueue() *linkedListQueue { + return &linkedListQueue{ data: list.New(), } } - // Offer 入队 - func (s *LinkedListQueue) Offer(value any) { + + // offer 入队 + func (s *linkedListQueue) offer(value any) { s.data.PushBack(value) } - // Poll 出队 - func (s *LinkedListQueue) Poll() any { - if s.IsEmpty() { + + // poll 出队 + func (s *linkedListQueue) poll() any { + if s.isEmpty() { return nil } e := s.data.Front() s.data.Remove(e) return e.Value } - // Peek 访问队首元素 - func (s *LinkedListQueue) Peek() any { - if s.IsEmpty() { + + // peek 访问队首元素 + func (s *linkedListQueue) peek() any { + if s.isEmpty() { return nil } e := s.data.Front() return e.Value } - // Size 获取队列的长度 - func (s *LinkedListQueue) Size() int { + + // size 获取队列的长度 + func (s *linkedListQueue) size() int { return s.data.Len() } - // IsEmpty 判断队列是否为空 - func (s *LinkedListQueue) IsEmpty() bool { + + // isEmpty 判断队列是否为空 + func (s *linkedListQueue) isEmpty() bool { return s.data.Len() == 0 } ``` @@ -805,34 +811,38 @@ comments: true ```go title="array_queue.go" /* 基于环形数组实现的队列 */ - type ArrayQueue struct { + type arrayQueue struct { data []int // 用于存储队列元素的数组 capacity int // 队列容量(即最多容量的元素个数) front int // 头指针,指向队首 rear int // 尾指针,指向队尾 + 1 } - // NewArrayQueue 基于环形数组实现的队列 - func NewArrayQueue(capacity int) *ArrayQueue { - return &ArrayQueue{ + + // newArrayQueue 基于环形数组实现的队列 + func newArrayQueue(capacity int) *arrayQueue { + return &arrayQueue{ data: make([]int, capacity), capacity: capacity, front: 0, rear: 0, } } - // Size 获取队列的长度 - func (q *ArrayQueue) Size() int { + + // size 获取队列的长度 + func (q *arrayQueue) size() int { size := (q.capacity + q.rear - q.front) % q.capacity return size } - // IsEmpty 判断队列是否为空 - func (q *ArrayQueue) IsEmpty() bool { + + // isEmpty 判断队列是否为空 + func (q *arrayQueue) isEmpty() bool { return q.rear-q.front == 0 } - // Offer 入队 - func (q *ArrayQueue) Offer(v int) { + + // offer 入队 + func (q *arrayQueue) offer(v int) { // 当 rear == capacity 表示队列已满 - if q.Size() == q.capacity { + if q.size() == q.capacity { return } // 尾结点后添加 @@ -840,9 +850,10 @@ comments: true // 尾指针向后移动一位,越过尾部后返回到数组头部 q.rear = (q.rear + 1) % q.capacity } - // Poll 出队 - func (q *ArrayQueue) Poll() any { - if q.IsEmpty() { + + // poll 出队 + func (q *arrayQueue) poll() any { + if q.isEmpty() { return nil } v := q.data[q.front] @@ -850,9 +861,10 @@ comments: true q.front = (q.front + 1) % q.capacity return v } - // Peek 访问队首元素 - func (q *ArrayQueue) Peek() any { - if q.IsEmpty() { + + // peek 访问队首元素 + func (q *arrayQueue) peek() any { + if q.isEmpty() { return nil } v := q.data[q.front] diff --git a/docs/chapter_stack_and_queue/stack.md b/docs/chapter_stack_and_queue/stack.md index 374de7424..8bb343c4f 100644 --- a/docs/chapter_stack_and_queue/stack.md +++ b/docs/chapter_stack_and_queue/stack.md @@ -378,43 +378,49 @@ comments: true ```go title="linkedlist_stack.go" /* 基于链表实现的栈 */ - type LinkedListStack struct { + type linkedListStack struct { // 使用内置包 list 来实现栈 data *list.List } - // NewLinkedListStack 初始化链表 - func NewLinkedListStack() *LinkedListStack { - return &LinkedListStack{ + + // newLinkedListStack 初始化链表 + func newLinkedListStack() *linkedListStack { + return &linkedListStack{ data: list.New(), } } - // Push 入栈 - func (s *LinkedListStack) Push(value int) { + + // push 入栈 + func (s *linkedListStack) push(value int) { s.data.PushBack(value) } - // Pop 出栈 - func (s *LinkedListStack) Pop() any { - if s.IsEmpty() { + + // pop 出栈 + func (s *linkedListStack) pop() any { + if s.isEmpty() { return nil } e := s.data.Back() s.data.Remove(e) return e.Value } - // Peek 访问栈顶元素 - func (s *LinkedListStack) Peek() any { - if s.IsEmpty() { + + // peek 访问栈顶元素 + func (s *linkedListStack) peek() any { + if s.isEmpty() { return nil } e := s.data.Back() return e.Value } - // Size 获取栈的长度 - func (s *LinkedListStack) Size() int { + + // size 获取栈的长度 + func (s *linkedListStack) size() int { return s.data.Len() } - // IsEmpty 判断栈是否为空 - func (s *LinkedListStack) IsEmpty() bool { + + // isEmpty 判断栈是否为空 + func (s *linkedListStack) isEmpty() bool { return s.data.Len() == 0 } ``` @@ -716,41 +722,47 @@ comments: true ```go title="array_stack.go" /* 基于数组实现的栈 */ - type ArrayStack struct { + type arrayStack struct { data []int // 数据 } - func NewArrayStack() *ArrayStack { - return &ArrayStack{ + + func newArrayStack() *arrayStack { + return &arrayStack{ // 设置栈的长度为 0,容量为 16 data: make([]int, 0, 16), } } - // Size 栈的长度 - func (s *ArrayStack) Size() int { + + // size 栈的长度 + func (s *arrayStack) size() int { return len(s.data) } - // IsEmpty 栈是否为空 - func (s *ArrayStack) IsEmpty() bool { - return s.Size() == 0 + + // isEmpty 栈是否为空 + func (s *arrayStack) isEmpty() bool { + return s.size() == 0 } - // Push 入栈 - func (s *ArrayStack) Push(v int) { + + // push 入栈 + func (s *arrayStack) push(v int) { // 切片会自动扩容 s.data = append(s.data, v) } - // Pop 出栈 - func (s *ArrayStack) Pop() any { + + // pop 出栈 + func (s *arrayStack) pop() any { // 弹出栈前,先判断是否为空 - if s.IsEmpty() { + if s.isEmpty() { return nil } - val := s.Peek() + val := s.peek() s.data = s.data[:len(s.data)-1] return val } - // Peek 获取栈顶元素 - func (s *ArrayStack) Peek() any { - if s.IsEmpty() { + + // peek 获取栈顶元素 + func (s *arrayStack) peek() any { + if s.isEmpty() { return nil } val := s.data[len(s.data)-1] diff --git a/docs/chapter_tree/binary_search_tree.md b/docs/chapter_tree/binary_search_tree.md index 979612852..e3ddfaaac 100644 --- a/docs/chapter_tree/binary_search_tree.md +++ b/docs/chapter_tree/binary_search_tree.md @@ -103,7 +103,7 @@ comments: true ```go title="binary_search_tree.go" /* 查找结点 */ - func (bst *BinarySearchTree) Search(num int) *TreeNode { + func (bst *binarySearchTree) search(num int) *TreeNode { node := bst.root // 循环查找,越过叶结点后跳出 for node != nil { @@ -299,7 +299,7 @@ comments: true ```go title="binary_search_tree.go" /* 插入结点 */ - func (bst *BinarySearchTree) Insert(num int) *TreeNode { + func (bst *binarySearchTree) insert(num int) *TreeNode { cur := bst.root // 若树为空,直接提前返回 if cur == nil { @@ -609,7 +609,7 @@ comments: true ```go title="binary_search_tree.go" /* 删除结点 */ - func (bst *BinarySearchTree) Remove(num int) *TreeNode { + func (bst *binarySearchTree) remove(num int) *TreeNode { cur := bst.root // 若树为空,直接提前返回 if cur == nil { @@ -653,10 +653,10 @@ comments: true // 子结点数为 2 } else { // 获取中序遍历中待删除结点 cur 的下一个结点 - next := bst.GetInOrderNext(cur) + next := bst.getInOrderNext(cur) temp := next.Val // 递归删除结点 next - bst.Remove(next.Val) + bst.remove(next.Val) // 将 next 的值复制给 cur cur.Val = temp }