vector<string> restoreIpAddresses(string s) {
// write code here
int n = s.size();
vector<string> res;
for (int i = 1; i < 4 && i < n - 2; ++i)
{
for (int j = i + 1; j < i + 4 && j < n - 1; ++j)
{
for (int k = j + 1; k < j + 4 && k < n; ++k)
{
if (k < n - 3)
{
continue;
}
string a = s.substr(0, i);
string b = s.substr(i, j - i);
string c = s.substr(j, k - j);
string d = s.substr(k);
if (stoi(a) > 255 || stoi(b) > 255 || stoi(c) > 255 || stoi(d) > 255)
{
continue;
}
if ((a.size() > 1 && a.front() == '0') || (b.size() > 1 && b.front() == '0')
|| (c.size() > 1 && c.front() == '0') || (d.size() > 1 && d.front() == '0'))
{
continue;
}
res.push_back(a + '.' + b + '.' + c + '.' + d);
}
}
}
return res;
}
04-21
484
07-08
491
06-18
2122
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交