My c++ solution, not using recursion


  • 0
    S
    class Solution {
    public:
        int compareVersion(string version1, string version2) {
            int index1 = 0;
            int index2 = 0;
            int ret = 0;
            while (index1 != -1 || index2 != -1) {
                int data1 = 0;
                int data2 = 0;
                if (-1 != index1) {
                    index1 = version1.find('.');
                    string sub = (index1 == -1) ? version1 : version1.substr(0, index1);
                    istringstream is(sub);
                    is >> data1;
                    if (-1 != index1) {
                        version1 = version1.substr(index1 + 1);
                    }
                }
                if (-1 != index2) {
                    index2 = version2.find('.');
                    string sub = (index2 == -1) ? version2 : version2.substr(0, index2);
                    istringstream is(sub);
                    is >> data2;
                    if (-1 != index2) {
                        version2 = version2.substr(index2 + 1);
                    }
                }
    
                if (data1 > data2) {
                    ret = 1;
                    break;
                } else if (data1 < data2) {
                    ret = -1;
                    break;
                }
            }
            return ret;
        }
    };

Log in to reply
 

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