-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0165-compare-version-numbers.go
More file actions
64 lines (50 loc) · 1.22 KB
/
0165-compare-version-numbers.go
File metadata and controls
64 lines (50 loc) · 1.22 KB
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
54
55
56
57
58
59
60
61
62
63
64
package strings
// https://leetcode.com/problems/compare-version-numbers/
// func compareVersion(version1 string, version2 string) int {
// revs1 := strings.Split(version1, ".")
// revs2 := strings.Split(version2, ".")
// n1, n2 := len(revs1), len(revs2)
// var rev1, rev2 int
// for i1, i2 := 0, 0; i1 < n1 || i2 < n2; i1, i2 = i1+1, i2+1 {
// rev1, rev2 = 0, 0
// if i1 < n1 {
// rev1, _ = strconv.Atoi(revs1[i1])
// }
// if i2 < n2 {
// rev2, _ = strconv.Atoi(revs2[i2])
// }
// if rev1 < rev2 {
// return -1
// } else if rev1 > rev2 {
// return 1
// }
// }
// return 0
// }
// Approach: Two-Pointers
// time: O(n1 + n2)
// space: O(1)
func compareVersion(version1 string, version2 string) int {
idx1, idx2 := 0, 0
n1, n2 := len(version1), len(version2)
var revision1, revision2 int
for idx1 < n1 || idx2 < n2 {
revision1, revision2 = 0, 0
for idx1 < n1 && version1[idx1] != '.' {
revision1 = revision1*10 + int(version1[idx1]-'0')
idx1++
}
for idx2 < n2 && version2[idx2] != '.' {
revision2 = revision2*10 + int(version2[idx2]-'0')
idx2++
}
if revision1 < revision2 {
return -1
} else if revision1 > revision2 {
return 1
}
idx1++
idx2++
}
return 0
}