题目

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. MYSQL C API : struct MYSQL_STMT 结构的组合使用

    #include <iostream> #include <string> #include <string.h> #include <assert.h> ...

  2. QueryRunner类

    该类简单化了SQL查询,它与ResultSetHandler组合在一起使用可以完成大部分的数据库操作,能够大大减少编码量. QueryRunner类提供了两个构造方法: 默认的构造方法 需要一个 ja ...

  3. 剑指Offer:面试题21——包含min函数的栈(java实现)

    问题描述: 定义栈的数据结构,请在该类型中实现一个能够得到栈的最小元素的min函数.在该栈中,调用min,push及pop的时间复杂度都是O(1). 思路:加入一个辅助栈用来存储最小值集合 (这里要注 ...

  4. 三、Distributing Maya Plugins

    For example, a fully implemented render utility node will have at least three files: the plug-in fil ...

  5. python ConfigParser、shutil、subprocess、ElementTree模块简解

    ConfigParser 模块 一.ConfigParser简介ConfigParser 是用来读取配置文件的包.配置文件的格式如下:中括号“[ ]”内包含的为section.section 下面为类 ...

  6. EXCL poi导入

    public static void importExcel2(File file) throws Exception { InputStream is = new FileInputStream(f ...

  7. 使用DotNetOpenAuth搭建OAuth2.0授权框架

    标题还是一如既往的难取. 我认为对于一个普遍问题,必有对应的一个简洁优美的解决方案.当然这也许只是我的一厢情愿,因为根据宇宙法则,所有事物总归趋于混沌,而OAuth协议就是混沌中的产物,不管是1.0. ...

  8. List集合转换为数组形式

    通过List集合对象的转类型函数 .ToArray() 如List<decimal> → deciaml[] 代码如下: var ComIdList = ComList.Select(p ...

  9. arpg网页游戏之地图(二)

    [转]http://www.cnblogs.com/BlueWoods/p/4684557.html 这一节说说视窗,这个视窗,也就是游戏的视角.现在的网页游戏分为2D游戏,2.5D游戏和3D游戏,2 ...

  10. 无法连接到已配置的开发web服务器

    http://jingyan.baidu.com/article/29697b91099847ab20de3c8b.html 这是防火墙造成的,将防火墙关闭即可