Golang 常用函数

数学

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}

func SumInts(values ...int) int {
	if len(values) == 0 {
		return 0
	}
	var result int
	for _, valueElem := range values {
		result += valueElem
	}
	return result
}

func MaxInts(values ...int) int {
	if len(values) == 0 {
		panic(valuesIsEmptyMsg)
	}
	hitVal := values[0]
	for i := 1; i < len(values); i++ {
		if values[i] > hitVal {
			hitVal = values[i]
		}
	}
	return hitVal
}

func MinInts(values ...int) int {
	if len(values) == 0 {
		panic(valuesIsEmptyMsg)
	}
	hitVal := values[0]
	for i := 1; i < len(values); i++ {
		if values[i] < hitVal {
			hitVal = values[i]
		}
	}
	return hitVal
}

切片

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
func Contains[T comparable](s []T, v T) bool {
	for _, vv := range s {
		if vv == v {
			return true
		}
	}
	return false
}

func ContainsAll[T comparable](s []T, v []T) bool {
	set := make(map[T]struct{}, len(s))
	for _, vv := range s {
		set[vv] = emptyStruct
	}
	var exists bool
	for _, vv := range v {
		_, exists = set[vv]
		if !exists {
			return false
		}
	}
	return true
}

func All[T any](s []T, fn func(v T) bool) bool {
	if len(s) == 0 {
		return true
	}
	for _, elem := range s {
		if !fn(elem) {
			return false
		}
	}
	return true
}

func Any[T any](s []T, fn func(v T) bool) bool {
	if len(s) == 0 {
		return false
	}
	for _, elem := range s {
		if fn(elem) {
			return true
		}
	}
	return false
}

func Shuffle[T any](s []T) []T {
	newS := make([]T, len(s), len(s))
	copy(newS, s)
	for i := range newS {
		j := rand.Intn(i + 1)
		newS[i], newS[j] = newS[j], newS[i]
	}
	return newS
}

字符串

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
func reverseString(s string) string {
	runes := []rune(s)
	length := len(runes)
	for i, j := 0, length-1; i < j; i, j = i+1, j-1 {
		runes[i], runes[j] = runes[j], runes[i]
	}
	return string(runes)
}

func reverse(slice []int) {
	for i, j := 0, len(slice)-1; i < j; i, j = i+1, j-1 {
		slice[i], slice[j] = slice[j], slice[i]
	}
}

func Shuffle(s string) string {
	if s == "" {
		return s
	}
	runes := []rune(s)
	index := 0
	for i := len(runes) - 1; i > 0; i-- {
		index = rand.Intn(i + 1)
		if i != index {
			runes[i], runes[index] = runes[index], runes[i]
		}
	}
	return string(runes)
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
type InitHeap []int

func (hp InitHeap) Len() int           { return len(hp) }
func (hp InitHeap) Less(i, j int) bool { return hp[i] < hp[j] }
func (hp InitHeap) Swap(i, j int)      { hp[i], hp[j] = hp[j], hp[i] }
func (hp *InitHeap) Push(x interface{}) {
	*hp = append(*hp, x.(int))
}
func (hp *InitHeap) Pop() interface{} {
	old := *hp
	n := len(old)
	x := old[n-1]
	*hp = old[:n-1]
	return x
}

链表和树

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
type TreeNode struct {
	Val   int
	Left  *TreeNode
	Right *TreeNode
}

type ListNode struct {
	Val  int
	Next *ListNode
}

type Node struct {
	Val      int
	Children []*Node
}
本文作者:
本文链接: https://hgnulb.github.io/blog/2023/10823806
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处!