public class Solution {
public int compareVersion(String version1, String version2) {
String[] s1 = version1.split("\\.");
String[] s2 = version2.split("\\.");
int st = 0;
while(true)
{
int n11=0,n22=0;
if(s1.length>st)
{
n11 = Integer.parseInt(s1[st]);
}
if(s2.length>st)
{
n22 = Integer.parseInt(s2[st]);
}
if(n11>n22)
{
return 1;
}
else if(n11<n22)
{
return -1;
}
else
{
if(s1.length<=st&&s2.length<=st)
{
return 0;
}
}
st++;
}
}
}
利用String.split将string划分为string数组,在while循环中逐一比较。