-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompareVersionNumbers.cpp
58 lines (52 loc) · 1.33 KB
/
CompareVersionNumbers.cpp
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
class Solution {
public:
int compareVersion(string version1, string version2) {
vector<int> Version1, Version2;
int Num = 0;
for(int i=0; i<version1.length(); i++){
if(version1[i] == '.'){
Version1.push_back(Num);
Num = 0;
}else{
Num = Num*10 + (version1[i] - '0');
}
}
Version1.push_back(Num);
Num = 0;
for(int i=0; i<version2.length(); i++){
if(version2[i] == '.'){
Version2.push_back(Num);
Num = 0;
}else{
Num = Num*10 + (version2[i] - '0');
}
}
Version2.push_back(Num);
int N = Version1.size() - 1;
int M = Version2.size() - 1;
while(N >= 0 && Version1[N] == 0){
N--;
}
while(M >= 0 && Version2[M] == 0){
M--;
}
for(int i = 0; i <= min(N, M); i++){
if(Version1[i] == Version2[i]){
continue;
}
if(Version1[i] > Version2[i]){
return 1;
}
if(Version1[i] < Version2[i]){
return -1;
}
}
if(N == M){
return 0;
}
if(N < M){
return -1;
}
return 1;
}
};