Simple java solution


  • 0
    L
    public int compareVersion(String version1, String version2) {
        char[] v1 = version1.toCharArray();
        char[] v2 = version2.toCharArray();
        int i1 = 0,i2 = 0;
        int s1 = 0,s2 = 0;
        while(i1 < v1.length || i2 < v2.length){
            while(i1 < v1.length && v1[i1] != '.') i1++;
            while(i2 < v2.length && v2[i2] != '.') i2++;
            int a = s1 < v1.length ? Integer.valueOf(version1.substring(s1,i1)) : 0;
            int b = s2 < v2.length ? Integer.valueOf(version2.substring(s2,i2)) : 0;
            if(a > b) return 1;
            if(a < b) return -1;
            s1 = ++i1;
            s2 = ++i2;
        }
        return 0;
    }

Log in to reply
 

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