-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompareVersionNumbers.cpp.save
More file actions
59 lines (57 loc) · 1.25 KB
/
Copy pathCompareVersionNumbers.cpp.save
File metadata and controls
59 lines (57 loc) · 1.25 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
#include<iostream>
#include<vector>
#include<string>
#include<sstream>
using namespace std;
class Solution {
public:
int compareVersion(string version1, string version2) {
istringstream is(version1);
istringstream iss(version2);
vector<string> vec1;
vector<string> vec2;
string tmp;
while(getline(is,tmp,'.'))
{
vec1.push_back(tmp);
}
while(getline(iss,tmp,'.'))
{
vec2.push_back(tmp);
}
int len1=vec1.size();
int len2=vec2.size();
int i=0,j=0;
while(i!=len1&&j!=len2)
{
if(atoi(vec1[i]<vec2[i])
return -1;
else if(vec1[i]>vec2[i])
return 1;
i++;
j++;
}
if(i==len1&&j==len2)
return 0;
while(i!=len1)
{
if(vec1[i]!=0)
return 1;
i++;
}
while(j!=len2)
{
if(vec2[j]!=0)
return -1;
j++;
}
return 0;
}
};
int main()
{
string s1="10.23.1";
string s2="01.23";
Solution s;
cout<<s.compareVersion(s1,s2)<<endl;
}