news 2026/6/24 0:20:48

验证IP地址(四)

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
验证IP地址(四)

方法二:分治法

思想

IPv4 和 IPv6 地址均是由特定的分界符隔开的字符串组成,并且每个子字符串具有相同格式。

因此,可以将地址分为多个块,然后逐块进行验证。

仅当每个块都有效时,该地址才有效。这种方法称为分治法

算法

  • 对于 IPv4 地址,通过界定符 . 将地址分为四块;对于 IPv6 地址,通过界定符 : 将地址分为八块。
  • 对于 IPv4 地址的每一块,检查它们是否在 0 - 255 内,且没有前置零。
  • 对于 IPv6 地址的每一块,检查其长度是否为 1 - 4 位的十六进制数。

Python 实现

class Solution: def validate_IPv4(self, IP: str) -> str: nums = IP.split('.') for x in nums: # Validate integer in range (0, 255): # 1. length of chunk is between 1 and 3 if len(x) == 0 or len(x) > 3: return "Neither" # 2. no extra leading zeros # 3. only digits are allowed # 4. less than 255 if x[0] == '0' and len(x) != 1 or not x.isdigit() or int(x) > 255: return "Neither" return "IPv4" def validate_IPv6(self, IP: str) -> str: nums = IP.split(':') hexdigits = '0123456789abcdefABCDEF' for x in nums: # Validate hexadecimal in range (0, 2**16): # 1. at least one and not more than 4 hexdigits in one chunk # 2. only hexdigits are allowed: 0-9, a-f, A-F if len(x) == 0 or len(x) > 4 or not all(c in hexdigits for c in x): return "Neither" return "IPv6" def validIPAddress(self, IP: str) -> str: if IP.count('.') == 3: return self.validate_IPv4(IP) elif IP.count(':') == 7: return self.validate_IPv6(IP) else: return "Neither"

Java 实现

class Solution { public String validateIPv4(String IP) { String[] nums = IP.split("\\.", -1); for (String x : nums) { // Validate integer in range (0, 255): // 1. length of chunk is between 1 and 3 if (x.length() == 0 || x.length() > 3) return "Neither"; // 2. no extra leading zeros if (x.charAt(0) == '0' && x.length() != 1) return "Neither"; // 3. only digits are allowed for (char ch : x.toCharArray()) { if (! Character.isDigit(ch)) return "Neither"; } // 4. less than 255 if (Integer.parseInt(x) > 255) return "Neither"; } return "IPv4"; } public String validateIPv6(String IP) { String[] nums = IP.split(":", -1); String hexdigits = "0123456789abcdefABCDEF"; for (String x : nums) { // Validate hexadecimal in range (0, 2**16): // 1. at least one and not more than 4 hexdigits in one chunk if (x.length() == 0 || x.length() > 4) return "Neither"; // 2. only hexdigits are allowed: 0-9, a-f, A-F for (Character ch : x.toCharArray()) { if (hexdigits.indexOf(ch) == -1) return "Neither"; } } return "IPv6"; } public String validIPAddress(String IP) { if (IP.chars().filter(ch -> ch == '.').count() == 3) { return validateIPv4(IP); } else if (IP.chars().filter(ch -> ch == ':').count() == 7) { return validateIPv6(IP); } else return "Neither"; } }

复杂度分析

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/23 11:00:16

16、Kubernetes存储与有状态应用运行指南

Kubernetes存储与有状态应用运行指南 1. Kubernetes存储插件 Kubernetes提供了多种存储插件,以满足不同的存储需求。其中,iSCSI插件支持 ReadWriteOnce 和 ReadonlyMany 访问模式,但目前不能对设备进行分区。以下是iSCSI卷的配置示例: volumes:- name: iscsi-volum…

作者头像 李华
网站建设 2026/6/23 17:00:06

19、Kubernetes资源配额、集群容量管理与性能优化

Kubernetes资源配额、集群容量管理与性能优化 1. 资源配额概述 在Kubernetes中,资源配额是管理命名空间内资源使用的重要手段。以下是一些常见的资源配额类型: - ResourceQuotas :命名空间内可存在的资源配额总数。 - Services :命名空间内可存在的服务总数。 - …

作者头像 李华
网站建设 2026/6/23 16:59:03

21、高级 Kubernetes 网络技术全解析

高级 Kubernetes 网络技术全解析 1. 基础 Linux 网络知识 Linux 默认具有单一的共享网络空间,在这个命名空间中,所有物理网络接口都是可访问的。不过,物理命名空间可以划分为多个逻辑命名空间,这与容器网络密切相关。 IP 地址和端口 :网络实体通过其 IP 地址进行标识。…

作者头像 李华
网站建设 2026/6/23 16:56:46

FastAPI多环境部署终极指南:3步告别配置地狱

FastAPI多环境部署终极指南:3步告别配置地狱 【免费下载链接】full-stack-fastapi-template 项目地址: https://gitcode.com/gh_mirrors/fu/full-stack-fastapi-template 还在为不同环境的配置差异而抓狂吗?🤯 开发环境跑得好好的&am…

作者头像 李华
网站建设 2026/6/23 14:45:26

DAIR-V2X车路协同实战手册:从数据到决策的全链路解密

DAIR-V2X车路协同实战手册:从数据到决策的全链路解密 【免费下载链接】DAIR-V2X 项目地址: https://gitcode.com/gh_mirrors/da/DAIR-V2X 技术痛点:为什么单车智能不够用? 在真实的城市道路环境中,单车智能面临着三大技术…

作者头像 李华
网站建设 2026/6/23 18:10:18

EmotiVoice语音温度调节概念引入,冷暖随心

EmotiVoice:让语音拥有情感温度 在智能音箱轻声回应“好的,马上为您播放音乐”的那一刻,你是否曾希望它的语气不是千篇一律的平静,而是能因你的喜悦而欢快、因你的疲惫而温柔?当虚拟助手读出天气预报时,如果…

作者头像 李华