Simple java solution


  • 1
    H
    public int compareVersion(String version1, String version2) {
        String[] s1 = version1.split("\\.");
        String[] s2 = version2.split("\\.");
        for (int i = 0; i < Math.min(s1.length, s2.length); i++) {
            int n1 = Integer.parseInt(s1[i]);
            int n2 = Integer.parseInt(s2[i]);
            if (n1 > n2) {return 1;}
            if (n1 < n2) {return -1;}
        }
        if (s1.length > s2.length) {
            int p1 = s2.length;
            while (p1 < s1.length && Integer.parseInt(s1[p1]) == 0) {
                p1++;
            }
            if (p1 < s1.length) return 1;
        }
        if (s2.length > s1.length) {return -compareVersion(version2, version1);}
        return 0;
    }

Log in to reply
 

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