0165.cpp (730B)
1 class Solution { 2 public: 3 int compareVersion(string version1, string version2) { 4 stringstream s1(version1), s2(version2); 5 vector<int> v1, v2; 6 7 string crnt; 8 while (getline(s1, crnt, '.')) 9 v1.push_back(stoi(crnt)); 10 while (getline(s2, crnt, '.')) 11 v2.push_back(stoi(crnt)); 12 13 while (v1.size() < v2.size()) 14 v1.push_back(0); 15 while (v1.size() > v2.size()) 16 v2.push_back(0); 17 18 int i = 0, j = 0; 19 while (i < v1.size() && j < v2.size()) { 20 if (v1[i] > v2[j]) return 1; 21 if (v1[i] < v2[j]) 22 return -1; 23 else 24 i++, j++; 25 } 26 27 return 0; 28 } 29 };