Share My AC Solution(C++)


  • 0
    C
    class Solution {
    public:
        int compareVersion(string version1, string version2){
            vector<int> v1;
            vector<int> v2;
            int temp = 0;
            
            for(int i = 0; i < version1.length(); i++){
                if (version1[i] == '.'){
                    v1.push_back(temp);
                    temp = 0;
                    continue;
                }
                temp *= 10;
                temp += version1[i] - '0';
            }
            v1.push_back(temp);
            
            temp = 0;
            
            for(int i = 0; i < version2.length(); i++){
                if (version2[i] == '.'){
                    v2.push_back(temp);
                    temp = 0;
                    continue;
                }
                temp *= 10;
                temp += version2[i] - '0';
            }
            v2.push_back(temp);
            
            if (v1.size() < v2.size()){
                for (int i = 0; i < v2.size() - v1.size(); i++){
                    v1.push_back(0);
                }
            }
            
            if (v1.size() > v2.size()){
                for (int i = 0; i < v1.size() - v2.size(); i++){
                    v2.push_back(0);
                }
            }
            
            for (int i = 0; i < v1.size(); i++){
                if (v1[i] > v2[i]){
                    return 1;
                }else if (v1[i] < v2[i]){
                    return -1;
                }
            }
            return 0;
        }
        
    };

Log in to reply
 

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