-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path165_compareversion.cpp
More file actions
34 lines (34 loc) · 846 Bytes
/
165_compareversion.cpp
File metadata and controls
34 lines (34 loc) · 846 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
30
31
32
33
34
class Solution {
public:
int stringToInt(string s){
int num = 0;
for(int i = 0;i<s.size();i++){
num*=10;
num+=s[i]-'0';
}
return num;
}
int compareVersion(string version1, string version2) {
int i = 0,j = 0;
int len1 = version1.size(),len2=version2.size();
int v1,v2;
while(i<len1||j<len2){
string s1="",s2="";
while(i<len1&&version1[i]!='.'){
s1+=version1[i++];
}
while(j<len2&&version2[j]!='.'){
s2+=version2[j++];
}
i++;
j++;
v1 = stringToInt(s1);
v2 = stringToInt(s2);
if(v1>v2)
return 1;
if(v1<v2)
return -1;
}
return 0;
}
};