[Leetcode] Restore IP Addresses

Source: Internet
Author: User

Given a string containing only digits, restore it is returning all possible valid IP address combinations.

For example:
Given "25525511135" ,

Return ["255.255.11.135", "255.255.111.35"] . (Order does not matter)

Hide TagsBacktracking String

Idea: Dfs,backtrace,ip can only be composed of 3 digits, 1, 2, 3 each attempt, 2 bit when the first bit can not be 0, 3 bits is 0~255

classSolution {Vector<string>M_res; Vector<string>m_str;  Public:        voidDfsintDepstrings) {//Printvector (m_str);            if(M_str.size () >4)                return; if(m_str.size () = =4&& DEP <s.size ())return; if(DEP = =s.size ()) {                if(m_str.size () = =4)                {                    stringTmpstr;  for(inti =0; I <3; i++) {Tmpstr+ = (M_str[i] +"."); } tmpstr+ = m_str[3];                M_res.push_back (TMPSTR); }                return; }             for(inti =1; I <=3; i++)            {                if((dep+i-1) >=s.size ()) Break; if(i = =2)                {                    if(S[DEP] = ='0')                         Break; }                Else if(i = =3)                {                    if(S[DEP] = ='1'||(S[DEP]=='2'&& (s[dep+1] <='4'|| (s[dep+1] =='5'&& s[dep+2] <'6'))))                        ; Else                         Break;                } m_str.push_back (S.substr (DEP, i)); DFS (DEP+I, s);            M_str.pop_back (); }} vector<string> restoreipaddresses (strings) {m_res.clear ();            M_str.clear (); DFS (0, s); returnM_res; }};

[Leetcode] Restore IP Addresses

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.