zoukankan      html  css  js  c++  java
  • [leetcode]Restore IP Addresses

    DFS所有可能。。。

    class Solution {
    public:
        void search(const string& s , vector<string>& ans , int step , const string& ip , int start){
            if(s.size() == start && step == 4){
                ans.push_back(ip.substr(1 , ip.size()-1));  
               // cout << ip.substr(1 , ip.size()-1) <<endl;
            }
            
            if(s.size() <= start)
            {
                return;
            }
            //enumation 
            //length 1,2,3
            //1
           // cout << step << " "<< ip << endl;
            if(start < s.size())
            {
               // cout << "get in" <<endl;
                string tip = s.substr(start,1);
              //  cout<< tip << endl;
                search(s , ans , step + 1 , ip + "." + tip , start + 1);
            }
            //2
       
            if(start + 1 < s.size()){
                string tip = s.substr(start,2);
                if(tip[0] != '0') search(s , ans , step + 1 , ip + "." + tip , start + 2);
            }
            //3
            if(start + 2 < s.size()){
                string tip = s.substr(start,3);
                stringstream ss;
                ss << tip;
                int n;
                ss >> n;
                if(n>=100 && n <= 255) search(s , ans , step + 1 , ip + "." + tip , start + 3);
            }
        }
        vector<string> restoreIpAddresses(string s) {
            vector<string> ans;
            if(s.size() > 3*4) return ans;
            search(s , ans , 0 , "" , 0);
            return ans;
        }
    };
  • 相关阅读:
    使用jmeter和fidder进行APP接口测试
    测试模板积累
    功能测试-
    Django 路由
    Django 基础
    JQ 简单动画显示隐藏效果
    JQ $.each详解
    JQ 事件绑定与事件委派
    django模板之forloop
    JQ 属性操作
  • 原文地址:https://www.cnblogs.com/x1957/p/3496292.html
Copyright © 2011-2022 走看看