转换成int处理,由于存入vector,要处理容器匹配问题,
因此需要进行去除后缀0
</pre><pre name="code" class="cpp">/**
* @author johnsondu
* @time 13:21 16th Oct 2015
* @status Accepted
* @strategy convert into integer to compare
* @problem Compare Version Numbers
* @url https://leetcode.com/problems/compare-version-numbers/
*/
class Solution {
public:
int compareVersion(string version1, string version2) {
vector<int> v1, v2, vv1, vv2;
// convert into a series of numbers
int idx = 0;
while(idx < version1.size()) {
int cur = 0;
while(idx < version1.size() && version1[idx] != '.') {
cur = cur * 10 + version1[idx] - '0';
idx ++;
}
idx ++;
v1.push_back(cur);
}
// in case of trail zeros
idx = v1.size()-1;
while(idx >= 0 && v1[idx] == 0) idx --;
for(int i = 0; i <= idx; i ++) vv1.push_back(v1[i]);
if(vv1.size() == 0) vv1.push_back(0);
// convert into a series of numbers
idx = 0;
while(idx < version2.size()) {
int cur = 0;
while(idx < version2.size() && version2[idx] != '.') {
cur = cur * 10 + version2[idx] - '0';
idx ++;
}
idx ++;
v2.push_back(cur);
}
// in case of trail zeros
idx = v2.size() - 1;
while(idx >= 0 && v2[idx] == 0) idx --;
for(int i = 0; i <= idx; i ++) vv2.push_back(v2[i]);
if(vv2.size() == 0) vv2.push_back(0);
return (vv1 == vv2 ? 0 : (vv1 > vv2 ? 1 : -1));
}
};