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 ...
随机推荐
- gerrit 解决中文乱码相关配置(转载)
From:http://www.cnblogs.com/Jerryshome/archive/2012/04/19/2457170.html 计划在团队中采用code review,因为一直是用git ...
- 直接解压msi文件
msiexec /a "F:\TDDownload\subversion-1.5.5.msi" /qb TARGETDIR="F:\TDDownload\subversi ...
- 使用Atlas实现MySQL读写分离
1.MySQL所在机器 192.168.29.128(Master) 192.168.29.129(Slave) 配置好主从同步,参考 http://www.cnblogs.com/luxh/p/40 ...
- ios系统的中arm指令集
arm结构处理器,几乎所有的手机都基于arm,其在嵌入式系统中应用非常广泛. ARM 处理器因为低功耗和小尺寸而闻名,它的性能在同等功耗的产品中也很出色.这里我们注意一点,模拟器并不运行arm代码,软 ...
- 66. Regular Expression Matching
Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' ...
- app测试与web测试的区别
1.从功能测试的来讲的话,在流程和功能测试上是没有区别的.系统测试和一些细节可能会不一样. 那么我们就要先来了解,web和app的区别. web项目,一般都是b/s架构,基于浏览器的,而app则是c/ ...
- noip2007解题报告
T1.统计数字 给出n个数,统计每个数字出现的个数. n小,快排解决. T2.字符串的展开 给出一个字符串,其中形如 d-h,4-9之类的就展开,(前面比后面小的保留,相等也是),三个参数,P1表示大 ...
- 2016-07-15: Window定时器使用
windows下定时器使用实例 #include <iostream> #include <Windows.h> using namespace std; void Timer ...
- IO同步、异步与阻塞、非阻塞
一.同步与异步同步/异步, 它们是消息的通知机制 1. 概念解释A. 同步所谓同步,就是在发出一个功能调用时,在没有得到结果之前,该调用就不返回. 按照这个定义,其实绝大多数函数都是同步调用(例如si ...
- 1.No MBR错误
如果提示如下错误: Error: No MBR is found at SD/MMC. Hint: use f ...