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 ...
随机推荐
- contiki-事件调度
事件驱动机制广泛应用于嵌入式系统,类似于中断机制,当有事件到来时(比如按键.数据到达),系统响应并处理该事件.相对于轮询机制,事件机制优势很明星,低功耗(系统处于休眠状态,当有事件到达时才被唤醒)和M ...
- 整合TabBarController与NavigationController
一.项目结构 一开始创建一个空的项目
- 强大的打印功能jatoolsPrinter使用总结
最近功能做项目,需要实现打印条码标签的功能,对于第一次接触打印机的小白来说简直是折磨死我拉,公司采购的打印机是斑马的GK888T,其实,如果单纯的想实现能打印出来标签的话,直接用window.prin ...
- ios cordova报gap://ready 弹出框,一直弹
The iOS app was not working because there was a plugin missing: https://github.com/apache/cordova-pl ...
- 通过form表单获取值
<!DOCTYPE html><html> <head> <meta charset="utf-8" /> <title> ...
- Mac终端下打开sublime
我安装的sublime 2终端输入如下命令 alias subl=\''/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl ...
- C++回溯法走迷宫
#include <iostream> #include <iomanip> #include <cstdlib> using namespace std; #de ...
- 一个靠谱的国外maven镜像地址
<mirror> <id>ui</id> <mirrorOf>central</mirrorOf> <name>Human Re ...
- ORACLE中的支持正则表达式的函数
ORACLE中的支持正则表达式的函数主要有下面四个:1,REGEXP_LIKE :与LIKE的功能相似2,REGEXP_INSTR :与INSTR的功能相似3,REGEXP_SUBSTR :与SUBS ...
- IE9,10中console对象的bug
首先上一段很简单的代码 <!DOCTYPE html> <html> <head> <title></title> <meta htt ...