-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0165.CompareVersionNumbers.cpp
More file actions
39 lines (36 loc) · 1.16 KB
/
0165.CompareVersionNumbers.cpp
File metadata and controls
39 lines (36 loc) · 1.16 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
class Solution {
public:
std::vector<int> getVersionValues(const string& versionString) {
// Get version list.
std::vector<int> result;
int currentValue = 0;
for (int i = 0; i < versionString.size(); i++) {
if (versionString[i] == '.') {
result.emplace_back(currentValue);
currentValue = 0;
} else {
currentValue *= 10;
currentValue += versionString[i] - '0';
}
}
if (currentValue) result.emplace_back(currentValue);
return result;
}
int compareVersion(string version1, string version2) {
// Get version lists.
vector<int>
v1 = getVersionValues(version1),
v2 = getVersionValues(version2);
for (int i = 0; i < v1.size() || i < v2.size(); i++) {
if (v1.size() <= i) {
if (v2[i] > 0) return -1;
} else if (v2.size() <= i) {
if (v1[i] > 0) return 1;
} else {
if (v1[i] < v2[i]) return -1;
else if (v1[i] > v2[i]) return 1;
}
}
return 0;
}
};