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

一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

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地址

解题过程中需要注意无效ip,诸如00,010等

解题思路:采用回溯法,ip有四级,分别判断每一级是否为有效的,依次递归,到最后判断能不能组成有效ip,没有就回溯到上一级继续递归。详细思路见代码注释

class Solution {
public:
    vector<string> restoreIpAddresses(string s) {
        vector<string> ret;
        string ip;
        dfsRestoreIp(s,ip,0,0,ret);
        return ret;
    }
    void dfsRestoreIp(string& s , string ip , int count , int i ,vector<string>&ret)
    {
        if(count==4)//计数到4后表示一个ip地址
        {
            if(i==s.length()) {
                ip.erase(ip.end()-1);//去除最后的‘.‘
                ret.push_back(ip);
            }
            return;
        }
        int num = 0;
        for(int j = i ; j < s.length() ; j++)
        {
            ip+=s[j];
            num = num*10+(s[j]-‘0‘);
            if(num>0&&s[i]==‘0‘) return;//去除诸如‘01’,‘010’等无效ip
            if(num==0&&j-i>=1) return;//去除诸如‘00‘等无效ip
            if(num<256)
            {
                ip+=‘.‘;//加上.代表到下一级
                dfsRestoreIp(s,ip,count+1,j+1,ret);
                ip.erase(ip.end()-1);//回溯到上一级
            }
            else return;
        }
    }
};
时间: 2024-10-06 11:03:31

【一天一道LeetCode】#93. Restore IP Addresses的相关文章

LeetCode: 93. Restore IP Addresses

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) 给一串数字字符串,返回所有可能的

leetCode 93.Restore IP Addresses (恢复IP地址) 解题思路和方法

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

LeetCode 93. Restore IP Addresses 20170705 部分之前做了没写的题目

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) 题目大意:给出一串数字组成的字符

[leetcode]93. Restore IP Addresses还原IP地址

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"] 题意 给定一个没加点的IP地址,返回所有可能的合法IP地址. 思路:DFS 代码 原文

【LeetCode】93. Restore IP Addresses 【面试题】

Restore IP Addresses 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 mat

【leetcode】Restore IP Addresses

Restore IP Addresses 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 mat

93. Restore IP Addresses

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) 什么时候临时容器符合题意加入结

Java for LeetCode 093 Restore IP Addresses

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) 解题思路: 使用循环即可解决,

[LeetCode]46. Restore IP Addresses复原IP地址

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) Subscribe to se