468. Validate IP Address
Description
Intuition
Pitfall
这题目坑比较多……
JDK Split API
There are 2 pitfalls for split API).
- The parameter is regular expression. Therefore,
.
needs excape. - The last empty string will be ignored.
- For example,
"abc.edf.".split("\\.")
will be split to["abc", "edf"]
- For example,
IPv4
The IPv 4 section has the following rules:
- The IP ranges from
[0, 255]
- The IP cannot startswith
0
- For example,
01
or023
is invalid
- For example,
- The IP cannot empty
1..1.1
is invalid
- 这边最好直接先检查
section
的长度,验证数字还可以stirng很长,1111111...111,然后overflow,回到[0, 255]
IPv6
The IPv6 section should be following rules:
- 8 sections seperated by
:
- Every section's length ranges from
[1, 4]
- It can contain leading zeros
- Every char should be a Hex char