题目

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)

分析

给定一个字符串,给出其可构成的所有ip集合;

下面用两种方法解决这个问题:

  1. 首先,一种简单的做法便是三重循环,得到构成ip地址的四段子地址,然后判断合法与否;该方法效率比较低;但是容易想到;
  2. 第二种方法,便是利用动态规划的思想;
    (1)对于给定的字符串,IP地址要分隔为4段,第一段有最多三种可能2、 25 、 255因为其最长只能为3个字符;
    (2)此时若我们划分的第一段为合法的,我们可以递归将剩余子串划分为3段;将ip1与集合中所有合法后三段ip链接即可;
    (3)明显的,利用以上理念很容易写出递归实现代码;

AC代码

 class Solution {
public:
/*方法一:动态规划方式*/
vector<string> restoreIpAddresses(string s) {
if (s.empty())
return vector<string>(); return getIpAddresses(s, );
} /*s为需要划分的字符串,num为需要划分段数*/
vector<string> getIpAddresses(string s, int num)
{
int len = s.length();
if (s.empty() || num <= || len > * num || len < num)
return vector<string>();
vector<string> ips;
if (num == )
{
if (isLegal(s))
ips.push_back(s);
return ips;
}//if
else{
//得到首段,每段ip长度不超过3
for (int i = ; i < len && i < ; ++i)
{
string ip1 = s.substr(, i + );
if (isLegal(ip1))
{
vector<string> tmpIps = getIpAddresses(s.substr(i + ), num - );
if (!tmpIps.empty())
{
auto iter = tmpIps.begin();
while (iter != tmpIps.end())
{
string ip = ip1 + "." + *iter;
ips.push_back(ip);
++iter;
}//while
}//if
}//if
}//for
return ips;
}
} /*判断ip段是否合法*/
bool isLegal(string ip)
{
if (ip.empty())
return false;
else if (ip[] == '')
return ip.length() == ;
else{
int pos = ip.length() - ;
int sum = , tmp = ;
while (pos >= )
{
sum = sum + (ip[pos] - '') * tmp;
if (sum > )
return false;
tmp *= ;
--pos;
}//while
}//else
return true;
} /*方法二:三重循环,效率低*/
vector<string> restoreIpAddresses2(string s) {
vector<string> ips;
if (s.empty())
return vector<string>(); int len = s.length();
for (int i = ; i < len - ; ++i)
{
for (int j = i + ; j < len - ; ++j)
{
for (int k = j + ; k < len; ++k)
{
string ip1 = s.substr(, i + );
string ip2 = s.substr(i + , j - i);
string ip3 = s.substr(j + , k - j);
string ip4 = s.substr(k + );
if (isLegal(ip1) && isLegal(ip2) && isLegal(ip3) && isLegal(ip4))
ips.push_back(ip1 + "." + ip2 + "." + ip3 + "." + ip4);
}//for
}//for
}//for return ips;
}
};

GitHub测试程序源码

LeetCode(93) Restore IP Addresses的更多相关文章

  1. Leetcode 22. Generate Parentheses Restore IP Addresses (*) 131. Palindrome Partitioning

    backtracking and invariant during generating the parathese righjt > left  (open bracket and cloas ...

  2. Leetcode(93): Restore IP Addresses

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

  3. LeetCode(93): 复原IP地址

    Medium! 题目描述: 给定一个只包含数字的字符串,复原它并返回所有可能的 IP 地址格式. 示例: 输入: "25525511135" 输出: ["255.255. ...

  4. LeetCode之“字符串”:Restore IP Addresses

    题目链接 题目要求: Given a string containing only digits, restore it by returning all possible valid IP addr ...

  5. [leetcode.com]算法题目 - Restore IP Addresses

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

  6. leetcode 93. Restore IP Addresses(DFS, 模拟)

    题目链接 leetcode 93. Restore IP Addresses 题意 给定一段序列,判断可能组成ip数的所有可能集合 思路 可以采用模拟或者DFS的想法,把总的ip数分成四段,每段判断是 ...

  7. 【LeetCode】93. Restore IP Addresses

    Restore IP Addresses Given a string containing only digits, restore it by returning all possible val ...

  8. 93. Restore IP Addresses

    题目: Given a string containing only digits, restore it by returning all possible valid IP address com ...

  9. 93.Restore IP Addresses(M)

    93.Restore IP Addresses Medium 617237FavoriteShare Given a string containing only digits, restore it ...

随机推荐

  1. NSUserDefaults简介及使用

    NSUserDefaults类提供了一个与默认系统进行交互的编程接口.NSUserDefaults对象是用来保存,恢复应用程序相关的偏好设置,配置数据等等.默认系统允许应用程序自定义它的行为去迎合用户 ...

  2. ubuntu下安装php memcache扩展

    memcached 安装sudo apt-get install memcached memcached 参数说明memcached -d -m 50 -p 11211 -u root-m 指定使用多 ...

  3. eclipse 工程默认编码修改 JSP编码修改

    1. Window->Preferences->General->Workspace->Text file encoding 将其改为UFT-8  新建的文件即为UTF-8编码 ...

  4. (转载)SQL Server 2005 如何启用xp_cmdshell组件

    原文地址:http://www.cnblogs.com/atree/p/SQL_SERVER_xp_cmdshell.html [错误描述]: SQL Server阻止了对组件‘xp_cmdshell ...

  5. tiny学习3

    这几天在埋头写自己的个星期!而且由于它是基于事件发生的次序(小时就把我的文件导出来了--呵呵.在阅读本文之前,请先看看我Blog里转贴的<TinyXML学习笔记>,相信它能给各位一个关于T ...

  6. 8,SFDC 管理员篇 - 数据模型 - 公式和验证 2

    1, Checkbox 只接受真值或者假值 And(arg1, arg2....)至少两个参数,只有参数都为真时候,才返回真,只要有一个为假,就都为假 例如:AND(DoNotCall, HasOpt ...

  7. MVC 自定义IModelBinder实现json参数转Dictionary<string, string>

    IModelBinder的学习不算深入,现在用它来实现一个json转Dictionary<string, string> 一.原始json转Dictionary<string, st ...

  8. http的header参数有关

    1.读文件 a.如果经过php处理:变成mime:text/html,在浏览器可以打开.否则是下载 b.phpui如果加上参数resid=01,那么返回的数据的Content-Type:applica ...

  9. 基于WDF的PCI/PCIe接口卡Windows驱动程序(4)- 驱动程序代码(源文件)

    原文出处:http://www.cnblogs.com/jacklu/p/4687325.html 本篇文章将对PCIe驱动程序的源文件代码作详细解释与说明.整个WDF驱动程序工程共包含4个头文件(已 ...

  10. 关于mysql 的一些零碎.

    /* 又在做自己以前做的事.总是拿以前的眼光来看现在,导致了其实自己已经很low,但是还觉得自己很xxx. 好吧,最近开始PHP审计.jishigou!!!!!! */ 查看日志情况. show va ...