LeetCode 165. Compare Version Numbers
作者:互联网
题意:比较两个版本的大小
题解:
class Solution {
public:
int ver1[1005];
int pos1;
int pos2;
int ver2[1005];
int compareVersion(string version1, string version2) {
int i,j;
string s="";
for(i=0;i<version1.length();i++)
{
if(version1[i]=='.')
{
ver1[pos1]=fun(s);
pos1++;
s="";
}
else
{
s+=version1[i];
}
}
ver1[pos1++]=fun(s);
s="";
for( i=0;i<version2.length();i++)
{
if(version2[i]=='.')
{
ver2[pos2]=fun(s);
pos2++;
s="";
}
else
{
s+=version2[i];
}
}
ver2[pos2++]=fun(s);
int ans=0;
for( i=0,j=0;i<pos1&&j<pos2;i++,j++)
{
if(ver1[i]>ver2[j])
{
return 1;
}
else if(ver1[i]<ver2[j])
{
return -1;
}
}
if(i==pos1&&j!=pos2)
{
for(int k=j;k<pos2;k++)
{
if(ver2[k]!=0)
return -1;
}
}
if(i!=pos1&&j==pos2)
{
for(int k=i;k<pos1;k++)
{
if(ver1[k]!=0)
return 1;
}
}
return 0;
}
int fun(string str)
{
string s="";
for(int i=0;i<str.length();i++)
{
if(str[i]=='0'&&s=="")
{
continue;
}
s+=str[i];
}
if(s=="")
s="0";
return atoi(s.c_str());
}
};
标签:ver1,Compare,string,int,题解,Version,165,1005,题意 来源: https://www.cnblogs.com/dacc123/p/12229335.html