-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path165.go
46 lines (43 loc) · 804 Bytes
/
165.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
package main
import (
"fmt"
"strconv"
"strings"
)
func compareVersion(version1 string, version2 string) int {
strs1 := strings.Split(version1, ".")
strs2 := strings.Split(version2, ".")
// 补全0
if len(strs1) < len(strs2) {
num := len(strs2) - len(strs1)
for i := 0; i < num; i++ {
strs1 = append(strs1, "0")
}
} else {
if len(strs1) > len(strs2) {
num := len(strs1) - len(strs2)
for i := 0; i < num; i++ {
fmt.Print(i)
fmt.Print(strs2)
strs2 = append(strs2, "0")
}
}
}
for i := 0; i < len(strs1); i++ {
num1, _ := strconv.Atoi(strs1[i])
num2, _ := strconv.Atoi(strs2[i])
if num1 == num2 {
continue
}
if num1 < num2 {
return -1
}
if num1 > num2 {
return 1
}
}
return 0
}
func main() {
fmt.Print(compareVersion("1.0.1", "1"))
}