-
Notifications
You must be signed in to change notification settings - Fork 0
/
leetcode_165.cpp
57 lines (48 loc) · 1.54 KB
/
leetcode_165.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
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
class Solution {
public:
int compareVersion(string version1, string version2) {
int idx1 = 0;
int idx2 = 0;
int length1 = version1.size();
int length2 = version2.size();
while(idx1 < length1 || idx2 < length2){
int s1 = idx1;
int s2 = idx2;
while(idx1 < length1 && version1[idx1] != '.') { idx1++;}
while(idx2 < length2 && version2[idx2] != '.') { idx2++;}
string str1 = (idx1 == s1) ? "0" : version1.substr(s1, idx1 - s1);
string str2 = (idx2 == s2) ? "0" : version2.substr(s2, idx2 - s2);
int num1 = stoi(str1);
int num2 = stoi(str2);
if (num1 != num2) { return (num1 > num2) ? 1: -1; }
idx1 = (idx1 < length1) ? idx1 + 1: idx1;
idx2 = (idx2 < length2) ? idx2 + 1: idx2;
}
return 0;
}
};
class Solution {
public:
int compareVersion(string version1, string version2) {
vector<int> v1;
vector<int> v2;
stringstream ss;
string s;
ss << version1;
while(getline(ss, s, '.')){
v1.push_back(stoi(s));
}
ss.clear();
ss << version2;
while(getline(ss, s, '.')){
v2.push_back(stoi(s));
}
while(v1.size() < v2.size()) v1.push_back(0);
while(v2.size() < v1.size()) v2.push_back(0);
for (int i = 0; i < v1.size(); i++){
if (v1[i] > v2[i]) return 1;
else if (v1[i] < v2[i]) return -1;
}
return 0;
}
};