mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 12:58:42 +08:00
feat: Revised the book (#978)
* Sync recent changes to the revised Word. * Revised the preface chapter * Revised the introduction chapter * Revised the computation complexity chapter * Revised the chapter data structure * Revised the chapter array and linked list * Revised the chapter stack and queue * Revised the chapter hashing * Revised the chapter tree * Revised the chapter heap * Revised the chapter graph * Revised the chapter searching * Reivised the sorting chapter * Revised the divide and conquer chapter * Revised the chapter backtacking * Revised the DP chapter * Revised the greedy chapter * Revised the appendix chapter * Revised the preface chapter doubly * Revised the figures
This commit is contained in:
@ -35,11 +35,11 @@ func insert(nums []int, num int, index int) {
|
||||
for i := len(nums) - 1; i > index; i-- {
|
||||
nums[i] = nums[i-1]
|
||||
}
|
||||
// 将 num 赋给 index 处元素
|
||||
// 将 num 赋给 index 处的元素
|
||||
nums[index] = num
|
||||
}
|
||||
|
||||
/* 删除索引 index 处元素 */
|
||||
/* 删除索引 index 处的元素 */
|
||||
func remove(nums []int, index int) {
|
||||
// 把索引 index 之后的所有元素向前移动一位
|
||||
for i := index; i < len(nums)-1; i++ {
|
||||
|
||||
@ -20,7 +20,7 @@ func TestLinkedList(t *testing.T) {
|
||||
n3 := NewListNode(5)
|
||||
n4 := NewListNode(4)
|
||||
|
||||
// 构建引用指向
|
||||
// 构建节点之间的引用
|
||||
n0.Next = n1
|
||||
n1.Next = n2
|
||||
n2.Next = n3
|
||||
|
||||
@ -28,7 +28,7 @@ func TestList(t *testing.T) {
|
||||
nums = nil
|
||||
fmt.Println("清空列表后 nums =", nums)
|
||||
|
||||
/* 尾部添加元素 */
|
||||
/* 在尾部添加元素 */
|
||||
nums = append(nums, 1)
|
||||
nums = append(nums, 3)
|
||||
nums = append(nums, 2)
|
||||
@ -36,7 +36,7 @@ func TestList(t *testing.T) {
|
||||
nums = append(nums, 4)
|
||||
fmt.Println("添加元素后 nums =", nums)
|
||||
|
||||
/* 中间插入元素 */
|
||||
/* 在中间插入元素 */
|
||||
nums = append(nums[:3], append([]int{6}, nums[3:]...)...) // 在索引 3 处插入数字 6
|
||||
fmt.Println("在索引 3 处插入数字 6 ,得到 nums =", nums)
|
||||
|
||||
|
||||
@ -4,7 +4,7 @@
|
||||
|
||||
package chapter_array_and_linkedlist
|
||||
|
||||
/* 列表类简易实现 */
|
||||
/* 列表类 */
|
||||
type myList struct {
|
||||
arrCapacity int
|
||||
arr []int
|
||||
@ -17,12 +17,12 @@ func newMyList() *myList {
|
||||
return &myList{
|
||||
arrCapacity: 10, // 列表容量
|
||||
arr: make([]int, 10), // 数组(存储列表元素)
|
||||
arrSize: 0, // 列表长度(即当前元素数量)
|
||||
arrSize: 0, // 列表长度(当前元素数量)
|
||||
extendRatio: 2, // 每次列表扩容的倍数
|
||||
}
|
||||
}
|
||||
|
||||
/* 获取列表长度(即当前元素数量) */
|
||||
/* 获取列表长度(当前元素数量) */
|
||||
func (l *myList) size() int {
|
||||
return l.arrSize
|
||||
}
|
||||
@ -49,7 +49,7 @@ func (l *myList) set(num, index int) {
|
||||
l.arr[index] = num
|
||||
}
|
||||
|
||||
/* 尾部添加元素 */
|
||||
/* 在尾部添加元素 */
|
||||
func (l *myList) add(num int) {
|
||||
// 元素数量超出容量时,触发扩容机制
|
||||
if l.arrSize == l.arrCapacity {
|
||||
@ -60,7 +60,7 @@ func (l *myList) add(num int) {
|
||||
l.arrSize++
|
||||
}
|
||||
|
||||
/* 中间插入元素 */
|
||||
/* 在中间插入元素 */
|
||||
func (l *myList) insert(num, index int) {
|
||||
if index < 0 || index >= l.arrSize {
|
||||
panic("索引越界")
|
||||
|
||||
@ -13,7 +13,7 @@ import (
|
||||
func TestMyList(t *testing.T) {
|
||||
/* 初始化列表 */
|
||||
nums := newMyList()
|
||||
/* 尾部添加元素 */
|
||||
/* 在尾部添加元素 */
|
||||
nums.add(1)
|
||||
nums.add(3)
|
||||
nums.add(2)
|
||||
@ -21,7 +21,7 @@ func TestMyList(t *testing.T) {
|
||||
nums.add(4)
|
||||
fmt.Printf("列表 nums = %v ,容量 = %v ,长度 = %v\n", nums.toArray(), nums.capacity(), nums.size())
|
||||
|
||||
/* 中间插入元素 */
|
||||
/* 在中间插入元素 */
|
||||
nums.insert(6, 3)
|
||||
fmt.Printf("在索引 3 处插入数字 6 ,得到 nums = %v\n", nums.toArray())
|
||||
|
||||
|
||||
Reference in New Issue
Block a user