Leetcode First brush _restore IP Addresses

Source: Internet
Author: User

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

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.