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. sql事务(Transaction)用法介绍及回滚实例

    sql事务(Transaction)用法介绍及回滚实例 事务(Transaction)是并发控制的单位,是用户定义的一个操作序列.这些操作要么都做,要么都不做,是一个不可分割的工作单位.通过事务, S ...

  2. iOS 从git拷贝Xcode的snippets

    do following things in terminal 1. check out the project using: git clone gitAddress 2. cd the proje ...

  3. [转] 《ES6标准入门》读书笔记

    来源:https://segmentfault.com/a/1190000005863641 let和const命令 ES6新增let命令,用于声明变量,是块级作用域. let声明的变量不会像var声 ...

  4. Xcode开发中 Code Snippets Library 的相关用法

    当在进行项目的时候,总会遇到很多相同的写法.因此,我们可以使用Code Snippets Library 来进行代码小片段的“封装”: 以Xcode中常用的属性为例: 使用步骤如下: 1.在Xcode ...

  5. IBatis添加信息返当前添加对象ID

      在Ibatis中,insert()的返回值为一个Object的主键,其实这个Object的主键是这样的来的:如果在bean的xml文件中设置了插入的keyProperty,则insert()方法返 ...

  6. 使用MS Test进行单元测试

    MS Test也可以方便的进行单元测试,可以通过Visual Studio很方便的建立单元测试. 添加对待测试工程的引用,即可方便的开始单元测试. 最基本的一些测试使用如下: using System ...

  7. Model & ModelMap & ModelAndView 比较ModelFactory简介

    Model: 是一个接口,其实现类必继承ModelMap. ModelMap: 继承与LinkedHashMap,相当于自定义了一个map. ModelAndView: 里面封装了两个对象,其中vie ...

  8. ansible-copy

    ansible是执行yaml文件控制远端服务器.执行命令为 ansible-playbook my.yamlplaybook是ansible中的脚本,采用yaml语言.VM1安装ansible,ip地 ...

  9. js获取modelandview的值

    JS当中不能接收ModelAndView的返回值吗?一定要在JSP页面中才能接收吗? 1 方法一 [有效?] 可以的,跟el表达式访问方式一样. 示例代码,一个数据展示请求的Action中存入一个us ...

  10. js window.onload 的一个验证

    window.onload必须等到页面内包括图片的所有元素加载完毕后才能执行. 以下验证是否是图片加载完成后才执行 <img class="icon" id="ic ...