[Leetcode] Restore IP Addresses Restore IP address

Source: Internet
Author: User

[leetcode] Restore IP Addresses restore IP address

Given a string containing only a number, restore it and return all possible IP address formats.

Example:

Input: "25525511135" output:["255.255.11.135", "255.255.111.35"]

The IP address consists of 32-bit binary numbers, for ease of use, often in the form of XXX.XXX.XXX.XXX, each group of XXX represents 10 binary numbers less than or equal to 255. So there are four paragraphs in total IP address, each paragraph may have one, two or three bits, the range is [0, 255], the topic clearly indicates that the input string contains only numbers, so when a segment is three, we have to determine whether it crosses the border (>255), and it is important that when there is only one, 0 can be a section, if there are two or three bits, such as 00, 01, 001, 011, 000, etc. are not legal, so we still need a decision function to determine whether a string is legitimate. This problem can also be seen as the segmentation of strings, in the input string to add three points, the string is divided into four paragraphs, each paragraph must be legal, for all possible situations. We use K to indicate the number of segments currently required, if k = 0, it means that three points have been added to complete, four paragraphs have been formed, if the string is just empty, then the results of the current good saved. If k! = 0, then for each paragraph, we use one, two, three bit to try, respectively, judge its incompatibility, if it is legal, then call DFS recursion continue to divide the remaining string, finally and find out all the legal combination, the code is as follows:


   
  
  1. class Solution (object):
  2. def restoreipaddresses (self, s):
  3. """
  4. : Type S:str
  5. : Rtype:list[str]
  6. """
  7. if s = = "" or len (s) > or len (s) < 4:
  8. return []
  9. result = []
  10. Parts = 4
  11. Item = ""
  12. Self. DFS (S, parts, item, result)
  13. return Result
  14. def DFS (self, s, parts, item, result):
  15. if parts = = 0:#全部取完
  16. if s = = "":
  17. Result.append (item)
  18. Else:
  19. for I in range (1, 4):
  20. if len (s) >= i and Self.isvalid (S[:i]):
  21. if parts = = 1:#最后一部分
  22. Self. DFS (s[i:], parts-1, item+s[:i], result)
  23. else:#开始部分
  24. Self. DFS (s[i:], parts-1, item+s[:i]+".", result)
  25. def IsValid (self, s):
  26. if s = = "" or len (s) > 3 or (len (s) > 1 and s[0 ] = = "0"):
  27. return False
  28. int_s = Int (s)
  29. return int_s <= 255 and int_s >= 0

[Leetcode] Restore IP Addresses Restore IP address

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.