✡ leetcode 165. Compare Version Numbers 比較兩個字符串數字的大小 --------- java


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;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM