forked from wufenggirl/LeetCode-in-Golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
largest-number.go
executable file
·68 lines (53 loc) · 1.08 KB
/
largest-number.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package problem0179
import (
"sort"
"strconv"
)
func largestNumber(nums []int) string {
size := len(nums)
b := make(bytes, size)
resSize := 0
// 转换 nums
for i := range b {
b[i] = []byte(strconv.Itoa(nums[i]))
resSize += len(b[i])
}
// 按照题意的规则,对 b 进行排序
sort.Sort(b)
// 串联 b 成 res
res := make([]byte, 0, resSize)
for i := size - 1; i >= 0; i-- {
res = append(res, b[i]...)
}
// 处理 res 以 0 开头的情况
// 比如,[0, 0, 0] 的结果是 "000"
i := 0
for i < resSize-1 && res[i] == '0' {
i++
}
return string(res[i:])
}
type bytes [][]byte
func (b bytes) Len() int {
return len(b)
}
func (b bytes) Less(i, j int) bool {
size := len(b[i]) + len(b[j])
bij := make([]byte, 0, size)
bij = append(bij, b[i]...)
bij = append(bij, b[j]...)
bji := make([]byte, 0, size)
bji = append(bji, b[j]...)
bji = append(bji, b[i]...)
for k := 0; k < size; k++ {
if bij[k] < bji[k] {
return true
} else if bij[k] > bji[k] {
return false
}
}
return false
}
func (b bytes) Swap(i, j int) {
b[i], b[j] = b[j], b[i]
}