My cpp code, more simple.


  • 1
    C
    class Solution {
    public:
        int compareVersion(string version1, string version2) {
            size_t posA = version1.find('.', 0);
            size_t posB = version2.find('.', 0);
            size_t begA = 0; 
            size_t begB = 0;
            do {
                auto verA = 0;
                auto verB = 0;
                if (begA != version1.length()) {
                    verA = stoi(version1.substr(begA, posA-begA));
                } 
                if (begB != version2.length()) {
                    verB = stoi(version2.substr(begB, posB-begB));
                }
                if (verA > verB) {
                    return 1;
                }
                else if (verA < verB) {
                    return -1;
                }
    
                if (posA != string::npos) {
                    begA = posA + 1;
                    posA = version1.find('.', begA);
                }
                else {
                    begA = version1.length();
                }
                if (posB != string::npos) {
                    begB = posB + 1;
                    posB = version2.find('.', begB);
                }
                else {
                    begB = version2.length();
                }
            } while (begA !=  version1.length() || begB != version2.length());
    
            return 0;
        }
    };

Log in to reply
 

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