C++ use istringstream,the most clean solution


  • 0
    H
    class Solution {
    public:
    int compareVersion(string version1, string version2) {
        istringstream v1(version1);
    	istringstream v2(version2);
    	int i1,i2;
    	int flag1=0;
    	int flag2=0;
    	while(true)
    	{
    		if(v1>>i1)  flag1=0; 
    		if(v2>>i2)  flag2=0;
    		v1.ignore(version1.size(),'.');
    		v2.ignore(version2.size(),'.');
    		if(i1<i2)  return -1;
    		if(i1>i2)  return 1;
    		if(flag1&&flag2)  return 0;
    		if(i1==i2)
    		{
    		 flag1=flag2=1;
             i1=i2=0;
    		}		
    	}
    } 
    

    };


Log in to reply
 

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