题目

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

Example:

Input: "25525511135"
Output: ["255.255.11.135", "255.255.111.35"]

解答

一开始看还挺难的,想了想发现其实就是DFS。索的每一步就相当于从剩下的字符串中截取一段,需要确保截取的这一段合法(不大于255,同时除了0之外首位不能为0)。剪枝有两种情况,一种是已经截取了4次但字符串还没截取完,另一种是截取不到4次就已经截取完了。当截取4次且字符串已经截取完,就得到了一个解。

下面是AC的代码:

class Solution {
public:
    vector<string> ans;
    vector<string> temp;
    void cut(string s){
        int length = s.size();
        int seg = temp.size();
        if(seg == 4 && length == 0){
            string temps;
            for(vector<string>::iterator iter = temp.begin(); iter != temp.end(); iter++){
                if(iter != temp.begin()){
                    temps += ".";
                }
                temps += *iter;
            }
            ans.push_back(temps);
        }
        else if(seg == 4 && length != 0){
            ;
        }
        else if(seg < 4 && length == 0){
            ;
        }
        else{
            for(int i = 1; i < 4 && i < length + 1; i++){
                if(i != 1 && s[0] == '0'){
                    continue;
                }
                if(atoi(s.substr(0, i).c_str()) < 256){
                    temp.push_back(s.substr(0, i));
                    cut(s.substr(i, length - i));
                    temp.pop_back();
                }
            }
        }
    }
    vector<string> restoreIpAddresses(string s) {
        cut(s);
        return ans;
    }
};

121
四个月❤️

LeetCode OJ 93. Restore IP Addresses的更多相关文章

  1. 【LeetCode】93. Restore IP Addresses

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

  2. 【一天一道LeetCode】#93. Restore IP Addresses

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

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

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

  4. LeetCode OJ:Restore IP Addresses(存储IP地址)

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

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

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

  6. 93.Restore IP Addresses(M)

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

  7. 93. Restore IP Addresses

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

  8. [LeetCode] 93. Restore IP Addresses 复原IP地址

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

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

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

随机推荐

  1. zabbix 3.4新功能值解析——Preprocessing预处理

    Zabbix 3.4版本更新了许多新功能,其中一个监控项功能Preprocessing,根据官方说明文档,在监控项收集的数据存储到数据库前,预先对数据进行处理,使用效果超过预期.这个功能存放位置在创建 ...

  2. dict函数

    增 fromkeys(iterable, value) 用可迭代对象生成键,创建默认值相同的字典(value默认None) 删 pop(k) 通过k来删除字典元素, 找不到就会报错, 返回被删除字典元 ...

  3. Jquery的框架解析

    最近闲的刁痛,想看看jQuery源码.但是这个源码看起来 还是挺费劲的.所以呢整理一份框架出来, 避免走入jQuery关键字的误区,我用Gys代替关键字jQuery. 下面是源码: (function ...

  4. python3基础操作

    ubuntu下python连接mysql apt-get install python-mysqldb 获取当前时间 >>> from datetime import datetim ...

  5. Python利用脚本2.x到3自动转换

    本文介绍一下在windows 10 环境下如何使用这个工具: 1)首先要先安装好python3,可到官网下载https://www.python.org/ 2)使用Windows 命令提示符(cmd) ...

  6. Java 5- Java 修饰符

    Java 修饰符 Java语言提供了很多修饰符,主要分为以下两类: 访问修饰符 非访问修饰符 修饰符用来定义类.方法或者变量,通常放在语句的最前端.我们通过下面的例子来说明: public class ...

  7. CS229 2.深入梯度下降(Gradient Descent)算法

    1 问题的引出 对于上篇中讲到的线性回归,先化一个为一个特征θ1,θ0为偏置项,最后列出的误差函数如下图所示: 手动求解 目标是优化J(θ1),得到其最小化,下图中的×为y(i),下面给出TrainS ...

  8. fastle

    昨晚梦见日本和中国打仗, 发过来了很多导弹, 但是飞行速度很慢, 我还能看到上面的辐射三角号 之后我就趴在地上躲导弹 然后感觉身体被蒸发, 意识逐渐模糊, 就醒了 attack大爷的休闲(修仙)题 感 ...

  9. 3-安装hive

    1.解压.修改权限 tar -zvxf apache-hive-1.2.1-bin.tar.gz -C /opt/app/ sudo chown -R hadoop:hadoop /opt/app/a ...

  10. python学习之----爬取图片

    import os from urllib.request import urlretrieve from urllib.request import urlopen from bs4 import ...