My 2ms C++ solution


  • 0
    D
    int compareVersion(string version1, string version2) {
    		if (version1 == version2)
    			return 0;
    		char *p;
    		int v1, v2;
    		if (version1.find('.') != string::npos)
    		{
    			v1 = strtol(version1.substr(0,version1.find('.')).c_str(), &p, 10);
    			version1 = version1.substr(version1.find('.')+1, version1.size());
    		}
    		else
    		{
    			v1 = strtol(version1.c_str(), &p, 10);
    			version1 = "";
    		}
    
    		if (version2.find('.') != string::npos)
    		{
    			v2 = strtol(version2.substr(0, version2.find('.')).c_str(), &p, 10);
    			version2 = version2.substr(version2.find('.')+1, version2.size());
    		}
    		else 
    		{
    			v2 = strtol(version2.c_str(), &p, 10);
    			version2 = "";
    		}
    
    		if (v1 > v2)
    			return 1;
    		else if (v1 < v2)
    			return -1;
    		else
    			return compareVersion(version1, version2);
        }

Log in to reply
 

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