当前位置: 首页 > news >正文

验证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"; } }

复杂度分析

http://www.jsqmd.com/news/104756/

相关文章:

  • ComfyUI字幕增强插件完整配置指南:从零部署到高效批量处理
  • 【centos】安装python3.12
  • 亚马逊广告深度运营:跨越认知盲区,解锁高转化操作心法
  • 53、Linux 脚本编程入门指南
  • 如何快速掌握rclone:云存储管理的终极指南
  • 54、Linux实用工具与脚本配置全解析
  • ReadCat:终极免费电子书阅读器,重新定义你的数字阅读体验
  • 33、网络管理与设备驱动:SNMP及帧缓冲器驱动全解析
  • MuJoCo逆向运动学终极指南:如何快速配置人形机器人运动重定向?
  • 智慧医疗内窥镜息肉检测数据集VOC+YOLO格式9248张2类别
  • 从GEO关键词热度看 AI 搜索产品的真实使用阶段
  • EmotiVoice语音一致性保障机制:确保长时间输出稳定
  • EmotiVoice语音合成服务熔断降级方案设计
  • 手把手教你部署Context7 MCP Server:告别环境配置烦恼
  • JSZip错误处理实战指南:从崩溃到掌控
  • [C#][winform]基于yolov11的打电话玩手机检测系统C#源码+onnx模型+评估指标曲线+精美GUI界面
  • 游戏NPC对话系统新选择:EmotiVoice情感化语音合成实战
  • FastAPI企业级应用架构:从零构建高可用微服务系统
  • 55、Linux系统脚本与故障排查实用指南
  • Subfinder:一站式解决你的字幕搜索下载难题
  • 34、Linux 帧缓冲设备驱动配置与数据库到文件实用工具指南
  • 如何用7个步骤构建企业级无人机云端系统:从架构设计到性能优化
  • OpenUtau:零基础打造专属虚拟歌手的终极指南
  • 48、Linux 系统安全:PAM、文件权限与网络防护
  • traceId 传递-controller场景
  • EmotiVoice API接口文档解读:快速接入自有系统
  • 49、系统安全与性能调优全攻略
  • AutoUnipus智能学习助手:轻松应对U校园课程挑战
  • HLS.js实战手册:从零搭建高性能流媒体播放器
  • OCLP-Mod终极使用教程:让老旧Mac快速升级最新macOS