题目地址:https://leetcode.com/problems/compare-version-numbers/
Compare two version numbers version1 and version2.
If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0.
You may assume that the version strings are non-empty and contain only digits and the . character.
The . character does not represent a decimal point and is used to separate number sequences.
For instance, 2.5 is not “two and a half” or “half way to version three”, it is the fifth second-level revision of the second first-level revision.
Here is an example of version numbers ordering:
0.1 < 1.1 < 1.2 < 13.37
版本号比较可参考译文版本号比较(Compare two Version numbers) 。
思路也比较简单,版本号分为大版本号(第一个.以前的数字),二级版本号……等等。
将版本号按照’.’分割成字符串数组,然后再将数组中的每个字符串转为数字,字符串转数字可参考【字符串转整数(String to Integer (atoi))】,然后对数字进行比较,得出结果:
public class CompareVersionNumbers {
public static int compareVersion(String version1, String version2) {
String[] v1 = version1.split("\\.");
String[] v2 = version2.split("\\.");
int i = 0;
for (; i < v1.length && i < v2.length; i++) {
if (myAtoi(deleteHeadZeros(v1[i])) > myAtoi(deleteHeadZeros(v2[i])))
return 1;
else if (myAtoi(deleteHeadZeros(v1[i])) == myAtoi(deleteHeadZeros(v2[i])))
continue;
else
return -1;
}
if (v1.length > v2.length) {
// 处理"1.0"与"1" 的情况
while (i < v1.length && myAtoi(deleteHeadZeros(v1[i])) == 0) {
i++;
continue;
}
if (i == v1.length)
return 0;
return 1;
}
else if (v1.length == v2.length)
return 0;
else {
while (i < v2.length && myAtoi(deleteHeadZeros(v2[i])) == 0) {
i++;
continue;
}
if (i == v2.length)
return 0;
return -1;
}
}
// 处理"00000010"这样类似的情况
public static String deleteHeadZeros(String s) {
while (s.charAt(0) == '0' && s.length() >= 2) {
s = s.substring(1);
}
return s;
}
// 字符串转整数
public static int myAtoi(String str) {
if (str == null)
return 0;
str = str.trim();
if (str.equals("") || str.equals("+") || str.equals("-"))
return 0;
if (str.length() == 1){
if (str.charAt(0) >= '0' && str.charAt(0) <= '9')
return str.charAt(0) - '0';
else
return 0;
}
if (str.length() > 1 && (str.charAt(0) == '+' || str.charAt(0) == '-')){
if (str.charAt(1) < '0' || str.charAt(1) > '9')
return 0;
}else if (str.length() > 1 && (str.charAt(0) < '0' || str.charAt(0) > '9')){
return 0;
}
if (str.length() > 1 && (str.charAt(1) == '+' || str.charAt(1) == '-'))
return 0;
str = maxLengthInteger(str);
if (str.charAt(0) == '-'){
if (str.length() > 11){
return -2147483648;
}
if (str.length() == 11 && str.compareTo("-2147483648") > 0){
return -2147483648;
}
return Integer.parseInt(str);
}else if (str.charAt(0) == '+'){
if (str.length() > 11){
return 2147483647;
}
if (str.length() == 11 && str.compareTo("+2147483647") > 0){
return 2147483647;
}
return Integer.parseInt(str.substring(1));
}else {
if (str.length() > 10)
return 2147483647;
if (str.length() == 10 && str.compareTo("2147483647") > 0)
return 2147483647;
return Integer.parseInt(str);
}
}
public static String maxLengthInteger(String str){
int k = 0;
if (str.charAt(0) == '-' || str.charAt(0) == '+'){
k = 1;
while(true) {
if (k > str.length() - 1 || (str.charAt(k) < '0' || str.charAt(k) > '9')) {
k--;
break;
}
else
k++;
}
}else{
while(true) {
if (k > str.length() - 1 || (str.charAt(k) < '0' || str.charAt(k) > '9')) {
k--;
break;
}
else
k++;
}
}
return str.substring(0, k + 1);
}
}
虽然题目比较好理解,但是想要把全部的test cases全部cover掉还是要考虑不少情况的,主要也就是两点,在代码注释中已经写出来了,由于题目中给了假设限制,所以很多其他特殊的情况就不需要再做考虑了。