-
Notifications
You must be signed in to change notification settings - Fork 1
/
165.cpp
37 lines (33 loc) · 995 Bytes
/
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
// 165. Compare Version Numbers - https://leetcode.com/problems/compare-version-numbers
#include "bits/stdc++.h"
using namespace std;
class Solution {
public:
int compareVersion(string version1, string version2) {
stringstream ss1(version1), ss2(version2);
vector<string> tokens1, tokens2;
string token;
char delim = '.';
while(getline(ss1, token, delim)) {
tokens1.emplace_back(token);
}
while(getline(ss2, token, delim)) {
tokens2.emplace_back(token);
}
int n1 = (int)tokens1.size();
int n2 = (int)tokens2.size();
int n = max(n1, n2);
for (int idx = 0; idx < n; ++idx) {
int v1 = idx < n1 ? stoi(tokens1[idx]) : 0;
int v2 = idx < n2 ? stoi(tokens2[idx]) : 0;
if (v1 != v2) {
return v1 < v2 ? -1 : 1;
}
}
return 0;
}
};
int main() {
ios::sync_with_stdio(false);
return 0;
}