题目

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. Linux 11g rac PSU

    PSU补丁:p22191577_112040_Linux-x86-64.zipOPATCH工具:p6880880_112000_LINUX.zip 解压OPATCH工具 覆盖到/u01/11.2.0/ ...

  2. C#中定义数组

    C#定义数组 一.一维:int[] numbers = new int[]{1,2,3,4,5,6}; //不定长 int[] numbers = new int[3]{1,2,3};//定长   二 ...

  3. 【摘】使用tail、head命令过滤行

    tail  -n  10  test.log   查询日志尾部最后10行的日志; tail -n +10 test.log    查询10行之后的所有日志; head -n 10  test.log  ...

  4. 关于sublime text2

    转自:http://www.qianduan.net/essential-to-sublime-the-text-2-plugins.html Sublime Text 2是一个轻量.简洁.高效.跨平 ...

  5. HTML 滚动标签<marquee>

    主要参数: behavior  移动方式 scroll        循环移动 slide         只移动一个回合 alternate   来回移动 direction 移动方向 left r ...

  6. UIUC同学Jia-Bin Huang收集的计算机视觉代码合集

    转自:http://blog.sina.com.cn/s/blog_631a4cc40100wrvz.html   UIUC的Jia-Bin Huang同学收集了很多计算机视觉方面的代码,链接如下: ...

  7. Oracle中快速查询和操作某个用户下的所有表数据信息

    一.禁止所有的外键约束 在pl/sql developer下执行如下语句:SELECT 'ALTER TABLE ' || table_name || ' disable CONSTRAINT ' | ...

  8. 如何实现Qlikview的增量数据加载

    笔者备注: 刚刚接错Qlikview,上网搜集的资料,如何处理增量数据. 1 寻找增量时间戳(1)各种数据库:表的创建时间字段和修改时间字段或者最后的修改时间字段:(2)sql server:可以用找 ...

  9. The Magic only works with total devotion of one's heart

    The Magic only works with total devotion of one's heart All tools and equipments are useless without ...

  10. 1.No MBR错误

    如果提示如下错误: Error: No MBR is found at SD/MMC.                                              Hint: use f ...