Skip to content

Commit df3b19c

Browse files
committed
Solution for 468.
1 parent c689b09 commit df3b19c

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

solution/0400-0499/0468.Validate IP Address/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,33 @@
6565
<!-- 这里可写当前语言的特殊实现逻辑 -->
6666

6767
```python
68+
class Solution:
69+
def validIPAddress(self, IP: str) -> str:
70+
if "." in IP:
71+
segments = IP.split(".")
72+
if len(segments) != 4:
73+
return "Neither"
74+
for segment in segments:
75+
if (
76+
not segment.isdigit()
77+
or not 0 <= int(segment) <= 255
78+
or (segment[0] == "0" and len(segment) > 1)
79+
):
80+
return "Neither"
81+
return "IPv4"
82+
elif ":" in IP:
83+
segments = IP.split(":")
84+
if len(segments) != 8:
85+
return "Neither"
86+
for segment in segments:
87+
if (
88+
not segment
89+
or len(segment) > 4
90+
or not all(c in string.hexdigits for c in segment)
91+
):
92+
return "Neither"
93+
return "IPv6"
94+
return "Neither"
6895

6996
```
7097

solution/0400-0499/0468.Validate IP Address/README_EN.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,33 @@
5757
### **Python3**
5858

5959
```python
60+
class Solution:
61+
def validIPAddress(self, IP: str) -> str:
62+
if "." in IP:
63+
segments = IP.split(".")
64+
if len(segments) != 4:
65+
return "Neither"
66+
for segment in segments:
67+
if (
68+
not segment.isdigit()
69+
or not 0 <= int(segment) <= 255
70+
or (segment[0] == "0" and len(segment) > 1)
71+
):
72+
return "Neither"
73+
return "IPv4"
74+
elif ":" in IP:
75+
segments = IP.split(":")
76+
if len(segments) != 8:
77+
return "Neither"
78+
for segment in segments:
79+
if (
80+
not segment
81+
or len(segment) > 4
82+
or not all(c in string.hexdigits for c in segment)
83+
):
84+
return "Neither"
85+
return "IPv6"
86+
return "Neither"
6087

6188
```
6289

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution:
2+
def validIPAddress(self, IP: str) -> str:
3+
if "." in IP:
4+
segments = IP.split(".")
5+
if len(segments) != 4:
6+
return "Neither"
7+
for segment in segments:
8+
if (
9+
not segment.isdigit()
10+
or not 0 <= int(segment) <= 255
11+
or (segment[0] == "0" and len(segment) > 1)
12+
):
13+
return "Neither"
14+
return "IPv4"
15+
elif ":" in IP:
16+
segments = IP.split(":")
17+
if len(segments) != 8:
18+
return "Neither"
19+
for segment in segments:
20+
if (
21+
not segment
22+
or len(segment) > 4
23+
or not all(c in string.hexdigits for c in segment)
24+
):
25+
return "Neither"
26+
return "IPv6"
27+
return "Neither"

0 commit comments

Comments
 (0)