Skip to content

Commit 7888203

Browse files
committed
feat: add solutions to lc problem: No.0468
No.0468.Validate IP Address
1 parent 55eed7e commit 7888203

File tree

4 files changed

+296
-0
lines changed

4 files changed

+296
-0
lines changed

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

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,108 @@
7676

7777
```
7878

79+
### **TypeScript**
80+
81+
```ts
82+
function validIPAddress(queryIP: string): string {
83+
const isIPv4 = () => {
84+
const ss = queryIP.split('.');
85+
if (ss.length !== 4) {
86+
return false;
87+
}
88+
for (const s of ss) {
89+
const num = Number(s);
90+
if (num < 0 || num > 255 || num + '' !== s) {
91+
return false;
92+
}
93+
}
94+
return true;
95+
};
96+
const isIPv6 = () => {
97+
const ss = queryIP.split(':');
98+
if (ss.length !== 8) {
99+
return false;
100+
}
101+
for (const s of ss) {
102+
if (s.length === 0 || s.length > 4) {
103+
return false;
104+
}
105+
for (const c of s) {
106+
if (
107+
(c >= '0' && c <= '9') ||
108+
(c >= 'a' && c <= 'f') ||
109+
(c >= 'A' && c <= 'F')
110+
) {
111+
continue;
112+
}
113+
return false;
114+
}
115+
}
116+
return true;
117+
};
118+
if (isIPv4()) {
119+
return 'IPv4';
120+
}
121+
if (isIPv6()) {
122+
return 'IPv6';
123+
}
124+
return 'Neither';
125+
}
126+
```
127+
128+
### **Rust**
129+
130+
```rust
131+
impl Solution {
132+
fn is_IPv4(s: &String) -> bool {
133+
let ss = s.split('.').collect::<Vec<&str>>();
134+
if ss.len() != 4 {
135+
return false;
136+
}
137+
for s in ss {
138+
match s.parse::<i32>() {
139+
Err(_) => return false,
140+
Ok(num) => {
141+
if num < 0 || num > 255 || num.to_string() != s.to_string() {
142+
return false;
143+
}
144+
}
145+
}
146+
}
147+
true
148+
}
149+
150+
fn is_IPv6(s: &String) -> bool {
151+
let ss = s.split(':').collect::<Vec<&str>>();
152+
if ss.len() != 8 {
153+
return false;
154+
}
155+
for s in ss {
156+
if s.len() == 0 || s.len() > 4 {
157+
return false;
158+
}
159+
for &c in s.as_bytes() {
160+
if c >= b'0' && c <= b'9' || c >= b'a' && c <= b'f' || c >= b'A' && c <= b'F' {
161+
continue;
162+
}
163+
return false;
164+
}
165+
}
166+
true
167+
}
168+
169+
pub fn valid_ip_address(query_ip: String) -> String {
170+
if Self::is_IPv4(&query_ip) {
171+
return String::from("IPv4");
172+
}
173+
if Self::is_IPv6(&query_ip) {
174+
return String::from("IPv6");
175+
}
176+
String::from("Neither")
177+
}
178+
}
179+
```
180+
79181
### **...**
80182

81183
```

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

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,108 @@
6666

6767
```
6868

69+
### **TypeScript**
70+
71+
```ts
72+
function validIPAddress(queryIP: string): string {
73+
const isIPv4 = () => {
74+
const ss = queryIP.split('.');
75+
if (ss.length !== 4) {
76+
return false;
77+
}
78+
for (const s of ss) {
79+
const num = Number(s);
80+
if (num < 0 || num > 255 || num + '' !== s) {
81+
return false;
82+
}
83+
}
84+
return true;
85+
};
86+
const isIPv6 = () => {
87+
const ss = queryIP.split(':');
88+
if (ss.length !== 8) {
89+
return false;
90+
}
91+
for (const s of ss) {
92+
if (s.length === 0 || s.length > 4) {
93+
return false;
94+
}
95+
for (const c of s) {
96+
if (
97+
(c >= '0' && c <= '9') ||
98+
(c >= 'a' && c <= 'f') ||
99+
(c >= 'A' && c <= 'F')
100+
) {
101+
continue;
102+
}
103+
return false;
104+
}
105+
}
106+
return true;
107+
};
108+
if (isIPv4()) {
109+
return 'IPv4';
110+
}
111+
if (isIPv6()) {
112+
return 'IPv6';
113+
}
114+
return 'Neither';
115+
}
116+
```
117+
118+
### **Rust**
119+
120+
```rust
121+
impl Solution {
122+
fn is_IPv4(s: &String) -> bool {
123+
let ss = s.split('.').collect::<Vec<&str>>();
124+
if ss.len() != 4 {
125+
return false;
126+
}
127+
for s in ss {
128+
match s.parse::<i32>() {
129+
Err(_) => return false,
130+
Ok(num) => {
131+
if num < 0 || num > 255 || num.to_string() != s.to_string() {
132+
return false;
133+
}
134+
}
135+
}
136+
}
137+
true
138+
}
139+
140+
fn is_IPv6(s: &String) -> bool {
141+
let ss = s.split(':').collect::<Vec<&str>>();
142+
if ss.len() != 8 {
143+
return false;
144+
}
145+
for s in ss {
146+
if s.len() == 0 || s.len() > 4 {
147+
return false;
148+
}
149+
for &c in s.as_bytes() {
150+
if c >= b'0' && c <= b'9' || c >= b'a' && c <= b'f' || c >= b'A' && c <= b'F' {
151+
continue;
152+
}
153+
return false;
154+
}
155+
}
156+
true
157+
}
158+
159+
pub fn valid_ip_address(query_ip: String) -> String {
160+
if Self::is_IPv4(&query_ip) {
161+
return String::from("IPv4");
162+
}
163+
if Self::is_IPv6(&query_ip) {
164+
return String::from("IPv6");
165+
}
166+
String::from("Neither")
167+
}
168+
}
169+
```
170+
69171
### **...**
70172

71173
```
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
impl Solution {
2+
fn is_IPv4(s: &String) -> bool {
3+
let ss = s.split('.').collect::<Vec<&str>>();
4+
if ss.len() != 4 {
5+
return false;
6+
}
7+
for s in ss {
8+
match s.parse::<i32>() {
9+
Err(_) => return false,
10+
Ok(num) => {
11+
if num < 0 || num > 255 || num.to_string() != s.to_string() {
12+
return false;
13+
}
14+
}
15+
}
16+
}
17+
true
18+
}
19+
20+
fn is_IPv6(s: &String) -> bool {
21+
let ss = s.split(':').collect::<Vec<&str>>();
22+
if ss.len() != 8 {
23+
return false;
24+
}
25+
for s in ss {
26+
if s.len() == 0 || s.len() > 4 {
27+
return false;
28+
}
29+
for &c in s.as_bytes() {
30+
if c >= b'0' && c <= b'9' || c >= b'a' && c <= b'f' || c >= b'A' && c <= b'F' {
31+
continue;
32+
}
33+
return false;
34+
}
35+
}
36+
true
37+
}
38+
39+
pub fn valid_ip_address(query_ip: String) -> String {
40+
if Self::is_IPv4(&query_ip) {
41+
return String::from("IPv4");
42+
}
43+
if Self::is_IPv6(&query_ip) {
44+
return String::from("IPv6");
45+
}
46+
String::from("Neither")
47+
}
48+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
function validIPAddress(queryIP: string): string {
2+
const isIPv4 = () => {
3+
const ss = queryIP.split('.');
4+
if (ss.length !== 4) {
5+
return false;
6+
}
7+
for (const s of ss) {
8+
const num = Number(s);
9+
if (num < 0 || num > 255 || num + '' !== s) {
10+
return false;
11+
}
12+
}
13+
return true;
14+
};
15+
const isIPv6 = () => {
16+
const ss = queryIP.split(':');
17+
if (ss.length !== 8) {
18+
return false;
19+
}
20+
for (const s of ss) {
21+
if (s.length === 0 || s.length > 4) {
22+
return false;
23+
}
24+
for (const c of s) {
25+
if (
26+
(c >= '0' && c <= '9') ||
27+
(c >= 'a' && c <= 'f') ||
28+
(c >= 'A' && c <= 'F')
29+
) {
30+
continue;
31+
}
32+
return false;
33+
}
34+
}
35+
return true;
36+
};
37+
if (isIPv4()) {
38+
return 'IPv4';
39+
}
40+
if (isIPv6()) {
41+
return 'IPv6';
42+
}
43+
return 'Neither';
44+
}

0 commit comments

Comments
 (0)