数字字符串转化成IP地址 思维

数字字符串转化成IP地址

限定语言:Kotlin、Typescript、Python、C++、Groovy、Rust、Java、Go、Scala、Javascript、Ruby、Swift、Php、Python 3

现在有一个只包含数字的字符串,将该字符串转化成IP地址的形式,返回所有可能的情况。

例如:

给出的字符串为"25525522135",

返回["255.255.22.135", "255.255.221.35"]. (顺序没有关系)

数据范围:字符串长度 

要求:空间复杂度 ,时间复杂度 

"25525522135"

输出

["255.255.22.135","255.255.221.35"]

示例2

输入

"1111"

输出

["1.1.1.1"]

示例3

输入

"000256"

输出

"[]"

注意:ip地址是由四段数字组成的数字序列,格式如 "x.x.x.x",其中 x 的范围应当是 [0,255]。

算法题解——将字符串转化为ip地址
题目描述
现在有一个只包含数字的字符串,将该字符串转化成IP地址的形式,返回所有可能的情况。
例如:
给出的字符串为"25525511135",
返回[“255.255.11.135”, “255.255.111.35”]. (顺序没有关系)

ip地址限制条件:

ip地址一共包含4段,每段用 '.'分隔
每段如果有两位及以上,则首位不能为0,如01,02
每段不能大于255
解题思路
使用深度优先搜索DFS,附加以上限制条件,并且要确保字符串都被搜索到。
代码如下:
 

class Solution {
public:
    /**
     * 
     * @param s string字符串 
     * @return string字符串vector
     */
    vector<string> restoreIpAddresses(string s) {
        // write code here
        if(s.size()>12 || s.empty())
            return {};    
        vector<string> res;
        vector<string> temp;
        dfs(s, res, temp);
        return res;
    }
    
    string convert(vector<string> temp)
    {
        string s;
        for(int i=0;i<temp.size()-1;i++)    //最后一段不加 '.'
        {
            s+=temp[i]+'.';
        }
        s+=temp[temp.size()-1];
        return s;
    }
    
    void dfs(string s,vector<string> &res,vector<string> &temp)
    {
        if(s.empty() && temp.size()==4)
            res.push_back(convert(temp));
        for(int n=1;n<=s.size();n++)    //这里的n不是索引,是子字符串的长度
        {
            if(s[0] == '0' && n>1)    //如果有两位及以上每段的首位不能为0
                return;
            int val = stoi(s.substr(0,n));
            if(val > 255)    //ip地址每段不能大于255
                return;
            temp.push_back(s.substr(0,n));    //符合条件插入候选数组
            dfs(s.substr(n),res,temp);     //递归搜素下一段
            temp.pop_back();    //清除temp,确保下一组ip不受影响;
        }
    }
};

class Solution {
public:
    /**
     * 
     * @param s string字符串 
     * @return string字符串vector
     */
    vector<string> restoreIpAddresses(string s) {
        // write code here
        if(s.size()>12 || s.empty())
            return {};    
        vector<string> res;
        vector<string> temp;
        dfs(s, res, temp);
        return res;
    }
    
    string convert(vector<string> temp)
    {
        string s;
        for(int i=0;i<temp.size()-1;i++)    //最后一段不加 '.'
        {
            s+=temp[i]+'.';
        }
        s+=temp[temp.size()-1];
        return s;
    }
    
    void dfs(string s,vector<string> &res,vector<string> &temp)
    {
        if(s.empty() && temp.size()==4)
            res.push_back(convert(temp));
        for(int n=1;n<=s.size();n++)    //这里的n不是索引,是子字符串的长度
        {
            if(s[0] == '0' && n>1)    //如果有两位及以上每段的首位不能为0
                return;
            int val = stoi(s.substr(0,n));
            if(val > 255)    //ip地址每段不能大于255
                return;
            temp.push_back(s.substr(0,n));    //符合条件插入候选数组
            dfs(s.substr(n),res,temp);     //递归搜素下一段
            temp.pop_back();    //清除temp,确保下一组ip不受影响;
        }
    }
};
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HehuaTang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值