[LeetCode] Restore IP Address [28]
题目
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地址。
这个题能够理解成,将字符串分割成若干个小块,将当中合法的(数值在0-255)组合起来,拼接成一个合法的IP地址。讲到这也就看出来了这还是一个组合问题。对于组合问题。算法都是结构性的递归套循环,看例如以下代码。
代码实现
class Solution {
public:
vector<string> restoreIpAddresses(string s) {
vector<string> ret;
helper(0, 0, s, string(), ret);
return ret;
}
void helper(int k, int start, const string& s, string part, vector<string> & ret){
if(k>4) return;
if(k==4){
if(start == s.size()){
ret.push_back(part);
}
return;
}
int key=0;
if(k>0)
part += ".";
for(int i=start; i<s.size(); ++i){
if(i>start && s[start] == '0') break;
key = 10*key + s[i]-'0';
if(key > 255) return;
string temp = part;
temp += s.substr(start, i-start+1);
helper(k+1, i+1, s, temp, ret);
}
}
};
另外。我开通了微信公众号--分享技术之美,我会不定期的分享一些我学习的东西.
)
[LeetCode] Restore IP Address [28]的更多相关文章
- LeetCode:Restore IP Address
93. Restore IP Addresses Given a string containing only digits, restore it by returning all possible ...
- [Leetcode] restore ip address 存储IP地址
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- [LeetCode] Validate IP Address 验证IP地址
In this problem, your job to write a function to check whether a input string is a valid IPv4 addres ...
- [LeetCode] Restore IP Addresses 复原IP地址
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- [LintCode] Restore IP Address 复原IP地址
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- [leetcode]Restore IP Addresses @ Python
原题地址:https://oj.leetcode.com/problems/restore-ip-addresses/ 题意: Given a string containing only digit ...
- LeetCode——Restore IP Addresses
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- LeetCode: Restore IP Addresses 解题报告
Restore IP Addresses My Submissions Question Solution Given a string containing only digits, restore ...
- [LeetCode] Restore IP Addresses 回溯
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
随机推荐
- 杭电oj2072
因为一直不能ac先发这里,希望有看到的大佬能指点一二. 先讲一下我的基本思路,首先将一整行数据保存在数组中,接着遍历数组,根据空格将每个单词存入二维数组中,最后遍历二维数组,找出其中不同的单词并计数. ...
- (十)stm32 GPIO口复用,重映射 RCC_APB2Periph_AFIO
什么时候需要用到RCC_APB2Periph_AFIO--复用IO时钟的使用 需要用到外设的重映射功能时才需要使能AFIO的时钟 外部中断(EXTI)中与AFIO有关的寄存器是AFIO-EXTICR1 ...
- es6记录
3.5? 一.const 1.冻结对象 const foo = Object.freeze({}); // 常规模式时,下面一行不起作用: // 严格模式时,该行会报错 foo.prop = ; 2. ...
- MVC中@Html.Action的用法(类似自定义控件)
MVC项目中如果有公共部分的代码就可以单独拿出来作为控件来用(比如头部和底部代码).跟ASP.NET中的ASCX实现的效果一样,但MVC比它方便的多. 一.@Html.Action的用法 @Html. ...
- [BZOJ1294][SCOI2009]围豆豆Bean 射线法+状压dp+spfa
1294: [SCOI2009]围豆豆Bean Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 458 Solved: 305[Submit][Sta ...
- AC日记——Little Elephant and Array codeforces 221d
221D - Little Elephant and Array 思路: 莫队: 代码: #include <cmath> #include <cstdio> #include ...
- Uva 12063 Zero and Ones
给个链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- python3 全栈开发 -- 面向对象 类的组合和封装
一.类的组合 1.什么是组合 组合:描述的是类与类之间的关系,是一种什么有什么关系 一个类产生的对象,该对象拥有一个属性,这个属性的值是来自于另外一个类的对象 2.什么是继承(回顾一下) 继承:描述的 ...
- JDBC_PreparedStatement 防sql注入
package songyan.jdbc.login.prepared; import java.sql.Connection; import java.sql.DriverManager; impo ...
- 用kermit通过串口往nandflash任意地址里烧写任何文件!
1.安装kermit #apt-get install ckermit 2.使用kermit之前,在用户宿主目录下(/home/用户名/)创建一个名为.kermrc的配置文件,内容如下 : set l ...