c++ solution using stringstream


  • 0
    S

    may not be the best solution, but to my understanding it is a little easier to understand (and the speed isn't bad). this should be an easy problem, but if one tries to avoid all the std tools and manage to parse the version number char by char, it may soon get very messy...

    class Solution {
    public:
    	int compareVersion(string version1, string version2) {
    		if (version1 == "" || version2 == "") {return 0;}
    		unsigned int v1 = 0, v2 = 0;
    
    		istringstream ss1(version1);
    		istringstream ss2(version2);
    
    		string t1 = "", t2 = "";
    
    		while(getline(ss1, t1, '.')) {
    			v1 = stoi(t1);
    			if(getline(ss2, t2, '.')) {
    				v2 = stoi(t2);
    			} else v2 = 0;
    			if (v1 > v2) return 1;
    			if (v1 < v2) return -1;
    		}
    		while(getline(ss2, t2, '.')) {
    			v2 = stoi(t2);
    			if( v2 != 0 ) return -1;
    		}
    		return 0;
    	}
    
    
    };

  • 0
    V

    Agree. But still , I guess this won't be the so-called "best" solution the interviewer want to see...


Log in to reply
 

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