class Solution {
public:
inline int getlen(int digit){
return digit == 0 ? 1 : (((int)log10(digit)) + 1);
}
void dfs(const string& s, const int totlen, int index, int cur, int partnum,vector<string>& ret, string& tmpstr){
if(index >= totlen){
if(partnum == 4){
ret.push_back(tmpstr);
return;
}
}
if(partnum >= 4){
return;
}
for(int tmpidx = index; tmpidx < totlen; ++tmpidx){
cur = cur * 10 + s[tmpidx] - '0';
if(cur >= 0 && cur <= 255){
int originlen = tmpstr.length();
if(partnum != 0){
tmpstr += ".";
}
char line[5];
memset(line, 0, sizeof(line));
sprintf(line, "%d", cur);
if(tmpidx - index + 1 == strlen(line)){
tmpstr += ((string)line);
dfs(s, totlen, tmpidx + 1, 0, partnum + 1, ret, tmpstr);
tmpstr.erase(originlen);
}
}
else{
break;
}
}
}
vector<string> restoreIpAddresses(string s) {
vector<string> ret;
int len = s.length();
int cur = 0;
string tmpstr = "";
dfs(s, len, 0, 0, 0, ret, tmpstr);
return ret;
}
};
错误1:
每次向下dfs时,cur应重新置为0
错误2:
点分十进制中前导零的处理问题