1. 程式人生 > >[LeetCode] 165. Compare Version Numbers 比較版本數

[LeetCode] 165. Compare Version Numbers 比較版本數

test case numbers cas return only problem it is you div

Compare two version numbers version1 and version1.
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

Credits:
Special thanks to @ts for adding this problem and creating all test cases.

給2個版本的字符串,裏面只含數字和 ‘.‘ ,比較兩個版本的大小。

解法:對字符串以 ‘.‘ ,進行分拆形成數組。然後從數組的第一個元素開始比較,如果相同就比較下一個元素,如果不同v1大就返回1,v2 大就返回-1,如果比較到最後都相同就返回0。

註意:如果產生的兩個數組長度不一樣,例如:1.2 < 1.2.3 則要把短的後面加上‘0‘補齊數組長度和長的一樣,然後在進行比較。

Java:

    String[] arr1 = version1.split("\\.");
    String[] arr2 = version2.split("\\.");
 
    int i=0;
    while(i<arr1.length || i<arr2.length){
        if(i<arr1.length && i<arr2.length){
            if(Integer.parseInt(arr1[i]) < Integer.parseInt(arr2[i])){
                return -1;
            }else if(Integer.parseInt(arr1[i]) > Integer.parseInt(arr2[i])){
                return 1;
            }
        } else if(i<arr1.length){
            if(Integer.parseInt(arr1[i]) != 0){
                return 1;
            }
        } else if(i<arr2.length){
           if(Integer.parseInt(arr2[i]) != 0){
                return -1;
            }
        }
 
        i++;
    }
 
    return 0;
}

Java:

public class Solution {
    public int compareVersion(String version1, String version2) {
        if (version1 == null || version2 == null) return 0;
        String[] v1s = version1.split("\\.");
        String[] v2s = version2.split("\\.");
        int i = 0, j = 0, res = 0;
        while (i < v1s.length || j < v2s.length) {
            int ver1 = i < v1s.length ? Integer.valueOf(v1s[i++]) : 0;
            int ver2 = j < v2s.length ? Integer.valueOf(v2s[j++]) : 0;
            if (ver1 < ver2) return -1;
            else if (ver1 > ver2) return 1;
        }
        return 0;
    }
}

Python:

class Solution2(object):
    def compareVersion(self, version1, version2):
        """
        :type version1: str
        :type version2: str
        :rtype: int
        """
        v1, v2 = version1.split("."), version2.split(".")
        
        if len(v1) > len(v2):
            v2 += [‘0‘ for _ in xrange(len(v1) - len(v2))]
        elif len(v1) < len(v2):
            v1 += [‘0‘ for _ in xrange(len(v2) - len(v1))]
        
        i = 0
        while i < len(v1):
            if int(v1[i]) > int(v2[i]):
                return 1
            elif int(v1[i]) < int(v2[i]):
                return -1
            else:
                i += 1
        
        return 0

Python: My solution  

class Solution(object):
    """
    :type v1, str
    :type v2, str
    :rtype: int
    """
    def compareVersion(self, v1, v2):
        split_v1 = v1.split(‘.‘)
        split_v2 = v2.split(‘.‘)
        print split_v1, split_v2
        n1 = len(split_v1)
        n2 = len(split_v2)
        
        if n1 < n2:
            for i in xrange(n2 - n1):
                split_v1 += ‘0‘
        elif n1 > n2: 
            for i in xrange(n1 - n2):
                split_v2 += ‘0‘
            
        for i in xrange(len(split_v1)):
            if split_v1[i] == split_v2[i]:
                continue
            if split_v1[i] > split_v2[i]:
                return 1
            if split_v1[i] < split_v2[i]:
                return -1
        
        return 0

類似題目:

[LeetCode] 278. First Bad Version 第一個壞版本

  

[LeetCode] 165. Compare Version Numbers 比較版本數