468. Validate IP Address

Description

Here

Intuition

Pitfall

这题目坑比较多……

JDK Split API

There are 2 pitfalls for split API).

  1. The parameter is regular expression. Therefore, . needs excape.
  2. The last empty string will be ignored.
    1. For example, "abc.edf.".split("\\.") will be split to ["abc", "edf"]

IPv4

The IPv 4 section has the following rules:

  1. The IP ranges from [0, 255]
  2. The IP cannot startswith 0
    1. For example, 01 or 023 is invalid
  3. The IP cannot empty
    1. 1..1.1 is invalid
  4. 这边最好直接先检查section的长度,验证数字还可以stirng很长,1111111...111,然后overflow,回到[0, 255]

IPv6

The IPv6 section should be following rules:

  1. 8 sections seperated by :
  2. Every section's length ranges from [1, 4]
  3. It can contain leading zeros
  4. Every char should be a Hex char

Solution

results matching ""

    No results matching ""