LeetCode93

class Solution {

public:

    vectorres;

    string temp="";

    vector restoreIpAddresses(string s) {

        backtracking(s,0,0);

        return res;

    }

    void backtracking(string s,int st,int Num)

    {

        if(temp.size()==s.size()+3&&Num==4&&st==s.size())

        {

            res.push_back(temp);

            return;

        }

        for(int i=st;i

        {

            string obj=s.substr(st,i-st+1);

            string ori=temp;

            if(Judge(obj)&&Num<4)

            {

                temp+=obj;

                if(Num<3)

                    temp+=".";

                Num++;

            }

            else

                break;

            backtracking(s,i+1,Num);

            temp=ori;

            Num--;

            

        }

    }

    bool Judge(string obj)

    {

        if(obj[0]=='0'&&obj.size()>1)

            return false;

        int v=stoi(obj);

        if(v<0||v>255)

            return false;

        return true;

    }

};

你可能感兴趣的:(leetcode,c++)