My C++ solution


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

Log in to reply
 

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