Given a string containing only digits, restore it by returning all possible valid IP address combinations.
Example:
Input: "25525511135"
Output:
["255.255.11.135", "255.255.111.35"]
将一串字符转换成正确的IP地址,这道题参考了这位博主,点我
class Solution {
public:
void helper(string s, int num, int pos, string path, vector<string> &res){
if(pos==s.size() && num==4){
res.push_back(path.substr(0, path.size()-1));///////////////////
return;
}
if(s.size()-pos > 3*(4-num))
return;
if(pos < s.size()){
path += s.substr(pos,1)+".";
helper(s, num+1, pos+1, path, res);
path = path.substr(0, path.size()-2);
}
if(pos<s.size()-1 && s[pos]!='0'){
path += s.substr(pos, 2)+".";
helper(s, num+1, pos+2, path, res);
path = path.substr(0, path.size()-3);
}
if(pos<s.size()-2 && s[pos]!='0' && s.substr(pos, 3)<="255"){
path += s.substr(pos, 3)+".";
helper(s, num+1, pos+3, path, res);
path = path.substr(0, path.size()-4);
}
}
vector<string> restoreIpAddresses(string s) {
vector<string> res;
string path="";
helper(s, 0, 0, path, res);
return res;
}
};