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

 

 

比较两个字符串数字的大小:(小数点可能不止一个)

注意的地方:

1、多个小数点的处理。

2、考虑清楚细节就可以了,题目本身不难。

 

 

1、直接判断,代码较多。

public class Solution {
    public int compareVersion(String version1, String version2) {
        int len1 = version1.length();
        int len2 = version2.length();
        int start1 = 0;
        int start2 = 0;
        int[] nums = new int[2];
        while (start1 < len1 && start2 < len2){
            nums = getNum(version1, start1, len1);
            int num1 = nums[1];
            start1 = nums[0];
            nums = getNum(version2, start2, len2);
            int num2 = nums[1];
            start2 = nums[0];
            if (num1 > num2){
                return 1;
            } else if (num1 < num2){
                return -1;
            }
        }
        if (start1 >= len1 && start1 >= len2){
            return 0;
        } else if (start1 >= len1){
            while (start2 < len2){
                nums = getNum(version2, start2, len2);
                if (nums[1] != 0){
                    return -1;
                }
                start2 = nums[0];
            }
            return 0;
        } else {
            while (start1 < len1){
                nums = getNum(version1, start1, len1);
                if (nums[1] != 0){
                    return 1;
                }
                start1 = nums[0];
            }
            return 0;
        }
    }
    public int[] getNum(String str, int start, int len){
        int end = start;
        int[] result = new int[2];
        while (end < len && str.charAt(end) != '.'){
            end++;
        }
        result[0] = end+1;
        if( end == start){
            result[1] = 0;
        } else {
            result[1] = Integer.valueOf(str.substring(start,end));
        }
        return result;
    }
}

 

 

2、同样的处理方式,也有很简单的写法(参考discuss)

public class Solution {
    public int compareVersion(String version1, String version2) {
        int temp1 = 0,temp2 = 0;
        int len1 = version1.length(),len2 = version2.length();
        int i = 0,j = 0;
        while(i<len1 || j<len2) {
            temp1 = 0;
            temp2 = 0;
            while(i<len1 && version1.charAt(i) != '.') {
                temp1 = temp1*10 + version1.charAt(i++)-'0';
            }
            while(j<len2 && version2.charAt(j) != '.') {
                temp2 = temp2*10 + version2.charAt(j++)-'0';
            
            }   
            if(temp1>temp2) return 1;
            else if(temp1<temp2) return -1;
            else {
                i++;
                j++;
            
            }
        
        }
        return 0;
    }
}

 

 

3、使用split()。(参考discuss)

public int compareVersion(String version1, String version2) {
    
    String[] v1 = version1.split("\\.");
    String[] v2 = version2.split("\\.");
    
    for ( int i = 0; i < Math.max(v1.length, v2.length); i++ ) {
        int num1 = i < v1.length ? Integer.parseInt( v1[i] ) : 0;
        int num2 = i < v2.length ? Integer.parseInt( v2[i] ) : 0;
        if ( num1 < num2 ) {
            return -1;
        } else if ( num1 > num2 ) {
            return +1;
        }
    } 
    
    return 0;
}