-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompareVersionNumbers.cpp
More file actions
51 lines (48 loc) · 1.17 KB
/
CompareVersionNumbers.cpp
File metadata and controls
51 lines (48 loc) · 1.17 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
#include <iostream>
#include <string.h>
using namespace std;
class Solution {
public:
int compareVersion(string version1, string version2) {
while(version1 != "" && version2 != ""){
int v1 = getVersion(version1);
int v2 = getVersion(version2);
if(v1 > v2)
return 1;
else if(v1 < v2)
return -1;
}
if(version1 != "" && getVersion(version1) != 0)
return 1;
else if(version2 != "" && getVersion(version2) != 0)
return -1;
else
return 0;
}
private:
int getVersion(string &version){
int v = 0;
int i;
int length = version.length();
for (i = 0; i<length; ++i) {
if(version[i] == '.'){
break;
}
}
string num = version.substr(0, i);
if (i==length) {
version = "";
}
else{
version = version.substr(i+1);
}
return atoi(num.c_str());
}
};
int main(int argc, char *argv[])
{
Solution s;
string s1="1.2";
std::cout << s.compareVersion("1", "1.0")<<endl;
return 0;
}