在 Go 中刷 LeetCode,以下是一些常用的标准库函数和数据类型的最大值、最小值:
math
包
math.Max(x, y)
:返回两个 float64 类型数中的较大值。math.Min(x, y)
:返回两个 float64 类型数中的较小值。math.Abs(x)
:取绝对值。math.Pow(x, y)
:计算 x^y
。math.Sqrt(x)
:计算平方根。sort
包
sort.Ints(slice []int)
:对整数切片升序排序。sort.Strings(slice []string)
:对字符串切片升序排序。sort.Float64s(slice []float64)
:对浮点数切片升序排序。sort.Slice(slice, func(i, j int) bool)
:自定义排序。strconv
包
strconv.Atoi(s string) (int, error)
:字符串转整数。strconv.Itoa(i int) string
:整数转字符串。strconv.ParseInt(s string, base int, bitSize int)
:将字符串解析为整数,base
指进制,bitSize
指数据类型位数。strings
包
strings.Split(s, sep)
:按指定分隔符拆分字符串。strings.Join(slice, sep)
:将字符串切片按指定分隔符连接。strings.Contains(s, substr)
:判断字符串是否包含子串。strings.Replace(s, old, new, n)
:替换字符串。strings.TrimSpace(s)
:去掉字符串前后的空格。bytes
包
bytes.Buffer
:高效拼接字符串。container/heap
包
heap.Init(h)
:初始化堆。heap.Push(h, x)
:向堆中插入元素。heap.Pop(h)
:弹出堆顶元素。math.MaxInt8
: 127math.MinInt8
: -128math.MaxInt16
: 32,767math.MinInt16
: -32,768math.MaxInt32
: 2,147,483,647math.MinInt32
: -2,147,483,648math.MaxInt64
: 9,223,372,036,854,775,807math.MinInt64
: -9,223,372,036,854,775,808math.MaxFloat32
: ≈ 3.4028235e+38math.MaxFloat64
: ≈ 1.7976931348623157e+308