-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompare Version Numbers
38 lines (28 loc) Β· 972 Bytes
/
Compare Version Numbers
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
class Solution {
public:
int compareVersion(string v1, string v2) {
int ans = 0;
int n1 = v1.size(), n2 = v2.size();
for(int i=0,j=0; i<n1 || j<n2; ++i, ++j) {
string s1 = "", s2 = "";
while(i<n1 && v1[i] != '.') {
if(s1.size() == 0 && v1[i] == '0') {
++i; continue;
}
s1 += v1[i++];
}
while(j<n2 && v2[j] != '.') {
if(s2.size() == 0 && v2[j] == '0') {
++j; continue;
}
s2 += v2[j++];
}
if(s1.size() < s2.size()) return -1;
else if(s2.size() < s1.size()) return 1;
ans = s1.compare(s2);
if(ans < 0) return -1;
else if(ans > 0) return 1;
}
return 0;
}
};