0ms simple C++ solution


  • 0
    H
    class Solution {
    public:
        int compareVersion(string version1, string version2) {
            auto d1 = version1.begin(), last1 = version1.end();
            auto d2 = version2.begin(), last2 = version2.end();
            int v1 = 0, v2 = 0;
            while(v1 == v2 && (d1 != last1 || d2 != last2))
            {
                v1 = v2 = 0;
                for(; d1 != last1 && *d1 != '.'; ++d1)
                    v1 = 10 * v1 + *d1 - '0';
                for(; d2 != last2 && *d2 != '.'; ++d2)
                        v2 = 10 * v2 + *d2 - '0';
                d1 += d1 == last1 ? 0 : 1;
                d2 += d2 == last2 ? 0 : 1;
            }
            return v1 == v2 ? 0 : (v1 < v2 ? -1 : 1);
        }
    };
    

Log in to reply
 

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