zoukankan      html  css  js  c++  java
  • leetcode

    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:
        std::vector<std::string> restoreIpAddresses(std::string s) {
            std::vector<std::string> res;
    		if(s.size() < 4 || s.size() > 12) return res;
    		dfs(s,1,"",res);
    #if 1
    		for (int i = 0; i < res.size(); i++)
    		{
    			std::cout << res[i] << std::endl;
    		}
    #endif // 1
    
    		return res;
        }
    private:
    	void dfs(std::string s,int n, std::string ip,std::vector<std::string> &res)
    	{
    		if(s.size() == 0) return;
    		if(n == 4 && isValid(s))
    		{
    			ip += s;
    			res.push_back(ip);
    			return;
    		}
    		for (int i = 1; i <= 3; i++)
    		{
    			if(i > s.size()) break;
    			std::string t = s.substr(0,i);
    			if(isValid(t))
    			{
    				std::string tmp = s.substr(i);
    				dfs(tmp,n+1,ip+t+".",res);
    			}
    		}
    	}
    	bool isValid(std::string s)
    	{
    		if(s.size() == 0 || (s.size() > 1 && s[0] == '0')) return false;
    		int n = std::atoi(s.c_str());
    		if(0 <= n && n <= 255) return true;
    		return false;
    	}
    };


  • 相关阅读:
    解决Ubuntu下pycharm无法输入中文的问题
    爬取www.mmjpg.com网站图片,你懂得哦!
    批量查询ip地址归属地
    Opencv源码编译
    使用阿里云安装python模块
    Ansible运维自动化
    Mha-Atlas-MySQL高可用
    SVN
    Tomcat
    DHCP
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/5234819.html
Copyright © 2011-2022 走看看