-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsolution.cpp
29 lines (29 loc) · 875 Bytes
/
solution.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
/**
* 81 / 81 test cases passed.
* Runtime: 0 ms
* Memory Usage: 6.2 MB
*/
class Solution {
public:
int compareVersion(string version1, string version2) {
auto convert = [](string& version) {
istringstream iss(version);
vector<int> sub_verions;
string v;
while (getline(iss, v, '.')) {
sub_verions.push_back(stoi(v));
}
return sub_verions;
};
auto verNum1 = convert(version1);
auto verNum2 = convert(version2);
int n = max(verNum1.size(), verNum2.size());
for (int i = 0; i < n; ++ i) {
int v1 = (i >= verNum1.size()) ? 0 : verNum1[i];
int v2 = (i >= verNum2.size()) ? 0 : verNum2[i];
if (v1 > v2) return 1;
else if (v1 < v2) return -1;
}
return 0;
}
};