0ms solution in C++ using extra m+n space


  • 0
    class Solution {
        void getLevel(string &s, vector<int> & level){
            level.push_back(0);
            for( auto it: s ){
                if( it == '.' ){
                    level.push_back(0);
                    continue;
                }
                level.back() = 10*level.back() + it-'0';
            }
            while(level.back() == 0 ) level.pop_back();
        }
    public:
        int compareVersion(string version1, string version2) {
            vector<int> level1, level2;
            getLevel(version1, level1);
            getLevel(version2, level2);
            for( int i=0 ; i<level1.size() && i<level2.size() ; i++){
                if( level1[i] == level2[i] ) continue;
                if( level1[i] > level2[i] ) return 1;
                else return -1;
            }
            return level1.size() == level2.size() ? 0 : (level1.size() > level2.size() ? 1 : -1);
        }
    };
    

Log in to reply
 

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