LC 468. 验证IP地址
题目描述
这是 LeetCode 上的 468. 验证IP地址 ,难度为 中等。
给定一个字符串 queryIP
。如果是有效的 IPv4
地址,返回 "IPv4"
;如果是有效的 IPv6
地址,返回 "IPv6"
;如果不是上述类型的 IP
地址,返回 "Neither"
。
有效的 IPv4
地址 是 “x1.x2.x3.x4”
形式的 IP
地址。 其中$ 0 <= x_i <= 255$ 且 $x_i$ 不能包含 前导零。
例如: “192.168.1.1”
、 “192.168.1.0”
为有效 IPv4
地址, “192.168.01.1”
为无效 IPv4
地址; “192.168.1.00”
、 “192.168@1.1”
为无效 IPv4
地址。
一个有效的 IPv6
地址 是一个格式为 “x1:x2:x3:x4:x5:x6:x7:x8”
的 IP
地址,其中:
- $1 <= x_i.length <= 4$
- $x_i$ 是一个 十六进制字符串 ,可以包含数字、小写英文字母(
'a'
到'f'
)和大写英文字母('A'
到'F'
)。 - 在 $x_i$ 中允许前导零。
例如 "2001:0db8:85a3:0000:0000:8a2e:0370:7334"
和 "2001:db8:85a3:0:0:8A2E:0370:7334"
是有效的 IPv6
地址,而 "2001:0db8:85a3::8A2E:037j:7334"
和 "02001:0db8:85a3:0000:0000:8a2e:0370:7334"
是无效的 IPv6
地址。
示例 1:1
2
3
4
5输入:queryIP = "172.16.254.1"
输出:"IPv4"
解释:有效的 IPv4 地址,返回 "IPv4"
示例 2:1
2
3
4
5输入:queryIP = "2001:0db8:85a3:0:0:8A2E:0370:7334"
输出:"IPv6"
解释:有效的 IPv6 地址,返回 "IPv6"
示例 3:1
2
3
4
5输入:queryIP = "256.256.256.256"
输出:"Neither"
解释:既不是 IPv4 地址,又不是 IPv6 地址
提示:
queryIP
仅由英文字母,数字,字符'.'
和':'
组成。
模拟
为了方便,我们称合法 IPv4
/IPv6
中由 .
/:
分割的部分称为 item
。
无论是 IPv4
还是 IPv6
,我们都只需将连续段的 item
取出,并结合题意判断即可,一个较为简单的方式使用 split
操作来得到所有的 item
,考虑到某些语言并不内置 split
,这里采取双指针的方式来做。
为方便大家理解,今天将题解文字说明写到注释中。
Java 代码: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
41
42
43
44class Solution {
public String validIPAddress(String ip) {
if (ip.indexOf(".") >= 0 && check4(ip)) return "IPv4";
if (ip.indexOf(":") >= 0 && check6(ip)) return "IPv6";
return "Neither";
}
boolean check4(String ip) {
int n = ip.length(), cnt = 0;
char[] cs = ip.toCharArray();
for (int i = 0; i < n && cnt <= 3; ) {
// 找到连续数字段,以 x 存取
int j = i, x = 0;
while (j < n && cs[j] >= '0' && cs[j] <= '9' && x <= 255) x = x * 10 + (cs[j++] - '0');
// 非 item 字符之间没有 item
if (i == j) return false;
// 含前导零 或 数值大于 255
if ((j - i > 1 && cs[i] == '0') || (x > 255)) return false;
i = j + 1;
if (j == n) continue;
// 存在除 . 以外的其他非数字字符
if (cs[j] != '.') return false;
cnt++;
}
// 恰好存在 3 个不位于两端的 .
return cnt == 3 && cs[0] != '.' && cs[n - 1] != '.';
}
boolean check6(String ip) {
int n = ip.length(), cnt = 0;
char[] cs = ip.toCharArray();
for (int i = 0; i < n && cnt <= 7; ) {
int j = i;
while (j < n && ((cs[j] >= 'a' && cs[j] <= 'f') || (cs[j] >= 'A' && cs[j] <= 'F') || (cs[j] >= '0' && cs[j] <= '9'))) j++;
// 非 item 字符之间没有 item 或 长度超过 4
if (i == j || j - i > 4) return false;
i = j + 1;
if (j == n) continue;
// 存在除 : 以外的其他非数字字符
if (cs[j] != ':') return false;
cnt++;
}
// 恰好存在 7 个不位于两段的 :
return cnt == 7 && cs[0] != ':' && cs[n - 1] != ':';
}
}
C++ 代码: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
35class Solution {
public:
string validIPAddress(string ip) {
if (ip.find('.') != string::npos && check4(ip)) return "IPv4";
if (ip.find(':') != string::npos && check6(ip)) return "IPv6";
return "Neither";
}
bool check4(string ip) {
int n = ip.length(), cnt = 0;
for (int i = 0; i < n && cnt <= 3; ) {
int j = i, x = 0;
while (j < n && isdigit(ip[j]) && x <= 255) x = x * 10 + (ip[j++] - '0');
if (i == j) return false;
if ((j - i > 1 && ip[i] == '0') || x > 255) return false;
i = j + 1;
if (j == n) continue;
if (ip[j] != '.') return false;
cnt++;
}
return cnt == 3 && ip[0] != '.' && ip[n - 1] != '.';
}
bool check6(string ip) {
int n = ip.length(), cnt = 0;
for (int i = 0; i < n && cnt <= 7; ) {
int j = i;
while (j < n && (isxdigit(ip[j]) || isdigit(ip[j]))) j++;
if (i == j || j - i > 4) return false;
i = j + 1;
if (j == n) continue;
if (ip[j] != ':') return false;
cnt++;
}
return cnt == 7 && ip[0] != ':' && ip[n - 1] != ':';
}
};
Python 代码: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
41
42class Solution:
def validIPAddress(self, ip: str) -> str:
if '.' in ip and self.check4(ip):
return "IPv4"
if ':' in ip and self.check6(ip):
return "IPv6"
return "Neither"
def check4(self, ip: str) -> bool:
n, cnt, i = len(ip), 0, 0
while i < n and cnt <= 3:
j, x = i, 0
while j < n and '0' <= ip[j] <= '9' and x <= 255:
x = x * 10 + (ord(ip[j]) - ord('0'))
j += 1
if i == j:
return False
if (j - i > 1 and ip[i] == '0') or x > 255:
return False
i = j + 1
if j == n:
continue
if ip[j] != '.':
return False
cnt += 1
return cnt == 3 and ip[0] != '.' and ip[n - 1] != '.'
def check6(self, ip: str) -> bool:
n, cnt, i = len(ip), 0, 0
while i < n and cnt <= 7:
j = i
while j < n and ('a' <= ip[j] <= 'f' or 'A' <= ip[j] <= 'F' or '0' <= ip[j] <= '9'):
j += 1
if i == j or j - i > 4:
return False
i = j + 1
if j == n:
continue
if ip[j] != ':':
return False
cnt += 1
return cnt == 7 and ip[0] != ':' and ip[n - 1] != ':'
TypeScript 代码: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
38function validIPAddress(ip: string): string {
if (ip.includes('.') && check4(ip)) return "IPv4";
if (ip.includes(':') && check6(ip)) return "IPv6";
return "Neither";
};
function check4(ip: string): boolean {
const n = ip.length;
let cnt = 0;
for (let i = 0; i < n && cnt <= 3; ) {
let j = i, x = 0;
while (j < n && '0' <= ip[j] && ip[j] <= '9' && x <= 255) {
x = x * 10 + (ip.charCodeAt(j) - '0'.charCodeAt(0));
j++;
}
if (i === j || (j - i > 1 && ip[i] === '0') || x > 255) return false;
i = j + 1;
if (j === n) continue;
if (ip[j] !== '.') return false;
cnt++;
}
return cnt === 3 && ip[0] !== '.' && ip[n - 1] !== '.';
}
function check6(ip: string): boolean {
const n = ip.length;
let cnt = 0;
for (let i = 0; i < n && cnt <= 7; ) {
let j = i;
while (j < n && ('a' <= ip[j] && ip[j] <= 'f' || 'A' <= ip[j] && ip[j] <= 'F' || '0' <= ip[j] && ip[j] <= '9')) {
j++;
}
if (i === j || j - i > 4) return false;
i = j + 1;
if (j === n) continue;
if (ip[j] !== ':') return false;
cnt++;
}
return cnt === 7 && ip[0] !== ':' && ip[n - 1] !== ':';
}
- 时间复杂度:$O(n)$
- 空间复杂度:使用
toCharArray
操作会产生新数组,复杂度为 $O(n)$,使用charAt
操作代替复杂度为 $O(1)$
最后
这是我们「刷穿 LeetCode」系列文章的第 No.468
篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!