Java - no library functions, beats 96%


  • 1
    T
        public int compareVersion(String version1, String version2) {
            char[] arr1 = version1.toCharArray();
            char[] arr2 = version2.toCharArray();
            int i = 0;
            int j = 0;
            for(; i < arr1.length || j < arr2.length;) {
                int vl1 = 0;
                int vl2 = 0;
                while(i < arr1.length && arr1[i] != '.') {
                    char aux = arr1[i++];
                    vl1 *= 10;
                    vl1 += (aux - '0');
                }
                i++;
                while(j < arr2.length && arr2[j] != '.') {
                    char aux = arr2[j++];
                    vl2 *= 10;
                    vl2 += (aux - '0');
                }
                j++;
                if(vl1 > vl2) return 1;
                else if (vl1 < vl2) return -1;
                
            }
            return 0;
        }
    }

Log in to reply
 

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