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 ...
随机推荐
- 手机端页面调试工具-vconsole使用
用的是VUE-CLI3第一步.安装vconsole npm install vconsole 第二步.创建js文件并写入内容 import Vconsole from 'vconsole' let v ...
- mojing SDK根据坐标进行移动
using UnityEngine; using UnityEngine.UI; using System.Collections; public class transforms : MonoBeh ...
- Centos 7.0设置/etc/rc.local无效问题解决
安装centos7以后按照以往习惯修改rc.local添加开机启动命令,但重启后发现无效,再次重启发现依然如故 检查系统rc.local服务运行情况 systemctl | grep "rc ...
- 我的Python升级打怪之路【六】:面向对象(一)
面向对象的概述 面向过程:根据业务逻辑从上到下写代码 函数式:将其功能代码封装到函数中,日后便无需编写,仅仅调用即可 [执行函数] 面向对象:对函数进行分类和封装.[创建对象]==>[通过对象执 ...
- Python爬取电影天堂指定电视剧或者电影
1.分析搜索请求 一位高人曾经说过,想爬取数据,要先分析网站 今天我们爬取电影天堂,有好看的美剧我在上面都能找到,算是很全了. 这个网站的广告出奇的多,用过都知道,点一下搜索就会弹出个窗口,伴随着滑稽 ...
- webpack+react
一直提醒我这个.闹心最后发现是在不同的js 里引入组件的时候 import React from 'react'; 和 import React from 'React'; 就是大小写的问题. 解决办 ...
- WPF基础控件
1.所有基础控件结束(英文) http://www.dotnetperls.com/tooltip-wpf 2.wpf- DataGrid 常用属性和事件 3.
- 西数常用TREX命令
西数常用TREX命令 trex命令:dut1 简便找盘idp或info did查看硬盘信息chkresfall检测固件smart 查看SMART表clrsmart 清SMART表svmod 0x.. ...
- Gradle 学习(一)
引入插件 apply plugin: 'java' apply plugin: 'war' apply plugin: 'jetty' 如果希望使用jar来启动项目, 可以这样修改项目和插件属性. a ...
- Javaee的Dao层的抽取
有时候我们在实现不同功能的时候回看到很多的Dao层的增加.修改.删除.查找都很相似,修改我们将他们提取BaseDao 一.提取前 1. 提取前的LinkDao层: public interface L ...