Java solution, 0ms, beats 96.45%


  • 3
    W
    public int compareVersion(String version1, String version2) {
        int i = 0, j = 0, len1 = version1.length(), len2 = version2.length();
        char[] c1 = version1.toCharArray(), c2 = version2.toCharArray();
        while (i < len1 || j < len2) {
            int cur1 = 0, cur2 = 0;
            while (i < len1 && c1[i] != '.') cur1 = cur1 * 10 + c1[i++] - '0';
            while (j < len2 && c2[j] != '.') cur2 = cur2 * 10 + c2[j++] - '0';
            if (cur1 > cur2) return 1;
            if (cur1 < cur2) return -1;
            i++;
            j++;
        }
        return 0;
    }

  • 0
    S

    Nice trick. Thank you.


  • 0
    G

    @w28l30 Excellent solution!


Log in to reply
 

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