zoukankan      html  css  js  c++  java
  • LeetCode Restore IP Addresses

    DFS

    class Solution {
    public:
    	vector<string> restoreIpAddresses(string s)
    	{
    		return insertDot(s, 0, 3);
    	}
    	
    	vector<string> insertDot(string s, int beginIndex, int countOfDot /*3, 2, 1, 0*/)
    	{
    		vector<string> result;
    		if( (s.size() - beginIndex) < (countOfDot + 1) || (s.size() - beginIndex - 1) > (countOfDot +1) * 3)
    			return result;
    		
    		if(countOfDot == 0)
    		{
    			string partition = s.substr(beginIndex);
    			if(partition.size() > 1 && partition[0] == '0') //Error 4: if the first char is 0, then no more chars, such as 1.00.1.1 is no valid;
    			{
    				return result;
    			}
    			int val = std::stoi(partition);
    			if(val >= 0 && val <256)
    			{
    				result.push_back(partition);
    			}
    			return result;
    		}
    		
    		for(int i = beginIndex + 1; i< s.size();i++ ) //Error 1: i< s.size() -1
    		{
    			string partition = s.substr(beginIndex, i - beginIndex);
    			if(partition.size() > 1 && partition[0] == '0') //Error 3: if the first char is 0, then no more chars, such as 1.00.1.1 is no valid;
    			{
    				break;
    			}
    			int val = std::stoi(partition);
    			if(val > 255)
    				break;
    			if(val >= 0 && val <256)
    			{
    				vector<string> subresult = insertDot(s, i, countOfDot - 1);
    				if(subresult.size() != 0)
    				{
    					for(int j = 0; j< subresult.size(); j++) //Error 2: j< s.size() -1
    					{
    						string onepartition = partition + "." + subresult[j];
    						result.push_back(onepartition);
    					}
    				}
    			}
    		}
    		return result;
    	}
    };
    
  • 相关阅读:
    电脑常用快捷键
    k8s node 系统参数调整
    docker 制作镜像并上传
    php服务部署安装
    安装 Kuboard v2
    keepalive配置
    elasticsearch的segment详解
    grafana模板
    jenkins启动
    kubernetes 中文文档
  • 原文地址:https://www.cnblogs.com/whyandinside/p/5281924.html
Copyright © 2011-2022 走看看