题目

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. 关于dict的formkeys方法注意

    使用容器中的元素生成k, v为统一值, 指向同一个内存地址 默认值指向同一个内存, 修改就全部修改 strvar = 'abcd' listvar = [] dictvar = {} dictvar ...

  2. 【TensorFlow学习笔记 】name_socpe variable_scope

    [引言]TensorFlow中的命名域是非常重要的概念,涉及到参数共享,方便命名参数管理,定义图结构 本文主要介绍name_scope 和 variable_scope,slim包中的arg_scop ...

  3. 什么是Apache Flink

    大数据计算引擎的发展 这几年大数据的飞速发展,出现了很多热门的开源社区,其中著名的有 Hadoop.Storm,以及后来的 Spark,他们都有着各自专注的应用场景.Spark 掀开了内存计算的先河, ...

  4. CF1133E K Balanced Teams(DP)

    /* 排序之后每个点往前能选择的是一段区间, 所以我们实际上转移位置是确定的 然后f[i][j]表示到了i选了j段的最大贡献, 显然状态数是O(n^2)的, 转移是O(1)的 */ #include& ...

  5. 再谈编码 decode和encode

    1. ascii. 有: 数字, 字母, 特殊字符. 8bit 1byte 128 最前面是0 2. gbk. 包含: ascii, 中文(主要), 日文, 韩文, 繁体文字. 16bit, 2byt ...

  6. 微信小程序笔记<二>认识app.json

    *.json文件在小程序开发中必不可少,从 app.json 开始认识小程序中的配置文件*.json: app.json 为小程序必须文件,它不仅作为配置文件管理着小程序的UI还充当着路由器的功能: ...

  7. android 开发 RecyclerView 横排列列表布局

    1.写一个一竖的自定义布局: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xml ...

  8. django交互vue遇到的问题

    接受列表(数组): request.POST.get('array', '') # 结果得到数组的最后一个元素 request.POST.getlist('array', '') # 获取整个列表

  9. windows下mvn verify异常

    [ERROR] Failed to execute goal org.apache.maven.plugins:maven-jarsigner-plugin:1.4:sign (sign) on pr ...

  10. webapi_uploadfile_gdal_to_geojson_and_unzipfile

    using ICSharpCode.SharpZipLib.Zip; using OSGeo.GDAL; using OSGeo.OGR; using System; using System.Col ...