Skip to content

Latest commit

 

History

History
24 lines (19 loc) · 522 Bytes

45. 把数组排成最小的数.md

File metadata and controls

24 lines (19 loc) · 522 Bytes

解题思路

自定义排序函数

将数字切片转换成字符串切片,自定义排序函数,当两个数拼接起来,谁小谁在前面

func minNumber(nums []int) string {
    str := []string{}
    for _, v := range nums {
        str = append(str, strconv.Itoa(v))
    }
    sort.Slice(str, func(i, j int) bool {
        str1 := str[i] + str[j]
        str2 := str[j] + str[i]
        return str1 < str2
    })
    return strings.Join(str, "")
}