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.

    Example:

    Input: "25525511135"
    Output: ["255.255.11.135", "255.255.111.35"]

    AC code:

    class Solution {
    public:
        vector<string> restoreIpAddresses(string s) {
            vector<string> res;
            helper(0, s, res, "", 0);
            return res;
        }
        
        void helper(int count, string s, vector<string>& res, string ret, int index) {
            if (count > 4) return;
            if (count == 4 && index == s.length())
                res.push_back(ret);
            for (int i = 1; i < 4; i++) {
                if (index+i > s.length()) break;
                string temp = s.substr(index, i);
                if(temp[0] == '0' && temp.length() > 1 || atoi(temp.c_str()) > 255) continue;
                string next = ret + temp;
                helper(count+1, s, res, next+(count == 3 ? "" : "."), index+i);
            }
        }
    };
    

    Runtime: 4 ms, faster than 23.68% of C++ online submissions for Restore IP Addresses.

    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    搭建论坛
    修改ecshop页面
    通信失败的原因
    UCenter教程
    discuz论坛 手机登录
    discuz论坛 安装短信插件
    eccshop获取商品详情属性的字段调用值
    php与discuz形成的对接
    php
    php论坛
  • 原文地址:https://www.cnblogs.com/h-hkai/p/9867423.html
Copyright © 2011-2022 走看看