Share my recursion solution C++


  • 0
    M
    int compareVersion(string version1, string version2){
    int i = 0,
    	j = 0,
    	n1 = 0,
    	n2 = 0;
    while(version1[i] && version1[i] != '.'){
    	n1 *= 10;
    	n1 += (version1[i++] - '0');
    }
    if (version1[i] == '.') 
    	version1 = version1.substr(i + 1);
    else
    	version1 = "0";
    while(version2[j] && version2[j] != '.'){
    	n2 *= 10;
    	n2 += (version2[j++] - '0');
    }
    if (version2[j] == '.') 
    	version2 = version2.substr(j + 1);
    else
    	version2 = "0";
    
    if ( n1 > n2)
    {
    	return 1;
    }else if ( n1 < n2)
    {
    	return -1;
    }else if (!(version1 == "0" && version2 == "0") && n1 == n2)
    {
    	return compareVersion(version1, version2);
    }else
    	return 0;
    }

Log in to reply
 

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