Compare Version Numbers
2015-03-06 14:44 笨笨的老兔子 阅读(220) 评论(0) 编辑 收藏 举报版本号比较
注意点
- 1.0 < 1.10
- 1.1.1 < 1.1.2
1.0 == 1
思路:利用stringstream和getline结合,按照.为分隔符取出每一部分,转换成Int进行比较
class Solution {
public:
int compareVersion(string version1, string version2) {
stringstream streamVer1(version1);
stringstream streamVer2(version2);
string strVer1, strVer2;
while (!streamVer1.eof() || !streamVer2.eof())
{
int iVer1(0), iVer2(0);
if (!streamVer1.eof())
{
getline(streamVer1, strVer1, '.');
iVer1 = stoi(strVer1);
}
if (!streamVer2.eof())
{
getline(streamVer2, strVer2, '.');
iVer2 = stoi(strVer2);
}
if (iVer1 > iVer2)
{
return 1;
}
if (iVer1 < iVer2)
{
return -1;
}
}
return 0;
}
};