In this problem, your job to write a function to check whether a input string is a valid IPv4 address or IPv6 address or neither.

IPv4 addresses are canonically represented in dot-decimal notation, which consists of four decimal numbers, each ranging from 0 to 255, separated by dots ("."), e.g.,172.16.254.1;

Besides, you need to keep in mind that leading zeros in the IPv4 is illegal. For example, the address 172.16.254.01 is illegal.

IPv6 addresses are represented as eight groups of four hexadecimal digits, each group representing 16 bits. The groups are separated by colons (":"). For example, the address 2001:0db8:85a3:0000:0000:8a2e:0370:7334 is a legal one. Also, we could omit some leading zeros among four hexadecimal digits and some low-case characters in the address to upper-case ones, so 2001:db8:85a3:0:0:8A2E:0370:7334 is also a valid IPv6 address(Omit leading zeros and using upper cases).

However, we don't replace a consecutive group of zero value with a single empty group using two consecutive colons (::) to pursue simplicity. For example, 2001:0db8:85a3::8A2E:0370:7334 is an invalid IPv6 address.

Besides, you need to keep in mind that extra leading zeros in the IPv6 is also illegal. For example, the address 02001:0db8:85a3:0000:0000:8a2e:0370:7334 is also illegal.

Note: You could assume there is no extra space in the test cases and there may some special characters in the input string.

Example 1:
Input: "172.16.254.1" Output: "IPv4" Explanation: This is a valid IPv4 address, return "IPv4".
Example 2:
Input: "2001:0db8:85a3:0:0:8A2E:0370:7334" Output: "IPv6" Explanation: This is a valid IPv6 address, return "IPv6".
Example 3:
Input: "256.256.256.256" Output: "Neither" Explanation: This is neither a IPv4 address nor a IPv6 address.

Be careful, split() will not include trailing empty strings in the result array

The string "boo:and:foo", for example, split(":")的结果是 {“boo”, "and", "foo"}; Trailing empty strings are not included in the resulting array.比如,split("o")的结果是{“b”, "", ":and:f"}

If there exist extra ":" or "." at the end of the string, directly return false

 public class Solution {
public String validIPAddress(String IP) {
if (IP==null || IP.length()==0) return "Neither";
boolean isIP4 = checkIP4(IP);
boolean isIP6 = checkIP6(IP);
if (isIP4) return "IPv4";
if (isIP6) return "IPv6";
return "Neither";
} public boolean checkIP4(String IP) {
if (IP.charAt(IP.length()-1) == '.') return false;
String[] numbers = IP.split("\\.");
if (numbers==null || numbers.length!=4) return false;
for (String str : numbers) {
if (str.length()==0 || str.length()>3) return false;
if (str.length()>1 && str.charAt(0)=='0') return false;
int val = 0;
for (int i=0; i<str.length(); i++) {
char c = str.charAt(i);
if (c<'0' || c>'9') return false;
val = val*10 + (int)(c-'0');
}
if (val<0 || val>255) return false;
}
return true;
} public boolean checkIP6(String IP) {
if (IP.charAt(IP.length()-1) == ':') return false;
String[] numbers = IP.split(":");
if (numbers==null || numbers.length!=8) return false;
for (String str : numbers) {
if (str.length()==0 || str.length()>4) return false;
int i = 0;
while (i < str.length()) {
char c = str.charAt(i++);
if ((c<'0' || c>'9') && (c<'a' || c>'f') && (c<'A' || c>'F')) return false;
}
}
return true;
}
}

Leetcode: Validate IP Address的更多相关文章

  1. [LeetCode] Validate IP Address 验证IP地址

    In this problem, your job to write a function to check whether a input string is a valid IPv4 addres ...

  2. 【LeetCode】468. Validate IP Address 解题报告(Python)

    [LeetCode]468. Validate IP Address 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...

  3. LeetCode:Restore IP Address

    93. Restore IP Addresses Given a string containing only digits, restore it by returning all possible ...

  4. [Leetcode] restore ip address 存储IP地址

    Given a string containing only digits, restore it by returning all possible valid IP address combina ...

  5. [LeetCode] Restore IP Address [28]

    题目 Given a string containing only digits, restore it by returning all possible valid IP address comb ...

  6. LeetCode "468. Validate IP Address"

    it is all about corner-cases... class Solution(object): def validIP4(self, IP): def validNum4(s): tr ...

  7. [Swift]LeetCode468. 验证IP地址 | Validate IP Address

    Write a function to check whether an input string is a valid IPv4 address or IPv6 address or neither ...

  8. 468 Validate IP Address 验证IP地址

    详见:https://leetcode.com/problems/validate-ip-address/description/ Java实现: class Solution { public St ...

  9. Java Regex match IP address

    Reference: [1] https://www.mkyong.com/regular-expressions/how-to-validate-ip-address-with-regular-ex ...

随机推荐

  1. psql-10权限和事务

    用户和角色 用户和角色在整个数据库实例中都是全局的;

  2. 首师大附中互测题:LJX的校园:入学典礼【C003】

    [C003]LJX的校园:入学典礼[难度C]—————————————————————————————————————————————————————————————————————————————— ...

  3. android--handler

    app在启动时会产生一个进程和一个线程,线程是主线程,又叫UI线程,更新UI元素必须要在UI线程中更新,否则会报错. 在UI线程中有消息队列,子线程sendMessage到MQ中,looper类取出队 ...

  4. Spring Autowired 注入失败总是Null

    报错:NullPointerException 分析:错误原因是注入失败? <context:annotation-config/> <context:component-scan ...

  5. Gitbook简易教程

    简介 GitBook 是一个基于 Node.js 的命令行工具,可使用 Github/Git 和 Markdown 来制作精美的电子书.GitBook支持输出以下几种文档格式 静态站点:GitBook ...

  6. 事后分析报告(Postmortem Report)

    小组讨论照片 设想和目标 1.我们的团队项目为英语单词学习助手,名为“我爱记单词”.主要提供服务包括:单词查询,单词测试,单词记忆和中英互译.目前开发的是单机版本,用户可以根据自己的需求灵活的使用相应 ...

  7. Bean不同配置方式的比较

    在<Spring3.x 企业应用开发实战>上学习了Bean的三种不同配置方法,下图是我从书中截取的图片,比较了一下这三种配置的异同 ps:发现图片不能完全显示(右侧有一块不显示),解决方法 ...

  8. Html简单介绍

    一.Html 1.万维网的核心语言,高大上称之为超文本标记语言(Html)的第五次修改 2.完成的时间:2014年10月29日 3.我们需要了解有一定高度的知识: WHATWG  WEB超文本应用技术 ...

  9. 学习笔记_springmvc返回值、数据写到页面、表单提交、ajax、重定向

    数据写到页面 后台往前台传数据 TestController添加 /** * 方法的返回值采用ModelAndView, new ModelAndView("index", map ...

  10. 面试习题之设计模式 C#观察者模式(猫叫老鼠惊走主人醒)

    腾讯云测试|TEST Tencent Cloud /* * CatShout.cs */ using System; using System.IO; using System.Collections ...