-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathsolution.cpp
More file actions
29 lines (26 loc) · 893 Bytes
/
solution.cpp
File metadata and controls
29 lines (26 loc) · 893 Bytes
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
class Solution {
public:
int compareVersion(string version1, string version2) {
int i = 0, j = 0;
int n = version1.size(), m = version2.size();
while (i < n || j < m) {
long num1 = 0, num2 = 0;
// parse next number from version1
while (i < n && version1[i] != '.') {
num1 = num1 * 10 + (version1[i] - '0');
++i;
}
// skip dot if present
if (i < n && version1[i] == '.') ++i;
// parse next number from version2
while (j < m && version2[j] != '.') {
num2 = num2 * 10 + (version2[j] - '0');
++j;
}
if (j < m && version2[j] == '.') ++j;
if (num1 < num2) return -1;
if (num1 > num2) return 1;
}
return 0;
}
};