# leetcode Restore IP Addresses

Given a string containing only digits, restore it by returning all possible valid IP address combinations.

For example:
Given "25525511135",

return ["255.255.11.135", "255.255.111.35"]. (Order does not matter)

class Solution {
public:
vector<string> restoreIpAddresses(string str){
vector<string>result;
if(str.empty())
return result;
string tmpIP;
getAllIP(result, str, tmpIP, 0, 0);
return result;
}
void getAllIP(vector<string>& result, string str, string tmpIP, int k, int i)
{
int len = str.length();
if(i>4)
return;
if(k == len)
{
if(i==4)
{
tmpIP.pop_back();//去掉最后一个'.'
result.push_back(tmpIP);
}
return;
}
string tmp;
tmp += str[k];
if(getvalue(tmp)<=255)
{
tmpIP += (tmp+'.');
getAllIP(result, str, tmpIP,k+1 ,i+1);
tmpIP.pop_back();
tmpIP.pop_back();
}
if(str[k] != '0')
{
if(k < len-1)
{
tmp += str[k+1];
if(getvalue(tmp)<=255)
{
tmpIP += (tmp+'.');
getAllIP(result, str, tmpIP, k+2, i+1);
tmpIP.pop_back();
tmpIP.pop_back();
tmpIP.pop_back();
}
}
if(k < len-2)
{
tmp += str[k+2];
if(getvalue(tmp)<=255)
{
tmpIP += (tmp+'.');
getAllIP(result, str, tmpIP, k+3 ,i+1);
tmpIP.pop_back();
tmpIP.pop_back();
tmpIP.pop_back();
tmpIP.pop_back();
}
}
}
}
int getvalue(string s)
{
int len = s.length();
int result = 0;
for(int i=0; i<len; ++i)
result = 10*result + s[i]-'0';
return result;
}
};

#### 【LeetCode with Python】 Restore IP Addresses

2014-06-15 22:48:33

#### [LeetCode]Restore IP Addresses, 解题报告

2014-04-07 23:26:56

#### leetCode 93.Restore IP Addresses (恢复IP地址) 解题思路和方法

2015-07-27 09:52:23

#### LeetCode93—Restore IP Addresses

2016-03-08 23:05:38

#### LeetCode-93. Restore IP Addresses (JAVA)（重新组合IP）

2017-04-17 14:18:25

#### LeetCode93:Restore IP Addresses

2015-07-08 15:50:36

#### LeetCode 93. Restore IP Addresses（恢复IP地址）

2016-05-23 00:34:59

#### Restore IP Addresses (Java)

2015-01-19 15:05:12

#### leetcode---Restore IP Addresses---回溯

2016-09-04 21:13:21