Skip to content

Commit

Permalink
doc(code): modify go code in docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Reanon committed Jan 8, 2023
1 parent 33e2c4f commit 0243957
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 117 deletions.
25 changes: 14 additions & 11 deletions docs/chapter_array_and_linkedlist/list.md
Original file line number Diff line number Diff line change
Expand Up @@ -864,16 +864,16 @@ comments: true

```go title="my_list.go"
/* 列表类简易实现 */
type MyList struct {
type myList struct {
numsCapacity int
nums []int
numsSize int
extendRatio int
}

/* 构造函数 */
func newMyList() *MyList {
return &MyList{
func newMyList() *myList {
return &myList{
numsCapacity: 10, // 列表容量
nums: make([]int, 10), // 数组(存储列表元素)
numsSize: 0, // 列表长度(即当前元素数量)
Expand All @@ -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("索引越界")
Expand All @@ -901,15 +901,15 @@ comments: true
}

/* 更新元素 */
func (l *MyList) set(num, index int) {
func (l *myList) set(num, index int) {
if index >= l.numsSize {
panic("索引越界")
}
l.nums[index] = num
}

/* 尾部添加元素 */
func (l *MyList) add(num int) {
func (l *myList) add(num int) {
// 元素数量超出容量时,触发扩容机制
if l.numsSize == l.numsCapacity {
l.extendCapacity()
Expand All @@ -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("索引越界")
}
Expand All @@ -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))...)
// 更新列表容量
Expand Down
16 changes: 8 additions & 8 deletions docs/chapter_computational_complexity/space_complexity.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}

/* 函数 */
Expand Down Expand Up @@ -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))
}
Expand Down Expand Up @@ -1108,7 +1108,7 @@ $$

```go title="space_complexity.go"
/* 指数阶(建立满二叉树) */
func buildTree(n int) *TreeNode {
func buildTree(n int) *treeNode {
if n == 0 {
return nil
}
Expand Down
24 changes: 12 additions & 12 deletions docs/chapter_hashing/hash_map.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
```
Expand Down
19 changes: 9 additions & 10 deletions docs/chapter_sorting/merge_sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 5 additions & 5 deletions docs/chapter_sorting/quick_sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 // 返回基准数的索引
}
```

Expand Down
Loading

0 comments on commit 0243957

Please sign in to comment.