My ugly code in java


  • 0
    Y
    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++){
            if(i>v1.length-1){
                if(0<Integer.parseInt(v2[i])) return -1;
            }
            else if(i>v2.length-1){
                if(Integer.parseInt(v1[i])>0) return 1;
            }
            else if(Integer.parseInt(v1[i])>Integer.parseInt(v2[i])) return 1;
            else if(Integer.parseInt(v1[i])<Integer.parseInt(v2[i])) return -1;
        }
        return 0;
    }

  • 0
    L

    What does the expression("\.") mean in this code? what's the difference between it and "."?
    Thanks!


  • 0
    M

    Check String.split documentation.

    It takes a regex parameter to split the string into a string array. Since '.' has special meaning in regex, it needs to be escaped with \.


  • 0
    M

    Check String.split documentation.

    It takes a regex parameter to split the string into a string array. Since '.' has special meaning in regex, it needs to be escaped with .


Log in to reply
 

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