leetcode-Restore IP Addresses-ZZ
http://www.cnblogs.com/remlostime/archive/2012/11/14/2770072.html
class Solution {
private:
vector<string> ret;
int pos[];
public:
bool check(string &s, int beg, int end)
{
string ip(s, beg, end - beg + );
if (ip.size() == )
return "" <= ip && ip <= "";
else if (ip.size() == )
return "" <= ip && ip <= "";
else if (ip.size() == )
return "" <= ip && ip <= "";
else
return false;
}
void dfs(int dep, int maxDep, string &s, int start)
{
if (dep == maxDep)
{
if (start == s.size())
{
int beg = ;
string addr;
for(int i = ; i < maxDep; i++)
{
string ip(s, beg, pos[i] - beg + );
beg = pos[i] + ;
addr += i == ? ip : "." + ip;
}
ret.push_back(addr);
}
return;
}
for(int i = start; i < s.size(); i++)
if (check(s, start, i))
{
pos[dep] = i;
dfs(dep + , maxDep, s, i + );
}
}
vector<string> restoreIpAddresses(string s) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
ret.clear();
dfs(, , s, );
return ret;
}
};
http://yucoding.blogspot.com/2013/04/leetcode-question-83-restore-ip.html
Restore IP Addresses:
Given
"25525511135",["255.255.11.135", "255.255.111.35"]. (Order does not matter)Analysis:
This problem can be viewed as a DP problem. There needed 3 dots to divide the string, and make sure the IP address is valid: less than or equal to 255, greater or equal to 0, and note that, "0X" or "00X" is not valid.
For the DP, the length of each part is from 1 to 3. We use a vector<string> to store each part, and cut the string every time. Details see the code.
Note that "atoi" is for c-string, <string> need to convert to cstring by str.c_str();
Code(Updated 201309):
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
class Solution {public: bool valid(string s){ if (s.size()==3 && (atoi(s.c_str())>255 || atoi(s.c_str())==0)){return false;} if (s.size()==3 && s[0]=='0'){return false;} if (s.size()==2 && atoi(s.c_str())==0){return false;} if (s.size()==2 && s[0]=='0'){return false;} return true; } void getRes(string s, string r, vector<string> &res, int k){ if (k==0){ if (s.empty()){res.push_back(r);} return; }else{ for (int i=1;i<=3;i++){ if (s.size()>=i && valid(s.substr(0,i))){ if (k==1){getRes(s.substr(i),r+s.substr(0,i),res,k-1);} else{getRes(s.substr(i),r+s.substr(0,i)+".",res,k-1);} } } } } vector<string> restoreIpAddresses(string s) { // Start typing your C/C++ solution below // DO NOT write int main() function vector<string> res; getRes(s,"",res,4); return res; }}; |
Code(old version):
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
class Solution {public: void dp(string s,vector<string> &cur ,vector<string> &res){ if (cur.size()==3){ // if there are 4 parts in the original string cur.push_back(s); //all 4 parts and check if valid bool r = true; for (int i=0;i<4;i++){ if (atoi(cur[i].c_str())>255){ //check value r = false; break; } if ((cur[i].size()>1 && cur[i][0]=='0')){ //check "0X" "00X" and "0XX" cases r =false; break; } } if (r){ res.push_back(cur[0]+"."+cur[1]+"."+cur[2]+"."+cur[3]); } cur.pop_back(); }else{ for (int i=0;i<3;i++){ if (s.size()>i+1){ cur.push_back(s.substr(0,i+1)); dp(s.substr(i+1,(s.size()-i-1)),cur,res); cur.pop_back(); } } } } vector<string> restoreIpAddresses(string s) { // Start typing your C/C++ solution below // DO NOT write int main() function vector<string> res,cur; if (s.size()>12 || s.size()<4 ){return res;} dp(s,cur,res); // cur stores the current separation return res; }};
|
leetcode-Restore IP Addresses-ZZ的更多相关文章
- LeetCode: Restore IP Addresses 解题报告
Restore IP Addresses My Submissions Question Solution Given a string containing only digits, restore ...
- [LeetCode] Restore IP Addresses 复原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 回溯
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- LeetCode Restore IP Addresses
DFS class Solution { public: vector<string> restoreIpAddresses(string s) { return insertDot(s, ...
- leetcode 93. Restore IP Addresses(DFS, 模拟)
题目链接 leetcode 93. Restore IP Addresses 题意 给定一段序列,判断可能组成ip数的所有可能集合 思路 可以采用模拟或者DFS的想法,把总的ip数分成四段,每段判断是 ...
- 【leetcode】Restore IP Addresses
Restore IP Addresses Given a string containing only digits, restore it by returning all possible val ...
- 【LeetCode】93. Restore IP Addresses
Restore IP Addresses Given a string containing only digits, restore it by returning all possible val ...
- LeetCode解题报告—— Reverse Linked List II & Restore IP Addresses & Unique Binary Search Trees II
1. Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass ...
随机推荐
- UNIX SHELL基础知识总结(一)
1. Unix常目录结构与作用: 2. 基本命令: $echo $date $who $who am i 3. 创建文件的几种方式: A. touch FileName 创建空文件 B. > ...
- CSAPP阅读笔记-栈帧-来自第三章3.7的笔记-P164-P176
1.基本结构: 如上图所示,是通用的栈帧结构.大致分两块,调用者函数P和被调用者函数Q. 对P来说,要做的工作是把传递参数中多于6个的部分压栈,随后把Q返回时要执行的下一条指令的地址压栈. 对Q来说, ...
- Testlink 机器重启后Access denied for user 'admin '@'localhost' (using password: YES)解决
问题表现: 装完Testlink,重启系统后,在testlink权限未分配会出现如下提示: 1045 - Access denied for user 'Testlink '@'localhost' ...
- (转)Mysql数据库管理 表的维护
原文:http://t.dbdao.com/archives/mysql%E6%95%B0%E6%8D%AE%E5%BA%93%E7%AE%A1%E7%90%86-%E8%A1%A8%E7%9A%84 ...
- Netty构建Http服务器
Netty 是一个基于 JAVA NIO 类库的异步通信框架,它的架构特点是:异步非阻塞.基于事件驱动.高性能.高可靠性和高可定制性.换句话说,Netty是一个NIO框架,使用它可以简单快速地开发网络 ...
- Python开发利器之UliPad
一.安装Ulipad 因为ulipad编辑器使用的是wxpython编写的gui,所以我们需要第三方库wxpython的支持,先讲一下Ulipad在Windows系统环境下的安装: 1. 确实自己的w ...
- IDEA里运行代码时出现Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger的解决办法(图文详解)
不多说,直接上干货! 问题详情 运行出现log4j的问题 -classpath "C:\Program Files\Java\jdk1.8.0_66\jre\lib\charsets.jar ...
- Xcode插件路径、缓存路径、图片压缩工具路径、代码片段路径、symbolicatecrash路径
Xcode插件路径 ~/Library/Application\ Support/Developer/Shared/Xcode/Plug-ins Xcode缓存路径 ~/Library/Devel ...
- Java入门系列-18-抽象类和接口
抽象类 在第16节继承中,有父类 People People people=new People(); people.sayHi(); 实例化People是没有意义的,因为"人"是 ...
- OAuth2.0 微信授权机制
我在了解设计Restful接口的时候,发现涉及到接口验证,可以利用OAuth2.0机制来验证. 我开发的微信端Web网页通过微信授权的时候,微信端也是用OAuth2.0机制来获取用户基本信息. OAu ...