题目

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. KPPW2.7 漏洞利用--文件上传

    KPPW2.7 漏洞利用----文件上传 文件上传导致任意代码执行 搭建环境 1,集成环境简单方便,如wamp,phpstudy.... 2,KPPW v2.7源码一份(文末有分享)放到WWW目录下面 ...

  2. 搭建postgresql集群的问题汇总

    问题一:如何配置pg远程访问          修改postgresql.conf-->listen_addresses = '*'          修改pg_hba.conf-->添加 ...

  3. MongoDB基础命令

    MongoDB 入门命令 查看当前数据库 > show dbs admin 0.000GB config 0.000GB local 0.000GB > -- use databaseNa ...

  4. 在windows server 2012/2016上,任务管理器性能页面增加磁盘监控的办法

    从windows server 2012开始,微软修改了任务管理器的显示方式,图像化看起来更直观了,但是可惜的是,默认情况下,2012和2016均只显示CPU/内存/网络三个资源监视,没有重要的磁盘, ...

  5. python访问hive

    #!/usr/bin/env python # -*- coding: utf-8 -*- # hive util with hive server2 """ @auth ...

  6. CentOS7中替换安装python3.7.0

    python3.7的安装包可从官网下载上传到主机,也可以用wget直接下载. [root@xxx ~]# cd /usr/local/src/[root@xxx src]# wget https:// ...

  7. 转:SQL 操作结果集 -并集、差集、交集、结果集排序

    为了配合测试,特地建了两个表,并且添加了一些测试数据,其中重复记录为东吴的人物. 表:Person_1魏国人物 表:Person_2蜀国人物 A.Union形成并集 Union可以对两个或多个结果集进 ...

  8. FastDFS 配置 Nginx 模块,并实现分布式同步-Linux

    1.搭建虚拟机 a.复制虚拟机文件 首先复制我们之前安装好的fastdfs虚拟机,因为我们现在要设置它的IP为21,改名为CentOS-fastdfs - 21. b.设置网络 生成新的MAC地址 设 ...

  9. mysql安装卸载-windows

     安装:(注意点) 官网download安装包 choose setup type --> custom 安装路径 detailed configuration developer machin ...

  10. python venv actieve uninstall pack-name sitepage