-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path165.compare-version-numbers.cpp
More file actions
75 lines (61 loc) · 1.83 KB
/
165.compare-version-numbers.cpp
File metadata and controls
75 lines (61 loc) · 1.83 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "testharness.h"
#include <string>
#include <string.h>
#include <vector>
using namespace std;
class Solution {
public:
int compareVersion(string version1, string version2) {
vector<string> ver1;
vector<string> ver2;
split(version1, &ver1);
split(version2, &ver2);
if (ver1.size() < ver2.size()) {
ver1.resize(ver2.size(), "0");
} else if (ver2.size() < ver1.size()) {
ver2.resize(ver1.size(), "0");
}
for (size_t i = 0; i < ver1.size(); ++i) {
int ret = compareString(ver1[i], ver2[i]);
if (ret != 0) return ret;
}
return 0;
}
private:
void split(const string& str, vector<string>* out) {
std::size_t pos = 0;
while (true) {
while (str[pos] == '0') {
pos++;
}
if (pos == str.size()) {
(*out).push_back("0");
return;
} else if (str[pos] == '.') {
(*out).push_back("0");
pos++;
} else {
size_t nextPos = str.find_first_of(".", pos);
if (nextPos == string::npos) {
(*out).push_back(str.substr(pos));
return;
} else {
(*out).push_back(str.substr(pos, nextPos - pos));
pos = nextPos + 1;
}
}
}
}
int compareString(const string& str1, const string& str2) const {
size_t size1 = str1.size();
size_t size2 = str2.size();
int diff = size1 - size2;
if (diff != 0)
return diff > 0 ? 1 : -1;
diff = strcmp(str1.c_str(), str2.c_str());
return diff > 0 ? 1 : diff < 0 ? -1 : 0;
}
};
TEST(Solution, test) {
ASSERT_EQ(2, 1+1);
}