93. 复原IP地址

思路:递归+回溯+剪枝(valid判断)
class Solution {
public:
vector restoreIpAddresses(string s) {

    vector res;
    if(s.length()<4)
        return res;
    
    backtracking(0,"",res,s,1);

    return res;
}

void backtracking(int begin,string tmp, vector& res, string s,int index)
{
    if(index==5)
    {
        if(begin==s.length())
        {
            res.push_back(tmp);
        }
        return;
    }

    for(int i=begin;i1 && s[0]=='0')
        return false;
    
    if(stoi(s)>255)
        return false;

    return true;

}

};

你可能感兴趣的:(93. 复原IP地址)