Friday, April 10, 2015

LeetCode [165] Compare Version Numbers

 165. Compare Version Numbers

Medium

Given two version numbers, version1 and version2, compare them.

    Version numbers consist of one or more revisions joined by a dot '.'. Each revision consists of digits and may contain leading zeros. Every revision contains at least one character. Revisions are 0-indexed from left to right, with the leftmost revision being revision 0, the next revision being revision 1, and so on. For example 2.5.33 and 0.1 are valid version numbers.

    To compare version numbers, compare their revisions in left-to-right order. Revisions are compared using their integer value ignoring any leading zeros. This means that revisions 1 and 001 are considered equal. If a version number does not specify a revision at an index, then treat the revision as 0. For example, version 1.0 is less than version 1.1 because their revision 0s are the same, but their revision 1s are 0 and 1 respectively, and 0 < 1.

    Return the following:

    • If version1 < version2, return -1.
    • If version1 > version2, return 1.
    • Otherwise, return 0.

     

    Example 1:

    Input: version1 = "1.01", version2 = "1.001"
    Output: 0
    Explanation: Ignoring leading zeroes, both "01" and "001" represent the same integer "1".
    

    Example 2:

    Input: version1 = "1.0", version2 = "1.0.0"
    Output: 0
    Explanation: version1 does not specify revision 2, which means it is treated as "0".
    

    Example 3:

    Input: version1 = "0.1", version2 = "1.1"
    Output: -1
    Explanation: version1's revision 0 is "0", while version2's revision 0 is "1". 0 < 1, so version1 < version2.
    

    Example 4:

    Input: version1 = "1.0.1", version2 = "1"
    Output: 1
    

    Example 5:

    Input: version1 = "7.5.2.4", version2 = "7.5.3"
    Output: -1
    

     

    Constraints:

    • 1 <= version1.length, version2.length <= 500
    • version1 and version2 only contain digits and '.'.
    • version1 and version2 are valid version numbers.
    • All the given revisions in version1 and version2 can be stored in a 32-bit integer.
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    class Solution {
    public:
        int compareVersion(string version1, string version2) {
            int n1 = version1.size(), n2 = version2.size(), i1 = 0, i2 = 0, v1 = 0, v2 = 0;
            while(i1<n1 && i2<n2){
                int j1 = i1, j2 = i2;
                while(j1<n1 && version1[j1]!='.') j1++;
                while(j2<n2 && version2[j2]!='.') j2++;
                v1 = stoi(version1.substr(i1, j1-i1));
                v2 = stoi(version2.substr(i2, j2-i2));
                if(v1>v2) return 1;
                else if(v1<v2) return -1;
                i1 = j1+1;
                i2 = j2+1;
            }
            v1 = 0;
            v2 = 0;
            if(i1<n1) v1 = stoi(version1.substr(i1, n1-i1));
            if(i2<n2) v2 = stoi(version2.substr(i2, n2-i2));
            if(v1>v2) return 1;
            else if(v1==v2) return 0;
            else return -1;
        }
    };
    

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    class Solution {
        boolean hasPositive(String str){
            for(int i=0; i<str.length(); ++i){
                if(str.charAt(i)!='.' && str.charAt(i)!='0') return true;
            }
            return false;
        }
    
        public int compareVersion(String version1, String version2) {
            int n1 = version1.length(), n2 = version2.length();
            int i1 = 0, i2 = 0;
            while(i1<n1 && i2<n2){
                int e1 = version1.indexOf('.', i1);
                int e2 = version2.indexOf('.', i2);
                if(e1==-1) e1 = n1;
                if(e2==-1) e2 = n2;
                int v1 = Integer.parseInt(version1.substring(i1, e1));
                int v2 = Integer.parseInt(version2.substring(i2, e2));
                if(v1>v2) return 1;
                if(v1<v2) return -1;
                i1 = e1+1;
                i2 = e2+1;
            }
    
            if(i1<n1 && hasPositive(version1.substring(i1))) return 1;
            if(i2<n2 && hasPositive(version2.substring(i2))) return -1;
            return 0;
        }
    }
    

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    class Solution {
        int compareString(String v1, String v2){
            int i1 = v1.equals("")?0:Integer.parseInt(v1);
            int i2 = v2.equals("")?0:Integer.parseInt(v2);
            if(i1<i2) return -1;
            else if(i1==i2) return 0;
            else return 1;
        }
        public int compareVersion(String version1, String version2) {
            int i = 0, j = 0;
            while(i<version1.length() || j<version2.length()){
                int dotIndex1 = version1.indexOf('.', i);
                String ver1 = version1.substring(i, dotIndex1==-1?version1.length():dotIndex1);
    
                int dotIndex2 = version2.indexOf('.', j);
                String ver2 = version2.substring(j, dotIndex2==-1?version2.length():dotIndex2);
    
                int r = compareString(ver1, ver2);
                if(r!=0) return r;
                
                i = dotIndex1<0?version1.length():dotIndex1+1;
                j = dotIndex2<0?version2.length():dotIndex2+1;
            }
    
            return 0;
        }
    }
    

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    class Solution {
        public int compareVersion(String version1, String version2) {
            String[] list1 = version1.split("\\.");
            String[] list2 = version2.split("\\.");
            int n1 = list1.length, n2 = list2.length;
            int i = 0, j = 0;
            while(i<n1 && j<n2){
                String v1 = trim(list1[i]);
                String v2 = trim(list2[i]);
                int r = value(v1)-value(v2);
                if(r<0) return -1;
                else if(r>0) return 1;
                i++;
                j++;
            }
    
            while(i<n1 && trim(list1[i]).length()==0) i++;
            while(j<n2 && trim(list2[j]).length()==0) j++;
    
            if(i==n1 && j==n2) return 0;
            if(i<n1) return 1;
            return -1;
        }
    
        String trim(String s){
            int i = 0;
            while(i<s.length() && s.charAt(i)=='0') i++;
            return s.substring(i);
        }
        
        int value(String s){
            if(s.length()==0) return 0;
            else return Integer.valueOf(s);
        }
    }
    

    No comments:

    Post a Comment