93. Restore IP Addresses(dfs)
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 address的规则:一共四段;每段的值不能超过255;不能以0开头,但可以在一段中只有数字0
class Solution {
public:
vector<string> restoreIpAddresses(string s) {
dfs(s, "", );
return ret;
}
void dfs(string s, string currentIP, int depth){ //depth表示第几个section
if(depth == ){
if(check(s))
ret.push_back(currentIP+s);
return;
}
int len = s.length();
if(len < -depth){ //剩余string长度过短
return;
}
string s1, s2;
//check if we can assign 3 digits in the section
if(len > ){
s1 = s.substr(,);
if(check(s1)){
s2 = s.substr();
dfs(s2,currentIP+s1+".", depth+);
}
}
//check if we can assign 2 digits in the section
if(len > ){
s1 = s.substr(,);
if(check(s1)){
s2 = s.substr();
dfs(s2,currentIP+s1+".", depth+);
}
}
//assign 1 digits in the section
s2 = s.substr();
dfs(s2,currentIP+s[]+".", depth+);
}
bool check(string section){
int len = section.length();
if(len == || len > ) return false;
int value = stoi(section);
if(len==){
if(section[]!='' && value <= ) return true;
else return false;
}
else if(len==){
if(section[]=='') return false;
else return true;
}
return true;
}
private:
vector<string> ret;
};
当然也能用循环,每两个section之间的分割用一个for循环遍历分割的位置,一共是三重for循环。
93. Restore IP Addresses(dfs)的更多相关文章
- leetcode 93. Restore IP Addresses(DFS, 模拟)
题目链接 leetcode 93. Restore IP Addresses 题意 给定一段序列,判断可能组成ip数的所有可能集合 思路 可以采用模拟或者DFS的想法,把总的ip数分成四段,每段判断是 ...
- 93.Restore IP Addresses(M)
93.Restore IP Addresses Medium 617237FavoriteShare Given a string containing only digits, restore it ...
- 93. Restore IP Addresses
题目: Given a string containing only digits, restore it by returning all possible valid IP address com ...
- 【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产生所有可能的ip地址
[抄题]: Given a string containing only digits, restore it by returning all possible valid IP address c ...
- LeetCode OJ 93. Restore IP Addresses
题目 Given a string containing only digits, restore it by returning all possible valid IP address comb ...
- 【LeetCode】93. Restore IP Addresses 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
- LeetCode : 93. Restore IP Addresses
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABZ4AAAHUCAYAAAC6Zj2HAAAMFGlDQ1BJQ0MgUHJvZmlsZQAASImVlw
- leetcode 93 Restore IP Addresses ----- java
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
随机推荐
- I.MX6 eMMC 中启动U-boot存放的首地址
/************************************************************************************ * I.MX6 eMMC 中 ...
- I.MX6 Manufacturing Tool V2 (MFGTool2) Emmc mksdcard-android.sh hacking
#!/bin/bash # 参考文章: # . Shell特殊变量:Shell $, $#, $*, $@, $?, $$和命令行参数 # http://c.biancheng.net/cpp/vie ...
- 分析现有 WPF / Windows Forms 程序能否顺利迁移到 .NET Core 3.0(使用 .NET Core 3.0 Desktop API Analyzer )
今年五月的 Build 大会上,微软说 .NET Core 3.0 将带来 WPF / Windows Forms 这些桌面应用的支持.当然,是通过 Windows 兼容包(Windows Compa ...
- 6-17 Shortest Path [2](25 分)
Write a program to find the weighted shortest distances from any vertex to a given source vertex in ...
- 动态库中的soname
soname( Short for shared object name) 其是应用程序加载dll 时候,其寻找共享库用的文件名.其格式为 lib + math+.so + ( major versi ...
- Git核心概念
Git作为流行的分布式版本管理系统,用好它要理解下面几个核心的概念. 1.Git保寸的是文件完整快照,而不是差异变化或者文件补丁.每次提交若文件有变化则会指向上一个版本的指针而不重复生成副本. Git ...
- [原创]JEECMS 自定义标签调用广告版位下的所有广告(利用广告管理管理首页幻灯片)
JEECMS自带的只有[@cms_advertising]标签,并且官方没有给文档,用法: [@cms_advertising id='3'] <img src=&quo ...
- vim之vundle
git clone https://github.com/gmarik/vundle.git ~/.vim/bundle/vundle,下载到本地 gvim ~/.vimrc set nocompat ...
- NPOI-Excel系列-1002.创建带有Document Summary Information和Summary Information的Excel文件
1. using NPOI.HSSF.UserModel; using NPOI.HPSF; using NPOI.POIFS.FileSystem; using Microsoft.VisualSt ...
- [html][LigerUI]使用示例
<link href="Source/lib/ligerUI/skins/Aqua/css/ligerui-all.css" rel="stylesheet&quo ...