1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-11-10 00:09:06 +01:00

go: add «179. Largest Number»

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-09-18 19:20:24 +02:00
parent 12f5d72bf2
commit 40da3403da
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

29
go/largest-number.go Normal file
View file

@ -0,0 +1,29 @@
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
}