Restore IP Addresses

Source: Internet
Author: User

Restore IP Addresses

Problem:

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

Ideas:

DFS + backtracking templates

My Code 1:

IsValid to catch to 00 0 010 is illegal data

 Public classSolution { PublicList<string>restoreipaddresses (String s) {if(s = =NULL|| S.length () < 4 | | S.length () > 12)returnlist; Helper (S,0, ""); returnlist; }    Privatelist<string> list =NewArraylist<string>();  Public voidHelper (String s,intcount, String Res) {        if(Count = = 4 && s.equals ("") {list.add (res.substring (1)); return; }         for(inti = 1; I <= s.length () && i <= 3; i++) {String IP= s.substring (0, i); if(Validip (IP)) {helper (s.substring (i), Count+ 1, res + "." +IP); }        }    }     Public Booleanvalidip (String IP) {if(Ip.length () > 1 && ip.charat (0) = = ' 0 ')return false; intValue =integer.parseint (IP); if(Value >= 0 && value <= 255)return true; return false; }}
View Code

The code in code 1 that satisfies the requirements should include the IF (count = = 4 &&!s.equals ("")) return; Early stacks, lest waste of time

My Code 2:

 Public classSolution { PublicList<string>restoreipaddresses (String s) {if(s = =NULL|| S.length () < 4 | | S.length () > 12)returnlist; Helper (S,0, ""); returnlist; }    Privatelist<string> list =NewArraylist<string>();  Public voidHelper (String s,intcount, String Res) {        if(Count = = 4)        {            if(!s.equals (""))return; List.add (Res.substring (1)); return; }         for(inti = 1; I <= s.length () && i <= 3; i++) {String IP= s.substring (0, i); if(Validip (IP)) {helper (s.substring (i), Count+ 1, res + "." +IP); }        }    }     Public Booleanvalidip (String IP) {if(Ip.length () > 1 && ip.charat (0) = = ' 0 ')return false; intValue =integer.parseint (IP); if(Value >= 0 && value <= 255)return true; return false; }}
View Code

Code 2 IsValid code is too ugly, not concise, the following two lines can be merged

My Code 3

 Public classSolution { PublicList<string>restoreipaddresses (String s) {if(s = =NULL|| S.length () < 4 | | S.length () > 12)returnlist; Helper (S,0, ""); returnlist; }    Privatelist<string> list =NewArraylist<string>();  Public voidHelper (String s,intcount, String Res) {        if(Count = = 4)        {            if(!s.equals (""))return; List.add (Res.substring (1)); return; }         for(inti = 1; I <= s.length () && i <= 3; i++) {String IP= s.substring (0, i); if(Validip (IP)) {helper (s.substring (i), Count+ 1, res + "." +IP); }        }    }     Public Booleanvalidip (String IP) {if(Ip.length () > 1 && ip.charat (0) = = ' 0 ')return false; intValue =integer.parseint (IP); returnValue >= 0 && value <= 255; }}
View Code

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.