My 0ms c++ is it a problem of OJ

  • -2
    bool valid(string &str)
    	int len=str.length();
    	if(!len)  return false;
    	if(len!=1&&str[0]=='0')  return false;
    	int i, sum=0;
    	for(i=0; i<len; i++)
    		sum = sum*10+str[i]-'0';
    	if(sum<0||sum>255)  return false;
    	return true;
    vector<string> restoreIpAddresses(string s) {
    	vector<string> rv;
    	int len=s.length();
    	string first, second, third, forth;
    	if(len<4||len>12) return rv;
    	int i, j, k;
    	for(i=1; i<4; i++)
    		first=s.substr(0, i);
    		if(!valid(first)) continue;
    		for(j=i+1; j<i+4; j++)
    			second=s.substr(i, j-i);
    			if(!valid(second)) continue;
    			for(k=j+1; k<min(j+4, len); k++)
    				third=s.substr(j, k-j);
    				forth=s.substr(k, len-k);
    				if(valid(third)&&valid(forth)) rv.push_back(first+'.'+second+'.'+third+'.'+forth);
    	return rv;

Log in to reply

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