Skip to content

Commit

Permalink
style(go): fix go code style
Browse files Browse the repository at this point in the history
    Make the classes and methods in the package private, in case misuse
  • Loading branch information
Reanon committed Jan 8, 2023
1 parent e8f7d8f commit b73ac7b
Show file tree
Hide file tree
Showing 18 changed files with 255 additions and 254 deletions.
24 changes: 12 additions & 12 deletions codes/go/chapter_array_and_linkedlist/my_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
package chapter_array_and_linkedlist

/* 列表类简易实现 */
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 @@ -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("索引越界")
Expand All @@ -42,15 +42,15 @@ 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("索引越界")
}
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 @@ -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("索引越界")
}
Expand All @@ -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("索引越界")
}
Expand All @@ -95,15 +95,15 @@ 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))...)
// 更新列表容量
l.numsCapacity = len(l.nums)
}

/* 返回有效长度的列表 */
func (l *MyList) toArray() []int {
func (l *myList) toArray() []int {
// 仅转换有效长度范围内的列表元素
return l.nums[:l.numsSize]
}
32 changes: 16 additions & 16 deletions codes/go/chapter_computational_complexity/space_complexity.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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))
}
Expand Down Expand Up @@ -112,7 +112,7 @@ func spaceQuadraticRecur(n int) int {
}

/* 指数阶(建立满二叉树) */
func buildTree(n int) *TreeNode {
func buildTree(n int) *treeNode {
if n == 0 {
return nil
}
Expand Down
32 changes: 16 additions & 16 deletions codes/go/chapter_hashing/array_hash_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion codes/go/chapter_searching/hashing_search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
12 changes: 6 additions & 6 deletions codes/go/chapter_sorting/merge_sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++
Expand Down
20 changes: 10 additions & 10 deletions codes/go/chapter_sorting/quick_sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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]) {
Expand All @@ -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)
// 将中位数交换至数组最左端
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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 {
// 哨兵划分操作
Expand Down
6 changes: 3 additions & 3 deletions codes/go/chapter_sorting/quick_sort_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@ 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)
}

// 快速排序(中位基准数优化)
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)
}

// 快速排序(尾递归优化)
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)
Expand Down
Loading

0 comments on commit b73ac7b

Please sign in to comment.