写leetcode常用的库函数和常量

在 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: 127
  • math.MinInt8: -128
  • math.MaxInt16: 32,767
  • math.MinInt16: -32,768
  • math.MaxInt32: 2,147,483,647
  • math.MinInt32: -2,147,483,648
  • math.MaxInt64: 9,223,372,036,854,775,807
  • math.MinInt64: -9,223,372,036,854,775,808
  • math.MaxFloat32: ≈ 3.4028235e+38
  • math.MaxFloat64: ≈ 1.7976931348623157e+308

你可能感兴趣的:(golang小知识点,算法,数据结构,golang)