Leetcode_restore-ip-addresses

地址:http://oj.leetcode.com/problems/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)

思路:dfs+回溯,注意剪枝,比如s的长度小于4或者大于12都是不可行的。

参考代码:

84ms
class Solution {
private:
    stringstream ss;
    int num;
public:
    void dfs(vector<string>&ans, int cnt, string ipstr, string s)
    {
        if(cnt == 4 && s.empty())
        {
            ans.push_back(ipstr);
            return;
        }
        if(cnt)
            ipstr += '.';
        for(int i = 1; i<=3 && i<=s.length(); ++i)
        {
            ss.clear();
            ss.str("");
            ss << s.substr(0, i);
            ss >> num;
            switch(i)
            {
                case 2:
                    if(num<10)
                        continue;
                break;
                case 3:
                    if(num<100 || num>255)
                        continue;
                break;
                default:
                break;
            }
            ipstr += s.substr(0, i);
            dfs(ans, cnt+1, ipstr, s.substr(i));
            ipstr = ipstr.substr(0, ipstr.length()-i);
        }
    }
    vector<string> restoreIpAddresses(string s) {
        vector<string>ans;
        if(s.empty() || s.length()<4 || s.length()>12)
            return ans;
        dfs(ans, 0, "", s);
        return ans;
    }
};



140ms
class Solution {
public:
    void dfs(vector<string>&ans, int cnt, string ipstr, string s)
    {
        if(cnt == 4 && s.empty())
        {
            ans.push_back(ipstr);
            return;
        }
        if(cnt)
            ipstr += '.';
        for(int i = 1; i<=3 && i<=s.length(); ++i)
        {
            int num;
            stringstream ss;
            ss << s.substr(0, i);
            ss >> num;
            switch(i)
            {
                case 2:
                    if(num<10)
                        continue;
                break;
                case 3:
                    if(num<100 || num>255)
                        continue;
                break;
                default:
                break;
            }
            ipstr += s.substr(0, i);
            dfs(ans, cnt+1, ipstr, s.substr(i));
            ipstr = ipstr.substr(0, ipstr.length()-i);
        }
    }
    vector<string> restoreIpAddresses(string s) {
        vector<string>ans;
        if(s.empty() || s.length()<4 || s.length()>12)
            return ans;
        dfs(ans, 0, "", s);
        return ans;
    }
};

看来stringstream变量挺耗时的,用同一个变量来存储快一点。


40ms, 

cnt>4的时候要返回

 
 
//SECOND TRIAL
class Solution {
private :
     void dfs ( vector < string >& ans , string dst , string src , int cnt )
     {

         if ( cnt >= 4 )
         {
             if ( cnt == 4 && src . empty ())
                 ans . push_back ( dst );
             return ;
         }
         for ( int i = 1 ; i <= 3 ; ++ i )
         {
             if ( src . length () >= i )
             {
                 if ( i == 2 && src [ 0 ] == '0' )
                     continue ;
                 else if ( i == 3 )
                 {
                     int num ;
                     string tmp = src . substr ( 0 , 3 );
                     stringstream ss ;
                     ss << tmp ;
                     ss >> num ;
                     if ( num > 255 || num < 100 )
                         continue ;
                 }
                 if ( cnt )
                     dst += "." ;
                 dst += src . substr ( 0 , i );
                 dfs ( ans , dst , src . substr ( i ), cnt + 1 );
                 if ( cnt )
                     dst = dst . substr ( 0 , dst . length () - i - 1 );
                 else
                     dst = dst . substr ( 0 , dst . length () - i );
             }
         }
     }
public :
     vector < string > restoreIpAddresses ( string s ) {
         vector < string > ans ;
         if ( s . length () < 4 || s . length () > 12 )
             return ans ;
         dfs ( ans , "" , s , 0 );
         return ans ;
     }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值