LeetCode Restore IP Addresses
DFS
class Solution {
public:
vector<string> restoreIpAddresses(string s)
{
return insertDot(s, 0, 3);
} vector<string> insertDot(string s, int beginIndex, int countOfDot /*3, 2, 1, 0*/)
{
vector<string> result;
if( (s.size() - beginIndex) < (countOfDot + 1) || (s.size() - beginIndex - 1) > (countOfDot +1) * 3)
return result; if(countOfDot == 0)
{
string partition = s.substr(beginIndex);
if(partition.size() > 1 && partition[0] == '0') //Error 4: if the first char is 0, then no more chars, such as 1.00.1.1 is no valid;
{
return result;
}
int val = std::stoi(partition);
if(val >= 0 && val <256)
{
result.push_back(partition);
}
return result;
} for(int i = beginIndex + 1; i< s.size();i++ ) //Error 1: i< s.size() -1
{
string partition = s.substr(beginIndex, i - beginIndex);
if(partition.size() > 1 && partition[0] == '0') //Error 3: if the first char is 0, then no more chars, such as 1.00.1.1 is no valid;
{
break;
}
int val = std::stoi(partition);
if(val > 255)
break;
if(val >= 0 && val <256)
{
vector<string> subresult = insertDot(s, i, countOfDot - 1);
if(subresult.size() != 0)
{
for(int j = 0; j< subresult.size(); j++) //Error 2: j< s.size() -1
{
string onepartition = partition + "." + subresult[j];
result.push_back(onepartition);
}
}
}
}
return result;
}
};
LeetCode Restore IP Addresses的更多相关文章
- 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 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 ...
随机推荐
- mysql:SQL语句操作数据库中表和字段的COMMENT值
转载:http://blog.163.com/inflexible_simple/blog/static/167694684201182601221758/ 参考文档不太给力啊,表注释和字段注释的资料 ...
- java打印Jni层log
在eclipse上新建jni工程可以参考:http://www.cnblogs.com/ashitaka/p/5953708.html 要在java层打印c的log必须引入这个头文件的宏定义: #if ...
- 发生tcp丢包(拥堵、超时)重传
可以根据wireshark的Seq序列号和Ack序列号来进行详细分析. 可见,网络丢包(可能是网络拥堵.也有可能是骨干网上有"防火墙"故意随机丢包,因为这个服务器的IP放在国外)对 ...
- IntelliJ IDEA中Maven项目的默认JDK版本
在IntelliJ IDEA 15中使用Maven时,IDEA将默认的编译版本.源码版本设置为jdk5.编译项目的时候出现警告:"Warning:Java: 源值1.5已过时, 将在未来所有 ...
- Android和iOS常用命令学习(真机)
1. 安装应用: Android: adb install xxx.apk iOS: ideviceinstaller -i xxx.ipa 2. 卸载应用 Android: abd uninstal ...
- Java_I/O输入输出_使用输入输出流读取文件,将一段文字加密后存入文件,然后读取,将加密前与后的文件输出
import java.io.*; public class Example { public static void main(String[] args) { char a[] = "今 ...
- win32 Dll 中添加afx.h 出现如下错误 error LNK2005: _DllMain@12 already defined
win32 Dll 中添加afx.h 出现如下错误 nafxcwd.lib(dllmodul.obj) : error LNK2005: _DllMain@12 already defined in ...
- 大家把做的公祭日的ps上传哦
上传时图片保存为JPG,写上自己的学号,说说自己的创作构思
- Jfreechart初案例--饼图
1.action @Controller(value = "pieAction") @Scope("prototype") public class PieAc ...
- ArcMap Add-in插件开发中解决VS调试时断点不会命中的问题
在VS2010中进行ArcMap Add-in插件开发(ArcEngine10.1,ArcGIS10.1),运行时为.NET4.0,在程序中设置了断点进行调试,但是运行后程序并不会在断点处停止,且原来 ...