zoukankan      html  css  js  c++  java
  • 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)

    class Solution {
    public:
        vector<string> restoreIpAddresses(string s) {
            int l = s.length(), c, t, ls, idx, i;
            vector<string> ans;
            if(l < 4 || l > 12)
                return ans;
            string str;
            queue<string> q;
            q.push(str);
            queue<int> pntq; //number of points
            pntq.push(0);
            while(!q.empty())
            {
                str = q.front();
                q.pop();
                c = pntq.front();
                pntq.pop();
                ls = str.length();
                for(i = 0, t = 0; i < 3; i++)
                {
                    idx = ls - c + i;
                    t = t * 10 + s[idx] - '0';
                    if(t > 255)
                        break;
                    if(l-idx-1 >= 3-c && l-idx-1 <= 3*(3-c))
                    {
                        string ts = str + s.substr(ls-c, i+1);
                        if(c < 3)
                        {
                            ts += ".";
                            q.push(ts);
                            pntq.push(c+1);
                        }
                        else
                        {
                            ans.push_back(ts);
                        }
                    }
                    if(0 == i && '0' == s[idx]) //注意不能出现010之类以0开头的子串
                        break;
                }
            }
            return ans;
        }
    };
    注意不能出现010之类以0开头的子串
  • 相关阅读:
    00-深入理解C#读书笔记说明
    00-翻译IdentityServer4的目的
    IdentityServer4-介绍大纲(译文)
    (CLR-Via-C#) 类型基础
    委托
    字符串匹配算法
    weight_decay(权重衰减)
    文本主题模型--LDA
    词义消歧
    模型融合的结合策略
  • 原文地址:https://www.cnblogs.com/argenbarbie/p/5359285.html
Copyright © 2011-2022 走看看