My solution using stringstream


  • 0
    V
    class Solution {
    public:
        int compareVersion(string version1, string version2) {
            stringstream s1, s2;
            s1 << version1;
            s2 << version2;
        
            char dot;
            int n1, n2;
            while( s1 && s2 )
            {
                s1 >> n1;
                s2 >> n2;
                if( n1 > n2 )
                    return 1;
                else if( n1 < n2 )
                    return -1;
                s1 >> dot;
                s2 >> dot;
            }
        
            while( s1 )
            {
                s1 >> n1;
                if( n1 )
                    return 1;
                s1 >> dot;
            }
    
            while( s2 )
            {
                s2 >> n2;
                if( n2 )
                    return -1;
                s2 >> dot;
            }
    
            return 0;
        }
    };

  • 0
    T

    very concise and clean, but too slow


Log in to reply
 

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