比较两个版本号 version1 和 version2。
如果 version1 > version2 返回 1,如果 version1 < version2 返回 -1, 除此之外返回 0。
你可以假设版本字符串非空,并且只包含数字和 . 字符。
. 字符不代表小数点,而是用于分隔数字序列。
例如,2.5 不是“两个半”,也不是“差一半到三”,而是第二版中的第五个小版本。
示例 1:
输入: version1 = "0.1", version2 = "1.1" 输出: -1
示例 2:
输入: version1 = "1.0.1", version2 = "1" 输出: 1
示例 3:
输入: version1 = "7.5.2.4", version2 = "7.5.3" 输出: -1
class Solution {
public:
int compareVersion(string version1, string version2)
{
int len1 = version1.size();
int len2 = version2.size();
int val1 = 0;
int val2 = 0;
int i = 0;
int j = 0;
while(i < len1 || j < len2)
{
while(i < len1 && version1[i] != '.')
{
val1 = val1 * 10 + version1[i] - '0';
i++;
}
while(j < len2 && version2[j] != '.')
{
val2 = val2 * 10 + version2[j] - '0';
j++;
}
if(val1 > val2)
{
return 1;
}
if(val1 < val2)
{
return -1;
}
i++;
j++;
val1 = 0;
val2 = 0;
}
return 0;
}
};