Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit 2fb259c

Browse files
aQuaaQua
aQua
authored and
aQua
committed
468 added
1 parent c199122 commit 2fb259c

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# [468. Validate IP Address](https://leetcode.com/problems/validate-ip-address/)
2+
3+
## 题目
4+
5+
Write a function to check whether an input string is a valid IPv4 address or IPv6 address or neither.
6+
7+
IPv4 addresses are canonically represented in dot-decimal notation, which consists of four decimal numbers, each ranging from 0 to 255, separated by dots ("."), e.g.,172.16.254.1;
8+
9+
Besides, leading zeros in the IPv4 is invalid. For example, the address 172.16.254.01 is invalid.
10+
11+
IPv6 addresses are represented as eight groups of four hexadecimal digits, each group representing 16 bits. The groups are separated by colons (":"). For example, the address 2001:0db8:85a3:0000:0000:8a2e:0370:7334 is a valid one. Also, we could omit some leading zeros among four hexadecimal digits and some low-case characters in the address to upper-case ones, so 2001:db8:85a3:0:0:8A2E:0370:7334 is also a valid IPv6 address(Omit leading zeros and using upper cases).
12+
13+
However, we don't replace a consecutive group of zero value with a single empty group using two consecutive colons (::) to pursue simplicity. For example, 2001:0db8:85a3::8A2E:0370:7334 is an invalid IPv6 address.
14+
15+
Besides, extra leading zeros in the IPv6 is also invalid. For example, the address 02001:0db8:85a3:0000:0000:8a2e:0370:7334 is invalid.
16+
17+
Note:
18+
You may assume there is no extra space or special characters in the input string.
19+
20+
Example 1:
21+
22+
```text
23+
Input: "172.16.254.1"
24+
25+
Output: "IPv4"
26+
27+
Explanation: This is a valid IPv4 address, return "IPv4".
28+
```
29+
30+
Example 2:
31+
32+
```text
33+
Input: "2001:0db8:85a3:0:0:8A2E:0370:7334"
34+
35+
Output: "IPv6"
36+
37+
Explanation: This is a valid IPv6 address, return "IPv6".
38+
```
39+
40+
Example 3:
41+
42+
```text
43+
Input: "256.256.256.256"
44+
45+
Output: "Neither"
46+
47+
Explanation: This is neither a IPv4 address nor a IPv6 address.
48+
```
49+
50+
## 解题思路
51+
52+
见程序注释
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package Problem0468
2+
3+
func validIPAddress(IP string) string {
4+
res :=
5+
6+
return res
7+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package Problem0468
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
// tcs is testcase slice
11+
var tcs = []struct {
12+
IP string
13+
ans string
14+
}{
15+
16+
17+
18+
// 可以有多个 testcase
19+
}
20+
21+
func Test_validIPAddress(t *testing.T) {
22+
ast := assert.New(t)
23+
24+
for _, tc := range tcs {
25+
fmt.Printf("~~%v~~\n", tc)
26+
ast.Equal(tc.ans, validIPAddress(tc.IP), "输入:%v", tc)
27+
}
28+
}
29+
30+
func Benchmark_validIPAddress(b *testing.B) {
31+
for i := 0; i < b.N; i++ {
32+
for _, tc := range tcs {
33+
validIPAddress(tc.IP)
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)