-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompareVersionNumbers.cc
More file actions
39 lines (35 loc) · 1.03 KB
/
CompareVersionNumbers.cc
File metadata and controls
39 lines (35 loc) · 1.03 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
// https://oj.leetcode.com/problems/compare-version-numbers/
class Solution {
public:
vector<int> splitVer(string s) {
int i = 0;
vector<int> res;
int ver = 0;
while (i < s.size()) {
char c = s[i];
if (c == '.') {
res.push_back(ver);
ver = 0;
} else {
ver = ver * 10 + (c - '0');
}
i++;
}
res.push_back(ver);
return res;
}
int compareVersion(string version1, string version2) {
vector<int> ver1s = splitVer(version1);
vector<int> ver2s = splitVer(version2);
int i = 0;
while (i < min(ver1s.size(), ver2s.size())) {
if (ver1s[i] > ver2s[i]) return 1;
if (ver1s[i] < ver2s[i]) return -1;
i++;
}
// special case "1.0" == "1"
for(; i < ver1s.size(); i++) if (ver1s[i] > 0) return 1;
for(; i < ver2s.size(); i++) if (ver2s[i] > 0) return -1;
return 0;
}
};