forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.ts
40 lines (40 loc) · 1 KB
/
Solution.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
function validIPAddress(queryIP: string): string {
const isIPv4 = () => {
const ss = queryIP.split('.');
if (ss.length !== 4) {
return false;
}
for (const s of ss) {
const num = Number(s);
if (num < 0 || num > 255 || num + '' !== s) {
return false;
}
}
return true;
};
const isIPv6 = () => {
const ss = queryIP.split(':');
if (ss.length !== 8) {
return false;
}
for (const s of ss) {
if (s.length === 0 || s.length > 4) {
return false;
}
for (const c of s) {
if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')) {
continue;
}
return false;
}
}
return true;
};
if (isIPv4()) {
return 'IPv4';
}
if (isIPv6()) {
return 'IPv6';
}
return 'Neither';
}