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. Log4net用法

    日记是我们在程序中经常用到的,故记于此 首先要下载Log4net.dll 官方网站:http://logging.apache.org/log4net/ vs里创建一个c#控制台程序,在App.con ...

  2. life_motto

    simple,dedicated,healthy life style,love those u love...

  3. linux下 html转pdf

    其实很简单的, 在当前文件夹中打开终端, 只需要一个命令就好 wkhtmltopdf test.html test.pdf 这样一个test.html的文件就转为test.pdf 的pdf文件啦!

  4. 将Matlab中的矩阵输出到txt文件

    将矩阵输出到txt文件中的方法,遍寻网络,始见真经!!! fid=fopen('C:Documents and Settingscleantotal.ped','wt');%写入文件路径 matrix ...

  5. spring自定义schema学习

    [转载请注明作者和原文链接,欢迎讨论,相互学习.] 一.前言 1. 最近在学习dubbo,里边很多如provider.consumer.registry的配置都是通过spring自定义Schema来实 ...

  6. c#内部类的使用

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  7. Hibernate学习笔记3

    ---恢复内容开始--- 一.hibernate如何转化jdbc代码实例[通过hibernate构建jdbc后往数据库传对象] import java.sql.Connection;import ja ...

  8. java并发编程(十)使用wait/notify/notifyAll实现线程间通信

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/17225469 wait()方法:public final void wait()  thr ...

  9. jvisualVM 分析heapdump

    代码很简单,eclipse里面设置下最大堆空间为128m,: @Test public void testOutOfMemory() { List<NewsAddDto> document ...

  10. LoadRunner - 001

    lr_eval_string():函数的主要作用:返回脚本中的一个参数当前的值,返回值类型:char一般多用在调试脚本时输出参数的值.具体用法如下:lr_output_message("Th ...