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

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

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

  2. 93.Restore IP Addresses(M)

    93.Restore IP Addresses Medium 617237FavoriteShare Given a string containing only digits, restore it ...

  3. 93. Restore IP Addresses

    题目: Given a string containing only digits, restore it by returning all possible valid IP address com ...

  4. 【LeetCode】93. Restore IP Addresses

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

  5. 93. Restore IP Addresses产生所有可能的ip地址

    [抄题]: Given a string containing only digits, restore it by returning all possible valid IP address c ...

  6. LeetCode OJ 93. Restore IP Addresses

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

  7. 【LeetCode】93. Restore IP Addresses 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...

  8. LeetCode : 93. Restore IP Addresses

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABZ4AAAHUCAYAAAC6Zj2HAAAMFGlDQ1BJQ0MgUHJvZmlsZQAASImVlw

  9. leetcode 93 Restore IP Addresses ----- java

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

随机推荐

  1. 本地Jmeter脚本部署在Jenkins上 - Windows

    一.下载并安装Jenkins(不进行特别的说明) 二.准备好jmeter脚本 三.插件准备:Publish HTML reports 四.开始 1.登录Jenkins后,点击新建任务 2.输入项目名, ...

  2. Linux udhcp client (udhcpc) get IP at anytime

    /*************************************************************************************** * Linux udh ...

  3. RabbitMQ引入

    引入MQ话题 可能很多人有疑惑:MQ到底是什么?哪些场景下要使用MQ? 前段时间安装了RabbitMQ,现在就记录下自己的学习心得吧.首先看段程序: class Program { static vo ...

  4. purescript 基本试用

    安装环境 安装预编译文件 https://github.com/purescript/purescript/releases 配置环境变量: export PATH=$PATH:/Users/dalo ...

  5. graphql-yoga 项目简单使用&&集成docker

      graphql-yoga 是一个实现了grahql的框架,使用简单,便捷 具体源码参考github https://github.com/rongfengliang/graphql-yoga-do ...

  6. Python DB

    #!/usr/bin/python #_*_ coding:utf-8 _*_ import MySQLdb import time import threading import random fr ...

  7. Bootstrap-CL:超大屏幕

    ylbtech-Bootstrap-CL:超大屏幕 1.返回顶部 1. Bootstrap 超大屏幕(Jumbotron) 本章将讲解 Bootstrap 支持的另一个特性,超大屏幕(Jumbotro ...

  8. postman-1版本区别、选择

    postman基于乙醇在腾讯课堂的postman教程 postman特点: 1.便于开发:开发接口的时候需要快速的调用接口,以便调试 2.便于测试:测试的时候需要非常方便的调用接口,通过不同的参数去测 ...

  9. php ini_set更改php.ini配置,通过它修改php.in达到php上传文件大小限制是不行的,除非修改.htaccess文件

    PHP ini_set() 无效的原因:如题:我租的是虚拟主机,php.ini里的upload_max_filesize是默认的2M,要直接修改php.ini文件是不可能的.我如果想上传超过2M的怎么 ...

  10. H.265:高清视频的最佳选择

    H.265技术经过2年发展,我们认为H.265高清监控产品市场化才真正开始.那么H.265的到来,大家又对该技术真正了解多少呢?下面就让我们一起来了解该技术.... HEVC/H.265标准于2013 ...