Fast Track:
https://oj.leetcode.com/problems/restore-ip-addresses/
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)
Given a string containing only numbers, restore all legitimate IP addresses. For example: Given "25525511135", Return ["255.255.11.135", "255.255.111.35"]. (Order does not matter)
Method One:
Idea: Violent enumeration of Dfs
Code:
classSolution { Public: voidHelpstring&s,intind,vector<string> &ip, vector<string> &result) { if(Ip.size () >=4) { if(Ind >=s.size ()) {Result.push_back (ip[0] +"."+ ip[1] +"."+ ip[2] +"."+ ip[3]); } return; } if(Ind >=s.size ()) { return; } ip.push_back (S.SUBSTR (Ind,1)); Help (S, Ind+1, IP, result); Ip.pop_back (); if(S[ind] = ='0') { return; } if(Ind +1<s.size ()) {Ip.push_back (S.SUBSTR (Ind,2)); Help (S, Ind+2, IP, result); Ip.pop_back (); } if(Ind +2< S.size ()) && (S.SUBSTR (Ind,3) <="255") {ip.push_back (S.SUBSTR (Ind,3)); Help (S, Ind+3, IP, result); Ip.pop_back (); }} vector<string> restoreipaddresses (strings) {//Important:please Reset any member data declared, as//The same solution instance would be a reused for each test case.vector<string>IP; Vector<string>result; if((S.size () >=4) && (S.size () <= A) {Help (s),0, IP, result); } returnresult; }};
Analysis of Complexity:
Recursive only approximate analysis, enumeration of all schemes, the number of not more than C (3, len-1), so the complexity can be considered O (len ^ 3), but Len is not more than 12 ...
Details and pitfalls:
Note that each number range is 0. 255, if the two-digit or three-digit number is not allowed to have the first 0.
Similar topics:
Personally think to a dictionary, ask whether a string can be spelled out in the dictionary word and this is very similar. If only ask whether can spell out, is the classic DP problem, add one word at a time. But for all the solutions, it's still exponential, and of course DP can record the precursor, or it can search ...
Method Two:
Idea: Because there are only four domains, just enumerate the lengths of each field, and then split the string to judge each other.
Python has convenient str () and int (), which makes it easy to use lambda and map to implement the judging section.
#!/usr/bin/python#-*-coding:utf-8-*-classSolution:#@param s, a string #@return A list of strings defrestoreipaddresses (self, s): Ret=[] IsValid=LambdaX:STR (int (x)) = = X andint (x) < 256 forIinchRange (1, 4): forJinchRange (1, 4): forKinchRange (1, 4): Sub= [S[0:i], S[i:i + j], S[i + j:i + j + K], S[i + j +K:]] if "' not inchSub andFalse not inchMap (IsValid, sub): Ret.append ('.'. Join (sub))returnRETs=solution ()PrintS.restoreipaddresses ('25525511134')
From [email protected] Rice Group brush problem squad &[email protected]
Http://www.meetqun.com/thread-2420-1-1.html
Https://github.com/illuz/leetcode/tree/master/solutions/093.Restore_IP_Addresses
[Leetcode] Restore IP Addresses