The code works well on my laptop , but has "runtime error" with "01" "1" and "failed" with "1.0.5" "1" on Leetcode.


  • 0
    F
    class Solution {
    public:
            string erasePreDot(string v) {
                    unsigned dot = v.find('.');
                    return (dot == string::npos) ? "0" : v.erase(0, dot + 1);
            }
    	
            int compareVersion(string version1, string version2) {
                    if (version1.compare(version2) == 0)
                            return 0;
    
                    int pre1 = atoi(version1.c_str());
                    int pre2 = atoi(version2.c_str());
                    if (pre1 > pre2)
                            return 1;
                    else if (pre1 < pre2)
                            return -1;
                    else {
                            version1 = erasePreDot(version1);
                            version2 = erasePreDot(version2);
                            return compareVersion(version1, version2);
                    }
            }
    };

Log in to reply
 

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