LeetCode/go/largest-number.go
Matej Focko 40da3403da
go: add «179. Largest Number»
Signed-off-by: Matej Focko <me@mfocko.xyz>
2024-09-18 19:20:24 +02:00

29 lines
415 B
Go

package main
import (
"cmp"
"slices"
"strconv"
)
func largestNumber(nums []int) string {
numbers := make([]string, len(nums))
for i, num := range nums {
numbers[i] = strconv.Itoa(num)
}
slices.SortFunc(numbers, func(a, b string) int {
return cmp.Compare(b+a, a+b)
})
if numbers[0] == "0" {
return "0"
}
largest := ""
for _, number := range numbers {
largest += number
}
return largest
}