这道题目有很多容易出错的地方,然后就是学会使用string的函数。
class Solution {
public:
bool match(string s)
{
if(s.length()==1 && s>="0"&&s<="9")
return true;
if(s.length()==2 && s>="10"&&s<="99")
return true;
if(s.length()==3 && s>="100"&&s<="255")
return true;
return false;
}
void select(int index,int num,string s,string result,vector<string>&V)
{
if(num==5)
{
if(index == s.length())
{
V.push_back(result);
}
return;
}
if(s.length()<=index)
return;
for(int i = 1;i<=3;i++)
{
string str(s,index,i);
if(match(str))
{
string r = result+str;
if(num!=4)
r+=".";
select(index+i,num+1,s,r,V);
}
}
}
vector<string> restoreIpAddresses(string s) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<string> V;
select(0,1,s,"",V);
return V;
}
};