mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 12:58:42 +08:00
Fix bugs and harmonize the code comments (#1199)
* Fix the comment in array_deque.go * Fix the comment in bucket_sort.c * Translate the Java code comments to Chinese * Bug fixes * 二分查找 -> 二分搜尋 * Harmonize comments in `utils` between multiple programming languages
This commit is contained in:
@ -64,7 +64,7 @@ func (q *arrayDeque) pushLast(num int) {
|
||||
}
|
||||
// 计算队尾指针,指向队尾索引 + 1
|
||||
rear := q.index(q.front + q.queSize)
|
||||
// 将 num 添加至队首
|
||||
// 将 num 添加至队尾
|
||||
q.nums[rear] = num
|
||||
q.queSize++
|
||||
}
|
||||
|
||||
@ -4,13 +4,13 @@
|
||||
|
||||
package pkg
|
||||
|
||||
// ListNode Definition for a singly-linked list node
|
||||
// ListNode 链表节点
|
||||
type ListNode struct {
|
||||
Next *ListNode
|
||||
Val int
|
||||
}
|
||||
|
||||
// NewListNode Generate a list node with an val
|
||||
// NewListNode 链表节点构造函数
|
||||
func NewListNode(v int) *ListNode {
|
||||
return &ListNode{
|
||||
Next: nil,
|
||||
@ -18,7 +18,7 @@ func NewListNode(v int) *ListNode {
|
||||
}
|
||||
}
|
||||
|
||||
// ArrayToLinkedList Generate a linked list with an array
|
||||
// ArrayToLinkedList 将数组反序列化为链表
|
||||
func ArrayToLinkedList(arr []int) *ListNode {
|
||||
// dummy header of linked list
|
||||
dummy := NewListNode(0)
|
||||
@ -29,11 +29,3 @@ func ArrayToLinkedList(arr []int) *ListNode {
|
||||
}
|
||||
return dummy.Next
|
||||
}
|
||||
|
||||
// GetListNode Get a list node with specific value from a linked list
|
||||
func GetListNode(node *ListNode, val int) *ListNode {
|
||||
for node != nil && node.Val != val {
|
||||
node = node.Next
|
||||
}
|
||||
return node
|
||||
}
|
||||
|
||||
@ -5,7 +5,6 @@
|
||||
package pkg
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@ -14,6 +13,4 @@ func TestListNode(t *testing.T) {
|
||||
head := ArrayToLinkedList(arr)
|
||||
|
||||
PrintLinkedList(head)
|
||||
node := GetListNode(head, 5)
|
||||
fmt.Println("Find node: ", node.Val)
|
||||
}
|
||||
|
||||
@ -11,13 +11,13 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// PrintSlice Print a slice
|
||||
// PrintSlice 打印切片
|
||||
func PrintSlice[T any](nums []T) {
|
||||
fmt.Printf("%v", nums)
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// PrintList Print a list
|
||||
// PrintList 打印列表
|
||||
func PrintList(list *list.List) {
|
||||
if list.Len() == 0 {
|
||||
fmt.Print("[]\n")
|
||||
@ -33,14 +33,14 @@ func PrintList(list *list.List) {
|
||||
fmt.Print(e.Value, "]\n")
|
||||
}
|
||||
|
||||
// PrintMap Print a hash map
|
||||
// PrintMap 打印哈希表
|
||||
func PrintMap[K comparable, V any](m map[K]V) {
|
||||
for key, value := range m {
|
||||
fmt.Println(key, "->", value)
|
||||
}
|
||||
}
|
||||
|
||||
// PrintHeap Print a heap
|
||||
// PrintHeap 打印堆
|
||||
func PrintHeap(h []any) {
|
||||
fmt.Printf("堆的数组表示:")
|
||||
fmt.Printf("%v", h)
|
||||
@ -49,7 +49,7 @@ func PrintHeap(h []any) {
|
||||
PrintTree(root)
|
||||
}
|
||||
|
||||
// PrintLinkedList Print a linked list
|
||||
// PrintLinkedList 打印链表
|
||||
func PrintLinkedList(node *ListNode) {
|
||||
if node == nil {
|
||||
return
|
||||
@ -63,12 +63,12 @@ func PrintLinkedList(node *ListNode) {
|
||||
fmt.Println(builder.String())
|
||||
}
|
||||
|
||||
// PrintTree Print a binary tree
|
||||
// PrintTree 打印二叉树
|
||||
func PrintTree(root *TreeNode) {
|
||||
printTreeHelper(root, nil, false)
|
||||
}
|
||||
|
||||
// printTreeHelper Help to print a binary tree, hide more details
|
||||
// printTreeHelper 打印二叉树
|
||||
// This tree printer is borrowed from TECHIE DELIGHT
|
||||
// https://www.techiedelight.com/c-program-print-binary-tree/
|
||||
func printTreeHelper(root *TreeNode, prev *trunk, isRight bool) {
|
||||
@ -96,7 +96,6 @@ func printTreeHelper(root *TreeNode, prev *trunk, isRight bool) {
|
||||
printTreeHelper(root.Left, trunk, false)
|
||||
}
|
||||
|
||||
// trunk Help to print tree structure
|
||||
type trunk struct {
|
||||
prev *trunk
|
||||
str string
|
||||
|
||||
@ -4,6 +4,7 @@
|
||||
|
||||
package pkg
|
||||
|
||||
// TreeNode 二叉树节点
|
||||
type TreeNode struct {
|
||||
Val any // 节点值
|
||||
Height int // 节点高度
|
||||
@ -11,6 +12,7 @@ type TreeNode struct {
|
||||
Right *TreeNode // 右子节点引用
|
||||
}
|
||||
|
||||
// NewTreeNode 二叉树节点构造函数
|
||||
func NewTreeNode(v any) *TreeNode {
|
||||
return &TreeNode{
|
||||
Val: v,
|
||||
|
||||
@ -9,14 +9,14 @@ type Vertex struct {
|
||||
Val int
|
||||
}
|
||||
|
||||
// NewVertex 构造函数
|
||||
// NewVertex 顶点构造函数
|
||||
func NewVertex(val int) Vertex {
|
||||
return Vertex{
|
||||
Val: val,
|
||||
}
|
||||
}
|
||||
|
||||
// ValsToVets Generate a vertex list tree given an array
|
||||
// ValsToVets 将值列表反序列化为顶点列表
|
||||
func ValsToVets(vals []int) []Vertex {
|
||||
vets := make([]Vertex, len(vals))
|
||||
for i := 0; i < len(vals); i++ {
|
||||
@ -25,7 +25,7 @@ func ValsToVets(vals []int) []Vertex {
|
||||
return vets
|
||||
}
|
||||
|
||||
// VetsToVals Serialize given vertex list to a value list
|
||||
// VetsToVals 将顶点列表序列化为值列表
|
||||
func VetsToVals(vets []Vertex) []int {
|
||||
vals := make([]int, len(vets))
|
||||
for i := range vets {
|
||||
|
||||
Reference in New Issue
Block a user