Skip to content

Commit 91db34d

Browse files
committed
compare_version_numbers
1 parent 4832eb2 commit 91db34d

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ Golang solution for leetcode. For each problem, there is a simple *_test.go to t
142142
#### [160. Intersection of Two Linked Lists](https://github.com/hitzzc/go-leetcode/tree/master/intersection_of_two_linked_lists)
143143
#### [162. Find Peak Element](https://github.com/hitzzc/go-leetcode/tree/master/find_peak_element)
144144
#### [164. Maximum Gap](https://github.com/hitzzc/go-leetcode/tree/master/maximum_gap)
145-
145+
#### [165. compare version numbers](https://github.com/hitzzc/go-leetcode/tree/master/compare_version_numbers)
146146

147147

148148

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package compare_version_numbers
2+
3+
func compareVersion(version1 string, version2 string) int {
4+
bytes1, bytes2 := []byte(version1), []byte(version2)
5+
bToI := map[byte]int{
6+
'0': 0,
7+
'1': 1,
8+
'2': 2,
9+
'3': 3,
10+
'4': 4,
11+
'5': 5,
12+
'6': 6,
13+
'7': 7,
14+
'8': 8,
15+
'9': 9,
16+
}
17+
i, j := -1, -1
18+
for i < len(bytes1) || j < len(bytes2) {
19+
var num1, num2 int
20+
for i++; i < len(bytes1) && bytes1[i] != '.'; i++ {
21+
num1 = num1*10 + bToI[bytes1[i]]
22+
}
23+
for j++; j < len(bytes2) && bytes2[j] != '.'; j++ {
24+
num2 = num2*10 + bToI[bytes2[j]]
25+
}
26+
if num1 > num2 {
27+
return 1
28+
} else if num1 < num2 {
29+
return -1
30+
}
31+
}
32+
return 0
33+
}

0 commit comments

Comments
 (0)