My JavaScript solution without split


  • 0
    H
    var compareVersion = function(version1, version2) {
        var l1 = version1.length;
        var l2 = version2.length;
        var i11 = 0;
        var i12 = 0;
        var i21 = 0;
        var i22 = 0;
        var a1,a2;
        while(i12<l1 || i22 <l2){
            while(i12<=l1-1){
                if(version1.charAt(i12)=='.'){
                    break;
                }
                i12++
            }
            while(i22<=l2-1){
                if(version2.charAt(i22)=='.'){
                    break;
                }
                i22++
            }
            a1 = +version1.slice(i11,i12);
            a2 = +version2.slice(i21,i22);
            if(a1>a2){
                return 1
            }
            if(a2>a1){
                return -1
            }
            if(i12<l1){
                i11 = i12 = i12+1;
            }else{
                i11 = i12; 
            }
            if(i22<l2){
                i21 = i22 = i22+1;
            }else{
                i21 = i22; 
            }
        }
        return 0;
    };
    

Log in to reply
 

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