The problem with strings is really hard. General recursion is better than writing code, the general return will be timed out, and test examples are particularly many.
This problem just got hand when the direct panic, this situation is too many. Then calm down to think, in fact, is relatively simple.
An IP address, must be four integers plus three points, four integers to meet what, 0~255, and also, that is, the four integers must be exactly the original string to run out. A class of test classes that have been ignored for a while are 0 in the front but the actual number is not zero.
What does it take to be poor, or what is the object of a poor lift? Of course, the location of the three points to be inserted, the effective range of the number is very small, so the range of the exhaustive is very small, the number of digits only, the code to see. It was clear what was going on. When I first started thinking is to save each small number with substr, and then finally splicing, this overhead is too big, not recommended.
BOOL Isvalide (string s, int start, int end) {if (End-start > 3) return false; if (s[start] = = ' 0 ' && end-start>0) return false; int res = 0; for (int i=start;i<=end;i++) res = res*10+ (s[i]-' 0 '); if (res<0| | res>255) return false; return true;} Class Solution {public:vector<string> Restoreipaddresses (string s) {vector<string> res; int len = S.length (); if (len<4) return res; String TP (len+3, ' # '); for (int i=0;i<3&&i<len-3;i++) {if (!isvalide (s, 0, I)) continue; cout<<i<< "I" <<endl; for (int j=i+1;j<i+4&&j<len-2;j++) {if (!isvalide (S, I+1, J)) continue; cout<<j<< "J" <<endl; for (int k=j+1;k<j+4&&k<len-1;k++) {//cout<<k<< "K" <<endl; if (! ( Isvalide (S, j+1, K) &&isvalide (S, k+1, len-1))) Continue cout<< "* *" <<endl; int index = 0, m = 0; while (index<=i) tp[m++] = s[index++]; tp[m++] = '. '; while (index<=j) tp[m++] = s[index++]; tp[m++] = '. '; while (index<=k) tp[m++] = s[index++]; tp[m++] = '. '; while (Index<len) tp[m++] = s[index++]; cout<<tp<<endl; Res.push_back (TP);} }} return res; }};
Leetcode First brush _restore IP Addresses