题目

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. vue的坑

    1. (vue2.x以上,1.x没有问题)vue和jq一起使用的冲突:在使用了v-bind: class的元素上,当vue和jq都需要增改class时,用jq加的属性可能无效. 原因:当数据的布尔值改 ...

  2. Shiro Realm

    Realm: 在实际应用中,shiro从数据库中获取安全数据(如用户.角色.权限),而不是从ini中,可作为安全数据源 即SecurityManager要验证用户身份,那么它需要从Realm获取相应的 ...

  3. POI操作Excel(二)

    注意:HSSFWorkBook对应2003版Excel      XSSFWorkBook对应2007以上的Excel 一.创建时间单元格 public void helloPoi3() throws ...

  4. C#截取字符串按字节截取SubString

    public static string CutByteString(string str,int len)     {       string result=string.Empty;// 最终返 ...

  5. Apache CLI Demo

    1. Options private Options options = new Options(); 2. option (1) way1 launcher.options.addOption(&q ...

  6. 各平台免费翻译API

    google http://translate.google.cn/translate_a/single?client=gtx&dt=t&dj=1&ie=UTF-8&s ...

  7. es6基础(5)--数值扩展

    { //Number.isFinite数字是有尽的 console.log(Number.isFinite(15));//true console.log(Number.isFinite(NaN)); ...

  8. linux之 redis 的rdb 转 aof 及主从复

    redis持久化RDB基于快照的持久化通过save命令,强制持久化  在redis.conf中dbfilename  dbmp.rdbsave  900 1save 300 10save 60  10 ...

  9. mySQL InnoDB 的性能问题讨论

    https://ncisoft.iteye.com/blog/34676 https://www.douban.com/note/245895324/ MySQL最为人垢病的缺点就是缺乏事务的支持,M ...

  10. 使用DolphinPHP的框架中的excel插件导入数据

    直接上函数吧 public function importfile() { if ($this->request->isPost()) { if($_POST['files']) { Cu ...