【Leetcode | 】93. 复原IP地址
生活随笔
收集整理的這篇文章主要介紹了
【Leetcode | 】93. 复原IP地址
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
class Solution {
public:vector<string> strs;//用于存放臨時(shí)的四個(gè)段vector<string> result;//存放結(jié)果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) //當(dāng)還需要搜索{for (int len = 1; len < 4; ++len) //對這個(gè)段的長度進(jìn)行窮舉{if (beginIndex + len <= s.size()) //判斷下標(biāo)的合法性{strs[step] = s.substr(beginIndex, len);//獲取這個(gè)段//檢測strs[step]這個(gè)段的合法性//是否是“023”長度大于1且首位位0這種 或者“345”這種超過防偽if ((strs[step][0] == '0' && strs[step].size() > 1) || stoi(strs[step]) > 255) {continue;}dfs(s, beginIndex + len, step + 1);//進(jìn)行下一步搜索}}}}vector<string> restoreIpAddresses(string s) {strs = vector<string>(4);dfs(s, 0, 0);//開始搜索return result;}
};
?
總結(jié)
以上是生活随笔為你收集整理的【Leetcode | 】93. 复原IP地址的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 永劫无间截图保存在哪
- 下一篇: 海量数据(一)