4ms,AC,hope give you some idea,shanks!


  • 0
    K
     int compareVersion(string version1, string version2) {
            int m1=0,m2=0;
            int n1=version1.size();
            int n2=version2.size();
            int i=0,j=0;
            while(i<n1||j<n2)
            {
                m1=0;
                m2=0;
                for(;version1[i]!='.'&&i<n1;i++)
                {
                    m1*=10;
                    m1+=(int)(version1[i]-'0');
                }
                if(version1[i]=='.')i++;
               for(;version2[j]!='.'&&j<n2;j++)
                {
                    m2*=10;
                    m2+=(int)(version2[j]-'0');
                }
               if(version2[j]=='.')j++;
                if(m1>m2)return 1;
                else if(m1<m2)return -1;
                else
                continue;
            }
            return 0;
        }

Log in to reply
 

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