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:
parent
12f5d72bf2
commit
40da3403da
1 changed files with 29 additions and 0 deletions
29
go/largest-number.go
Normal file
29
go/largest-number.go
Normal 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
|
||||
}
|
Loading…
Reference in a new issue