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的更多相关文章

  1. LeetCode: Restore IP Addresses 解题报告

    Restore IP Addresses My Submissions Question Solution Given a string containing only digits, restore ...

  2. [LeetCode] Restore IP Addresses 复原IP地址

    Given a string containing only digits, restore it by returning all possible valid IP address combina ...

  3. [leetcode]Restore IP Addresses @ Python

    原题地址:https://oj.leetcode.com/problems/restore-ip-addresses/ 题意: Given a string containing only digit ...

  4. LeetCode——Restore IP Addresses

    Given a string containing only digits, restore it by returning all possible valid IP address combina ...

  5. [LeetCode] Restore IP Addresses 回溯

    Given a string containing only digits, restore it by returning all possible valid IP address combina ...

  6. leetcode 93. Restore IP Addresses(DFS, 模拟)

    题目链接 leetcode 93. Restore IP Addresses 题意 给定一段序列,判断可能组成ip数的所有可能集合 思路 可以采用模拟或者DFS的想法,把总的ip数分成四段,每段判断是 ...

  7. 【leetcode】Restore IP Addresses

    Restore IP Addresses Given a string containing only digits, restore it by returning all possible val ...

  8. 【LeetCode】93. Restore IP Addresses

    Restore IP Addresses Given a string containing only digits, restore it by returning all possible val ...

  9. 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 ...

随机推荐

  1. CSS+Javascript

    今天做了一个简单的CSS和Javascript的调用,发现CSS和Javascript系统的来写还真是蛮方便的. 1.先建一个CSS文件和一个JS文件 2.在jsp中调用 <link type= ...

  2. Swift函数的定义

    //: Playground - noun: a place where people can play import Cocoa //基本的函数 //************************ ...

  3. Scrapy爬虫导图(持续完善中)

    Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架. 可以应用在包括数据挖掘,信息处理或存储历史数据等一系列的程序中.

  4. cmd 常用指令

    d: 进入d盘 cd xxx 进入某个目录

  5. 每个部门绩效成绩第二名 sql server 查询 ( 替代 not in )

    原题: 集团中有多个部门,部门底下有多个员工,求每个部门绩效分数排名第二的人员,数据表结构如下: DEPAR          NAME             SCORE A             ...

  6. vue新建项目

    一直都被如何用vue.js新建一个项目的问题困扰着,经过好久的实践,终于搞清楚如何用vue新建项目了: 1.官网对于vue-cli介绍: Vue.js provides an official CLI ...

  7. Linux小知识积累

    1.Linux图形界面和字符命令行界面的切换 从图形界面切换到字符界面,使用快捷键 Ctrl+Alt+F1 从字符界面切换到图形界面,使用快捷键 Ctrl+Alt+F7 2.解压文件 tar -xzv ...

  8. FastReport4.6 组件安装

    要完整版不是官方版的试用版.下面包括有的文件 安装前请册除原有的FR控件. 1. "Tools|Environmet options..."中的"Library" ...

  9. C# 进程和线程

    一.进程和线程 进程是对一段静态指令序列的动态执行过程,是系统进行资源分配和调度的基本单位.与进程相关的信息包括进程的用户标志.正在执行的已经编译好的程序.程序和数据在存储器中的位置等.同一个进程有可 ...

  10. Linux 动态链接库

    如何使用动态链接库 Linux下打开使用动态链接库需要三步(实际上和windows下基本一样):1.加载动态链接库,通过调用库函数dlopen()获得链接库的句柄,对应于windows下的 AfxLo ...