class Solution {
public:vector<string> strs;//用于存放临时的四个段vector<string> result;//存放结果void dfs(string &s, int beginIndex, int step) {if (step == 4 && beginIndex == s.size()) //搜索成功{string temRec = strs[0] + "." + strs[1] + "." + strs[2] + "." + strs[3];result.push_back(temRec);}else if (step < 4) //当还需要搜索{for (int len = 1; len < 4; ++len) //对这个段的长度进行穷举{if (beginIndex + len <= s.size()) //判断下标的合法性{strs[step] = s.substr(beginIndex, len);//获取这个段//检测strs[step]这个段的合法性//是否是“023”长度大于1且首位位0这种 或者“345”这种超过防伪if ((strs[step][0] == '0' && strs[step].size() > 1) || stoi(strs[step]) > 255) {continue;}dfs(s, beginIndex + len, step + 1);//进行下一步搜索}}}}vector<string> restoreIpAddresses(string s) {strs = vector<string>(4);dfs(s, 0, 0);//开始搜索return result;}
};