mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 00:25:22 +08:00
规范格式
This commit is contained in:
51
leetcode/0179.Largest-Number/179. Largest Number.go
Normal file
51
leetcode/0179.Largest-Number/179. Largest Number.go
Normal file
@ -0,0 +1,51 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func largestNumber(nums []int) string {
|
||||
if len(nums) == 0 {
|
||||
return ""
|
||||
}
|
||||
numStrs := toStringArray(nums)
|
||||
quickSortString(numStrs, 0, len(numStrs)-1)
|
||||
res := ""
|
||||
for _, str := range numStrs {
|
||||
if res == "0" && str == "0" {
|
||||
continue
|
||||
}
|
||||
res = res + str
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func toStringArray(nums []int) []string {
|
||||
strs := make([]string, 0)
|
||||
for _, num := range nums {
|
||||
strs = append(strs, strconv.Itoa(num))
|
||||
}
|
||||
return strs
|
||||
}
|
||||
func partitionString(a []string, lo, hi int) int {
|
||||
pivot := a[hi]
|
||||
i := lo - 1
|
||||
for j := lo; j < hi; j++ {
|
||||
ajStr := a[j] + pivot
|
||||
pivotStr := pivot + a[j]
|
||||
if ajStr > pivotStr { // 这里的判断条件是关键
|
||||
i++
|
||||
a[j], a[i] = a[i], a[j]
|
||||
}
|
||||
}
|
||||
a[i+1], a[hi] = a[hi], a[i+1]
|
||||
return i + 1
|
||||
}
|
||||
func quickSortString(a []string, lo, hi int) {
|
||||
if lo >= hi {
|
||||
return
|
||||
}
|
||||
p := partitionString(a, lo, hi)
|
||||
quickSortString(a, lo, p-1)
|
||||
quickSortString(a, p+1, hi)
|
||||
}
|
81
leetcode/0179.Largest-Number/179. Largest Number_test.go
Normal file
81
leetcode/0179.Largest-Number/179. Largest Number_test.go
Normal file
@ -0,0 +1,81 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question179 struct {
|
||||
para179
|
||||
ans179
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para179 struct {
|
||||
one []int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans179 struct {
|
||||
one string
|
||||
}
|
||||
|
||||
func Test_Problem179(t *testing.T) {
|
||||
|
||||
qs := []question179{
|
||||
|
||||
question179{
|
||||
para179{[]int{3, 6, 9, 1}},
|
||||
ans179{"9631"},
|
||||
},
|
||||
question179{
|
||||
para179{[]int{1}},
|
||||
ans179{"1"},
|
||||
},
|
||||
|
||||
question179{
|
||||
para179{[]int{}},
|
||||
ans179{""},
|
||||
},
|
||||
|
||||
question179{
|
||||
para179{[]int{2, 10}},
|
||||
ans179{"210"},
|
||||
},
|
||||
|
||||
question179{
|
||||
para179{[]int{3, 30, 34, 5, 9}},
|
||||
ans179{"9534330"},
|
||||
},
|
||||
|
||||
question179{
|
||||
para179{[]int{12, 128}},
|
||||
ans179{"12812"},
|
||||
},
|
||||
|
||||
question179{
|
||||
para179{[]int{12, 121}},
|
||||
ans179{"12121"},
|
||||
},
|
||||
|
||||
question179{
|
||||
para179{[]int{0, 0}},
|
||||
ans179{"0"},
|
||||
},
|
||||
|
||||
question179{
|
||||
para179{[]int{1440, 7548, 4240, 6616, 733, 4712, 883, 8, 9576}},
|
||||
ans179{"9576888375487336616471242401440"},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 179------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans179, q.para179
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, largestNumber(p.one))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
59
leetcode/0179.Largest-Number/README.md
Normal file
59
leetcode/0179.Largest-Number/README.md
Normal file
@ -0,0 +1,59 @@
|
||||
# [179. Largest Number](https://leetcode.com/problems/largest-number/)
|
||||
|
||||
## 题目
|
||||
|
||||
Given a list of non negative integers, arrange them such that they form the largest number.
|
||||
|
||||
|
||||
|
||||
Example 1:
|
||||
|
||||
```c
|
||||
Input: [10,2]
|
||||
Output: "210"
|
||||
```
|
||||
|
||||
|
||||
Example 2:
|
||||
|
||||
```c
|
||||
Input: [3,30,34,5,9]
|
||||
Output: "9534330"
|
||||
```
|
||||
|
||||
Note:
|
||||
|
||||
The result may be very large, so you need to return a string instead of an integer.
|
||||
|
||||
|
||||
|
||||
## 题目大意
|
||||
|
||||
给出一个数组,要求排列这些数组里的元素,使得最终排列出来的数字是最大的。
|
||||
|
||||
|
||||
## 解题思路
|
||||
|
||||
这一题很容易想到把数字都转化为字符串,利用字符串比较,来排序,这样 9 开头的一定排在最前面。不过这样做有一个地方是错误的,比如:"3" 和 "30" 比较,"30" 比 "3" 的字符序要大,这样排序以后就出错了。实际上就这道题而言, "3" 应该排在 "30" 前面。
|
||||
|
||||
在比较 2 个字符串大小的时候,不单纯的只用字符串顺序进行比较,还加入一个顺序。
|
||||
|
||||
```c
|
||||
aStr := a + b
|
||||
bStr := b + a
|
||||
```
|
||||
|
||||
通过比较 aStr 和 bStr 的大小来得出是 a 大还是 b 大。
|
||||
|
||||
举个例子,还是 "3" 和 "30" 的例子,比较这 2 个字符串的大小。
|
||||
|
||||
|
||||
```c
|
||||
aStr := "3" + "30" = "330"
|
||||
bStr := "30" + "3" = "303"
|
||||
```
|
||||
|
||||
通过互相补齐位数之后再进行比较,就没有问题了。很显然这里 "3" 比 "30" 要大。
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user