LeetCode(93) 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)
分析
给定一个字符串,给出其可构成的所有ip集合;
下面用两种方法解决这个问题:
- 首先,一种简单的做法便是三重循环,得到构成ip地址的四段子地址,然后判断合法与否;该方法效率比较低;但是容易想到;
- 第二种方法,便是利用动态规划的思想;
(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;
}
};
LeetCode(93) Restore IP Addresses的更多相关文章
- Leetcode 22. Generate Parentheses Restore IP Addresses (*) 131. Palindrome Partitioning
backtracking and invariant during generating the parathese righjt > left (open bracket and cloas ...
- Leetcode(93): Restore IP Addresses
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- LeetCode(93): 复原IP地址
Medium! 题目描述: 给定一个只包含数字的字符串,复原它并返回所有可能的 IP 地址格式. 示例: 输入: "25525511135" 输出: ["255.255. ...
- LeetCode之“字符串”:Restore IP Addresses
题目链接 题目要求: Given a string containing only digits, restore it by returning all possible valid IP addr ...
- [leetcode.com]算法题目 - Restore IP Addresses
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- leetcode 93. Restore IP Addresses(DFS, 模拟)
题目链接 leetcode 93. Restore IP Addresses 题意 给定一段序列,判断可能组成ip数的所有可能集合 思路 可以采用模拟或者DFS的想法,把总的ip数分成四段,每段判断是 ...
- 【LeetCode】93. Restore IP Addresses
Restore IP Addresses Given a string containing only digits, restore it by returning all possible val ...
- 93. Restore IP Addresses
题目: Given a string containing only digits, restore it by returning all possible valid IP address com ...
- 93.Restore IP Addresses(M)
93.Restore IP Addresses Medium 617237FavoriteShare Given a string containing only digits, restore it ...
随机推荐
- linux 安装软件程序
1.用aptitude管理软件包 查看已安装的/未安装的等软件包 无法通过aptitude看到一个细节是所有跟某个特定软件包关联的所有文件的列表.利用dpkg命令能看到这个列表. dpkg -L pa ...
- Makefile 自动化变量
Makefile中常用自动化变量解释如下: $@------规则的目标文件名 $<------规则的第一个依赖项文件名 $^------规则的所有依赖文件列表,以空格隔开. $?-------所 ...
- [DFNews] GetData也出取证软件了
从事计算机取证的应该都听说过MIP(Mount Image Pro).VFC仿真和Recover My Files,上述三个应用比较广泛的软件都是GetData公司的产品.GetData现在也推出了自 ...
- 【转】Using Gamma 2.2
This is a detailed description of the work with Gamma 2.2. If you are only interested in exact instr ...
- Linux VM acquisition
The evidence is a VM as below. The flat vmdk is the real disk, and the vmdk only 1kb is just a descr ...
- writel(readl(&pwm_timer->tcfg0) | 0xff, &pwm_timer->tcfg0);
解析这句代码什么意思! 神说:选定预分频器0 为什么? 神说:因为实验中选的是timer1,在预分频器0下: 若选择timer4,该如何写这句代码? 首先看tcfg0中选择预分频器1,在看tcfg1里 ...
- 转:RTC搭建android下三层应用程序访问服务器MsSql-服务器端
原文:http://www.cnblogs.com/delphi007/p/3346061.html 前几天通过Ro搭建webservice,然后在android下调用,虽然已近成功,但是返回的数据库 ...
- IMoniker接口的MKParseDisplayName和 GetDisplayName的注意事项
IMoniker接口的MKParseDisplayName和 GetDisplayName的注意事项
- Web前端之html_day1
1.html结构 1 2 3 4 5 6 7 8 9 10 <!DOCTYPE html> <html lang="en"> <head> ...
- 实现MFC菜单画笔画圆,并且打钩
这个是用最简单的方法,移动客户区,圆会不见,所以下一篇我还要改进. 首先新建一个MFC单文件,在资源那里的菜单下,建立画笔,可以弹出红画笔,蓝画笔和绿画笔,,给出ID_RED,ID_BLUE,ID_G ...