mirror of
https://github.com/krahets/hello-algo.git
synced 2025-02-02 22:43:50 +08:00
Fix "函数" and "方法"
This commit is contained in:
parent
674ff2910a
commit
504dff1728
@ -111,7 +111,7 @@ TreeNode *rotate(TreeNode *node) {
|
|||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 递归插入节点(辅助方法) */
|
/* 递归插入节点(辅助函数) */
|
||||||
TreeNode *insertHelper(TreeNode *node, int val) {
|
TreeNode *insertHelper(TreeNode *node, int val) {
|
||||||
if (node == NULL) {
|
if (node == NULL) {
|
||||||
return newTreeNode(val);
|
return newTreeNode(val);
|
||||||
@ -138,7 +138,7 @@ void insert(aVLTree *tree, int val) {
|
|||||||
tree->root = insertHelper(tree->root, val);
|
tree->root = insertHelper(tree->root, val);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 递归删除节点(辅助方法) */
|
/* 递归删除节点(辅助函数) */
|
||||||
TreeNode *removeHelper(TreeNode *node, int val) {
|
TreeNode *removeHelper(TreeNode *node, int val) {
|
||||||
TreeNode *child, *grandChild;
|
TreeNode *child, *grandChild;
|
||||||
if (node == NULL) {
|
if (node == NULL) {
|
||||||
|
@ -21,7 +21,7 @@ func TestArray(t *testing.T) {
|
|||||||
fmt.Println("数组 arr =", arr)
|
fmt.Println("数组 arr =", arr)
|
||||||
// 在 Go 中,指定长度时([5]int)为数组,不指定长度时([]int)为切片
|
// 在 Go 中,指定长度时([5]int)为数组,不指定长度时([]int)为切片
|
||||||
// 由于 Go 的数组被设计为在编译期确定长度,因此只能使用常量来指定长度
|
// 由于 Go 的数组被设计为在编译期确定长度,因此只能使用常量来指定长度
|
||||||
// 为了方便实现扩容 extend() 方法,以下将切片(Slice)看作数组(Array)
|
// 为了方便实现扩容 extend() 函数,以下将切片(Slice)看作数组(Array)
|
||||||
nums := []int{1, 3, 2, 5, 4}
|
nums := []int{1, 3, 2, 5, 4}
|
||||||
fmt.Println("数组 nums =", nums)
|
fmt.Println("数组 nums =", nums)
|
||||||
|
|
||||||
|
@ -12,7 +12,7 @@ type myList struct {
|
|||||||
extendRatio int
|
extendRatio int
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 构造方法 */
|
/* 构造函数 */
|
||||||
func newMyList() *myList {
|
func newMyList() *myList {
|
||||||
return &myList{
|
return &myList{
|
||||||
numsCapacity: 10, // 列表容量
|
numsCapacity: 10, // 列表容量
|
||||||
|
@ -18,7 +18,7 @@ type graphAdjList struct {
|
|||||||
adjList map[Vertex][]Vertex
|
adjList map[Vertex][]Vertex
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 构造方法 */
|
/* 构造函数 */
|
||||||
func newGraphAdjList(edges [][]Vertex) *graphAdjList {
|
func newGraphAdjList(edges [][]Vertex) *graphAdjList {
|
||||||
g := &graphAdjList{
|
g := &graphAdjList{
|
||||||
adjList: make(map[Vertex][]Vertex),
|
adjList: make(map[Vertex][]Vertex),
|
||||||
|
@ -14,7 +14,7 @@ type graphAdjMat struct {
|
|||||||
adjMat [][]int
|
adjMat [][]int
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 构造方法 */
|
/* 构造函数 */
|
||||||
func newGraphAdjMat(vertices []int, edges [][]int) *graphAdjMat {
|
func newGraphAdjMat(vertices []int, edges [][]int) *graphAdjMat {
|
||||||
// 添加顶点
|
// 添加顶点
|
||||||
n := len(vertices)
|
n := len(vertices)
|
||||||
|
@ -8,14 +8,14 @@ package chapter_heap
|
|||||||
// 实现 heap.Interface 需要同时实现 sort.Interface
|
// 实现 heap.Interface 需要同时实现 sort.Interface
|
||||||
type intHeap []any
|
type intHeap []any
|
||||||
|
|
||||||
// Push heap.Interface 的方法,实现推入元素到堆
|
// Push heap.Interface 的函数,实现推入元素到堆
|
||||||
func (h *intHeap) Push(x any) {
|
func (h *intHeap) Push(x any) {
|
||||||
// Push 和 Pop 使用 pointer receiver 作为参数
|
// Push 和 Pop 使用 pointer receiver 作为参数
|
||||||
// 因为它们不仅会对切片的内容进行调整,还会修改切片的长度。
|
// 因为它们不仅会对切片的内容进行调整,还会修改切片的长度。
|
||||||
*h = append(*h, x.(int))
|
*h = append(*h, x.(int))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pop heap.Interface 的方法,实现弹出堆顶元素
|
// Pop heap.Interface 的函数,实现弹出堆顶元素
|
||||||
func (h *intHeap) Pop() any {
|
func (h *intHeap) Pop() any {
|
||||||
// 待出堆元素存放在最后
|
// 待出堆元素存放在最后
|
||||||
last := (*h)[len(*h)-1]
|
last := (*h)[len(*h)-1]
|
||||||
@ -23,18 +23,18 @@ func (h *intHeap) Pop() any {
|
|||||||
return last
|
return last
|
||||||
}
|
}
|
||||||
|
|
||||||
// Len sort.Interface 的方法
|
// Len sort.Interface 的函数
|
||||||
func (h *intHeap) Len() int {
|
func (h *intHeap) Len() int {
|
||||||
return len(*h)
|
return len(*h)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Less sort.Interface 的方法
|
// Less sort.Interface 的函数
|
||||||
func (h *intHeap) Less(i, j int) bool {
|
func (h *intHeap) Less(i, j int) bool {
|
||||||
// 如果实现小顶堆,则需要调整为小于号
|
// 如果实现小顶堆,则需要调整为小于号
|
||||||
return (*h)[i].(int) > (*h)[j].(int)
|
return (*h)[i].(int) > (*h)[j].(int)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Swap sort.Interface 的方法
|
// Swap sort.Interface 的函数
|
||||||
func (h *intHeap) Swap(i, j int) {
|
func (h *intHeap) Swap(i, j int) {
|
||||||
(*h)[i], (*h)[j] = (*h)[j], (*h)[i]
|
(*h)[i], (*h)[j] = (*h)[j], (*h)[i]
|
||||||
}
|
}
|
||||||
|
@ -13,14 +13,14 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func testPush(h *intHeap, val int) {
|
func testPush(h *intHeap, val int) {
|
||||||
// 调用 heap.Interface 的方法,来添加元素
|
// 调用 heap.Interface 的函数,来添加元素
|
||||||
heap.Push(h, val)
|
heap.Push(h, val)
|
||||||
fmt.Printf("\n元素 %d 入堆后 \n", val)
|
fmt.Printf("\n元素 %d 入堆后 \n", val)
|
||||||
PrintHeap(*h)
|
PrintHeap(*h)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testPop(h *intHeap) {
|
func testPop(h *intHeap) {
|
||||||
// 调用 heap.Interface 的方法,来移除元素
|
// 调用 heap.Interface 的函数,来移除元素
|
||||||
val := heap.Pop(h)
|
val := heap.Pop(h)
|
||||||
fmt.Printf("\n堆顶元素 %d 出堆后 \n", val)
|
fmt.Printf("\n堆顶元素 %d 出堆后 \n", val)
|
||||||
PrintHeap(*h)
|
PrintHeap(*h)
|
||||||
|
@ -15,14 +15,14 @@ type maxHeap struct {
|
|||||||
data []any
|
data []any
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 构造方法,建立空堆 */
|
/* 构造函数,建立空堆 */
|
||||||
func newHeap() *maxHeap {
|
func newHeap() *maxHeap {
|
||||||
return &maxHeap{
|
return &maxHeap{
|
||||||
data: make([]any, 0),
|
data: make([]any, 0),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 构造方法,根据切片建堆 */
|
/* 构造函数,根据切片建堆 */
|
||||||
func newMaxHeap(nums []any) *maxHeap {
|
func newMaxHeap(nums []any) *maxHeap {
|
||||||
// 将列表元素原封不动添加进堆
|
// 将列表元素原封不动添加进堆
|
||||||
h := &maxHeap{data: nums}
|
h := &maxHeap{data: nums}
|
||||||
|
@ -111,7 +111,7 @@ func (t *aVLTree) insert(val int) {
|
|||||||
t.root = t.insertHelper(t.root, val)
|
t.root = t.insertHelper(t.root, val)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 递归插入节点(辅助方法) */
|
/* 递归插入节点(辅助函数) */
|
||||||
func (t *aVLTree) insertHelper(node *TreeNode, val int) *TreeNode {
|
func (t *aVLTree) insertHelper(node *TreeNode, val int) *TreeNode {
|
||||||
if node == nil {
|
if node == nil {
|
||||||
return NewTreeNode(val)
|
return NewTreeNode(val)
|
||||||
@ -138,7 +138,7 @@ func (t *aVLTree) remove(val int) {
|
|||||||
t.root = t.removeHelper(t.root, val)
|
t.root = t.removeHelper(t.root, val)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 递归删除节点(辅助方法) */
|
/* 递归删除节点(辅助函数) */
|
||||||
func (t *aVLTree) removeHelper(node *TreeNode, val int) *TreeNode {
|
func (t *aVLTree) removeHelper(node *TreeNode, val int) *TreeNode {
|
||||||
if node == nil {
|
if node == nil {
|
||||||
return nil
|
return nil
|
||||||
|
@ -9,7 +9,7 @@ type Vertex struct {
|
|||||||
Val int
|
Val int
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewVertex 构造方法
|
// NewVertex 构造函数
|
||||||
func NewVertex(val int) Vertex {
|
func NewVertex(val int) Vertex {
|
||||||
return Vertex{
|
return Vertex{
|
||||||
Val: val,
|
Val: val,
|
||||||
|
Loading…
Reference in New Issue
Block a user