Accepted small Java solution.


  • 134
    P

    This code assumes that next level is zero if no mo levels in shorter version number. And than compare levels.

    public int compareVersion(String version1, String version2) {
        String[] levels1 = version1.split("\\.");
        String[] levels2 = version2.split("\\.");
        
        int length = Math.max(levels1.length, levels2.length);
        for (int i=0; i<length; i++) {
        	Integer v1 = i < levels1.length ? Integer.parseInt(levels1[i]) : 0;
        	Integer v2 = i < levels2.length ? Integer.parseInt(levels2[i]) : 0;
        	int compare = v1.compareTo(v2);
        	if (compare != 0) {
        		return compare;
        	}
        }
        
        return 0;
    }

  • 0
    N

    Thanks for sharing.
    What does "\." mean? What is the different between "\." and "."?


  • 2
    P

    "." means a dot in regexp, and "." means any character in regexp.


  • -14
    J

    Code looks really similar to mine with one difference: I did not use Math.max, but used two comparisons in while loop:

        int count = 0;
        while (count < subversions1.length || count < subversions2.length) {
            ....
            count++;
        }
    

  • 0
    I

    If the length of any sub-string is too long (the value of each integer >= 2^32), then this code will not work.


  • 0
    H

    There is actual meaning of the numbers in this problem. How can you expect a version number to so large...... Your concern is not realistic obviously.


  • 1

    Nice Java split. C++ seems to have no such nice functions.


  • 0
    S

    Do you mean that \. means any character?


  • 2
    J

    . means any character, "\." means "."


  • 2
    D

    for example: "2.105" and "2.22", this will return the wrong answer.


  • 0
    F

    What if levels1[i] can't be parese as int, for instance levels1[i] is over Integer.MAX_VALUE. Thus I think you'd better not use Integer.parseInt to parse integer


  • 0
    S

    @franklovejava If a version number can really be that big...maybe we could use Long.parseLong()?


  • 3
    A

    @Dawn4596875
    Yes i think solution is wrong as its converting characters after "." as interger and comparing
    them . Will fail for cases like 1.02 and 1.1 as it will treat numbers after "." as 2 and 1 , thus giving 1.02 > 1.1 which is wrong.


  • 1

    Very clever to put spot zeros if an array reaches the end. I like your solution


  • 1
    B

    @Dawn4596875 This is not wrong. For version number, "2.105" is bigger thant "2.22".


  • 0
    4

    @buzhiweihe By bigger you mean older?


  • 3
    B

    @4dawn4596875

    By "bigger" I mean "newer".

    In this example of version numbers of "2.105" and "2.22", their major numbers are both "2". And for the minor numbers, "105" is bigger than "22". Since the version number always increases for each product release, version "2.105" is released after "2.22". So "2.105" is the later release.

    Therefor for this problem, compareVersion("2.105", "2.22") should return 1.

    Thanks!


  • 0
    Y

    Nice solution. Thanks. Could any one tell me the time complexity?

    My understanding is:

    1. split: O(n), n is the length of the longer string array.
    2. loop: O(kn), k is the average length of subversion number
      I check the Integer.parseInt, it should be O(k).

    So the result will be O(kn)? Am I right? Thanks.


  • 0
    S

    why the solution use two \\ in \\.? i thought \. should be fine
    Anyone could help explain a little in detail? thx!


  • 0
    Y

    Nice solution, but maybe we need to tell the interviewer the boundary case when string num is too big to convert to integer.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.