-
Notifications
You must be signed in to change notification settings - Fork 23
/
179.go
53 lines (41 loc) · 1001 Bytes
/
179.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
package problem
/*
给定一组非负整数,重新排列它们的顺序使之组成一个最大的整数。
示例 1:
输入: [10,2]
输出: 210
示例 2:
输入: [3,30,34,5,9]
输出: 9534330
说明: 输出结果可能非常大,所以你需要返回一个字符串而不是整数。
*/
func largestNumber(nums []int) string {
arrLen := len(nums) //空数组的情况下
if arrLen == 0 {
return "0"
}
sum := 0 //数组参数都是0的情况下
for _, num := range nums {
sum += num
}
if sum == 0 {
return "0"
}
length := len(nums)
for i := 0; i < length; i++ {
for j := 0; j < length-i-1; j++ {
s1 := strconv.Itoa(nums[j]) + strconv.Itoa(nums[j+1])
s2 := strconv.Itoa(nums[j+1]) + strconv.Itoa(nums[j])
s1Int, _ := strconv.Atoi(s1)
s2Int, _ := strconv.Atoi(s2)
if s1Int < s2Int {
nums[j], nums[j+1] = nums[j+1], nums[j]
}
}
}
s := make([]string, length)
for i := range nums {
s[i] = strconv.Itoa(nums[i])
}
return strings.Join(s, "")
}