Skip to content

Commit 4e5ef50

Browse files
committed
largest_number
1 parent 440e80e commit 4e5ef50

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ Golang solution for leetcode. For each problem, there is a simple *_test.go to t
151151
#### [172. Factorial Trailing Zeroes](https://github.com/hitzzc/go-leetcode/tree/master/factorial_trailing_zeroes)
152152
#### [173. Binary Search Tree Iterator](https://github.com/hitzzc/go-leetcode/tree/master/binary_search_tree_iterator)
153153
#### [174. Dungeon Game](https://github.com/hitzzc/go-leetcode/tree/master/dungeon_game)
154+
#### [179. Largest Number](https://github.com/hitzzc/go-leetcode/tree/master/largest_number)
154155

155156

156157

largest_number/largest_number.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package largest_number
2+
3+
import (
4+
"strconv"
5+
)
6+
7+
func largestNumber(nums []int) string {
8+
stringList := make([]string, len(nums))
9+
for i := range nums {
10+
stringList[i] = strconv.Itoa(nums[i])
11+
}
12+
quickSort(stringList, 0, len(stringList)-1)
13+
var ret string
14+
for i := range stringList {
15+
ret += stringList[i]
16+
}
17+
if len(ret) != 0 && ret[0] == '0' {
18+
return "0"
19+
}
20+
return ret
21+
}
22+
23+
func quickSort(stringList []string, i, j int) {
24+
if i >= j {
25+
return
26+
}
27+
mid := partition(stringList, i, j)
28+
quickSort(stringList, i, mid-1)
29+
quickSort(stringList, mid+1, j)
30+
return
31+
}
32+
33+
func partition(stringList []string, i, j int) int {
34+
k := i
35+
for ; i < j; i++ {
36+
if less(stringList[i], stringList[j]) {
37+
continue
38+
}
39+
stringList[k], stringList[i] = stringList[i], stringList[k]
40+
k++
41+
}
42+
stringList[k], stringList[j] = stringList[j], stringList[k]
43+
return k
44+
}
45+
46+
func less(s1, s2 string) bool {
47+
return s1+s2 <= s2+s1
48+
}

0 commit comments

Comments
 (0)