LeetCode //C - 468. Validate IP Address

embedded/2024/11/24 9:33:40/

468. Validate IP Address

Given a string queryIP, return “IPv4” if IP is a valid IPv4 address, “IPv6” if IP is a valid IPv6 address or “Neither” if IP is not a correct IP of any type.

A valid IPv4 address is an IP in the form “ x 1 . x 2 . x 3 . x 4 x_1.x_2.x_3.x_4 x1.x2.x3.x4” where 0 <= x i x_i xi <= 255 and xi cannot contain leading zeros. For example, “192.168.1.1” and “192.168.1.0” are valid IPv4 addresses while “192.168.01.1”, “192.168.1.00”, and “192.168@1.1” are invalid IPv4 addresses.

A valid IPv6 address is an IP in the form “ x 1 : x 2 : x 3 : x 4 : x 5 : x 6 : x 7 : x 8 x_1:x_2:x_3:x_4:x_5:x_6:x_7:x_8 x1:x2:x3:x4:x5:x6:x7:x8” where:

  • 1 <= x i . l e n g t h x_i.length xi.length <= 4
  • x i x_i xi is a hexadecimal string which may contain digits, lowercase English letter (‘a’ to ‘f’) and upper-case English letters (‘A’ to ‘F’).
  • Leading zeros are allowed in x i x_i xi.

For example, “2001:0db8:85a3:0000:0000:8a2e:0370:7334” and “2001:db8:85a3:0:0:8A2E:0370:7334” are valid IPv6 addresses, while “2001:0db8:85a3::8A2E:037j:7334” and “02001:0db8:85a3:0000:0000:8a2e:0370:7334” are invalid IPv6 addresses.
 

Example 1:

Input: queryIP = “172.16.254.1”
Output: “IPv4”
Explanation: This is a valid IPv4 address, return “IPv4”.

Example 2:

Input: queryIP = “2001:0db8:85a3:0:0:8A2E:0370:7334”
Output: “IPv6”
Explanation: This is a valid IPv6 address, return “IPv6”.

Example 3:

Input: queryIP = “256.256.256.256”
Output: “Neither”
Explanation: This is neither a IPv4 address nor a IPv6 address.

Constraints:
  • queryIP consists only of English letters, digits and the characters ‘.’ and ‘:’.

From: LeetCode
Link: 468. Validate IP Address


Solution:

Ideas:

IPv4 Validation:

  • Segment Parsing:
    • We manually parse each segment between the dots.
    • Check for empty segments or segments longer than 3 digits.
    • Ensure no leading zeros unless the segment is zero itself.
    • Convert each segment to an integer and check if it’s within the range 0 to 255.
  • Delimiter Checks:
    • If a dot is at the beginning or if there are two dots in a row, the IP is invalid.
    • Ensure that there are exactly 4 segments.

IPv6 Validation:

  • Segment Parsing:
    • We manually parse each segment between the colons.
    • Check for empty segments or segments longer than 4 characters.
    • Ensure all characters are valid hexadecimal digits.
  • Delimiter Checks:
    • If a colon is at the beginning or if there are two colons in a row, the IP is invalid.
    • Ensure that there are exactly 8 segments.

General Logic:

  • We use manual parsing instead of strtok to accurately detect empty segments and consecutive delimiters.
  • After parsing, we return “IPv4” or “IPv6” if the IP address meets all the criteria; otherwise, we return “Neither”.
Code:
char* validIPAddress(char* queryIP) {int len = strlen(queryIP);int i = 0;// Check for IPv4if (strchr(queryIP, '.') != NULL) {int segmentCount = 0;while (i < len) {int segmentLength = 0;int segmentValue = 0;// If we encounter a dot at the start or two dots in a row, it's invalidif (queryIP[i] == '.') {return "Neither";}while (i < len && queryIP[i] != '.') {if (!isdigit(queryIP[i])) {return "Neither";}if (segmentLength == 0 && queryIP[i] == '0' && (i + 1 < len && queryIP[i + 1] != '.')) {return "Neither"; // Leading zero}segmentValue = segmentValue * 10 + (queryIP[i] - '0');if (segmentValue > 255) {return "Neither";}segmentLength++;i++;}if (segmentLength == 0 || segmentLength > 3) {return "Neither";}segmentCount++;if (segmentCount > 4) {return "Neither";}if (i < len && queryIP[i] == '.') {i++; // Skip the dot// Check for double dotsif (i >= len || queryIP[i] == '.') {return "Neither";}}}return segmentCount == 4 ? "IPv4" : "Neither";}// Check for IPv6else if (strchr(queryIP, ':') != NULL) {int segmentCount = 0;while (i < len) {int segmentLength = 0;// If we encounter a colon at the start or two colons in a row, it's invalidif (queryIP[i] == ':') {return "Neither";}while (i < len && queryIP[i] != ':') {if (!isxdigit(queryIP[i])) {return "Neither";}segmentLength++;if (segmentLength > 4) {return "Neither";}i++;}if (segmentLength == 0) {return "Neither";}segmentCount++;if (segmentCount > 8) {return "Neither";}if (i < len && queryIP[i] == ':') {i++; // Skip the colon// Check for double colonsif (i >= len || queryIP[i] == ':') {return "Neither";}}}return segmentCount == 8 ? "IPv6" : "Neither";}return "Neither";
}

http://www.ppmy.cn/embedded/140091.html

相关文章

多模块开发环境中@autuwired注解注入Service层、Dao层组件注入失败

目录 多模块开发下Service模块中的组件在Web模块中注入失败 多模块开发下dao模块中的接口&#xff08;继承JPA/CrudRepository&#xff09;组件在Web模块中注入失败(单模块开发中没有问题); 引发思考SpringBootApplication和ComponentScan比较 注意事项&#xff1a; 结论&a…

Lucene数据写入与数据刷盘机制

一、Lucene数据写入流程 Lucene的数据写入流程主要涉及到文档的创建、索引的添加以及最终写入磁盘的过程。 文档的创建 Lucene中的文档&#xff08;Document&#xff09;是索引的基本单位&#xff0c;每个文档都包含了一系列的字段&#xff08;Field&#xff09;。这些字段可以…

Manus Xsens Metagloves虚拟现实手套

Manus Xsens Metagloves新一代手指捕捉 Xsens Metagloves经过专门开发&#xff0c;可与Xsens MVN软件无缝协作。只需点击一下&#xff0c;即可将精确的量子手指跟踪添加到Xsens设置中。 手指追踪的全新黄金标准 我们的新跟踪系统为Xsens套装提供了富有表现力的手指数据。使用…

微知-ib_write_bw的各种参数汇总(-d -q -s -R --run_infinitely)

背景 经常忘记使用ib_write_bw打流的一些参数&#xff0c;特此整理记录在这里方便快速查阅。尤其是run_infinitely这个参数容易写错。 最简洁 ib_write_bw -d mlx5_0 # server ib_write_bw -d mlx5_0 1.1.1.1 # client常用参数 非常常用 -d mlx5_0, --ib-dev 指定ib设备&a…

【踩坑】修复报错liblibcrypto.a找不到

转载请注明出处&#xff1a;小锋学长生活大爆炸[xfxuezhagn.cn] 如果本文帮助到了你&#xff0c;欢迎[点赞、收藏、关注]哦~ 报错示例 File "/usr/local/ssr/ss/crypto/util.py", line 60, in find_library path ctypes.util.find_library(name) File "/…

“人工智能+高职”:VR虚拟仿真实训室的发展前景

在当今科技日新月异的时代&#xff0c;人工智能&#xff08;AI&#xff09;与虚拟现实&#xff08;VR&#xff09;技术的融合正逐步改变着各行各业&#xff0c;教育领域也不例外。特别是在高等职业教育&#xff08;简称“高职”&#xff09;体系中&#xff0c;VR虚拟仿真实训室…

什么是 C++ 中的模板特化和偏特化?如何进行模板特化和偏特化?

参考文献&#xff1a;C模板全特化&#xff0c;偏特化&#xff08;局部特化&#xff09;_c 偏特化-CSDN博客 C 模板特化与偏特化_模板偏特化-CSDN博客 模板特化 定义 模板特化是指为特定的模板参数类型提供一个专门的实现。当使用这个特定类型时&#xff0c;编译器会优先选择这…

Unity图形学之着色器之间传递参数

1.初始化 struct&#xff1a; UNITY_INITIALIZE_OUTPUT&#xff08;type,name&#xff09; Type: struct 名字 Name :变量的名字 struct Input{float2 uv_MainTex;float3 myColor;};half _Glossiness;half _Metallic;fixed4 _Color;void MyVertx(inout appdata_base v,out I…