- 很简单的一道题目
- #include
#include
using namespace std;
int string2int(const string &v1)
{
int sum = 0;
for (int i = 0; i < v1.size(); i++)
{
sum = sum * 10 + v1[i] - ‘0’;
}
return sum;
}
int CompareVersionNumbers(string v1,string v2)
{
int i1 = 0;
int i2 = 0;
while (v1.size()!=0 && v2.size()!=0)
{
int pos1 = v1.find(’.’);
int pos2 = v2.find(’.’);
string str1 = v1.substr(0, pos1);
string str2 = v2.substr(0, pos2);
if (string2int(str1) > string2int(str2))
{
return 1;
}
else if (string2int(str1) < string2int(str2))
{
return -1;
}
v1 = v1.substr(pos1 + 1, v1.size() - 1);
v2 = v2.substr(pos2 + 1, v2.size() - 1);
}
if (v1.size() != 0)
{
while (v1.size() != 0)
{
int pos1 = v1.find(’.’);
string str1 = v1.substr(0, pos1);
if (string2int(str1) > 0)
{
return 1;
}
v1 = v1.substr(pos1 + 1, v1.size() - 1);
}
}
else if (v2.size() != 0)
{
while (v2.size() != 0)
{
int pos1 = v2.find(’.’);
string str1 = v2.substr(0, pos1);
if (string2int(str1) > 0)
{
return -1;
}
v2 = v2.substr(pos1 + 1, v2.size() - 1);
}
}
return 0;
}
int main()
{
string v1 = “1.0.0”;
string v2 = “1”;
cout << CompareVersionNumbers(v1, v2) << endl;
system(“pause”);
return 0;
}
[LeetCode] Compare Version Numbers 版本比较
最新推荐文章于 2022-02-25 10:10:01 发布