-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathcompare-version-numbers.cpp
More file actions
39 lines (35 loc) · 1.35 KB
/
compare-version-numbers.cpp
File metadata and controls
39 lines (35 loc) · 1.35 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
class Solution { //3ms solution. O(1) space, O(n) time complexity
public:
int compareVersion(string version1, string version2) {
int v1_index = 0;
int v2_index = 0;
int res = 0;
while (res == 0 && (v1_index < version1.size() || v2_index < version2.size())) {
//judge if has preceeding 0s
while (v1_index < version1.size() && version1[v1_index] == '0') {
++v1_index;
}
while (v2_index < version2.size() && version2[v2_index] == '0') {
++v2_index;
}
int current_res = 0;
while (v1_index < version1.size() && version1[v1_index] != '.'
&& v2_index < version2.size() && version2[v2_index] != '.') {
if (version1[v1_index] > version2[v2_index]) {
current_res = 1;
} else if (version2[v2_index] > version1[v1_index]){
current_res = -1;
}
++v1_index, ++v2_index;
}
while (v1_index < version1.size() && version1[v1_index++] != '.') {
current_res = 1;
}
while (v2_index < version2.size() && version2[v2_index++] != '.') {
current_res = -1;
}
res = current_res;
}
return res;
}
};