[LeetCode]Restore IP Addresses

来源:互联网 发布:供应链反应矩阵 编辑:程序博客网 时间:2024/04/28 07:07

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+剪枝

注意双0的情况

class Solution {public:    vector<string> restoreIpAddresses(string s) {        vector<string> ret;        string ip;        dfs(s,0,0,ip,ret);        return ret;    }    void dfs(string s,size_t start,int step,string ip,vector<string> &ret){        if(start==s.size()&&step==4){            ip.resize(ip.size()-1);            ret.push_back(ip);            return;        }        if(s.size()-start>3*(4-step))            return;        if(s.size()-start<4-step)            return;        int num = 0;        for(size_t i=start; i<start+3; ++i){            ip =ip+s[i];            num = 10 * num + s[i]-'0';            if(num <= 255)                dfs(s,i+1,step+1,ip+'.',ret);            if(num == 0) //no single 0                break;        }    }};


0 0
原创粉丝点击