Leetcode: Validate IP Address
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的更多相关文章
- [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 ...
- 【LeetCode】468. Validate IP Address 解题报告(Python)
[LeetCode]468. Validate IP Address 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
- LeetCode:Restore IP Address
93. Restore IP Addresses Given a string containing only digits, restore it by returning all possible ...
- [Leetcode] restore ip address 存储IP地址
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- [LeetCode] Restore IP Address [28]
题目 Given a string containing only digits, restore it by returning all possible valid IP address comb ...
- LeetCode "468. Validate IP Address"
it is all about corner-cases... class Solution(object): def validIP4(self, IP): def validNum4(s): tr ...
- [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 ...
- 468 Validate IP Address 验证IP地址
详见:https://leetcode.com/problems/validate-ip-address/description/ Java实现: class Solution { public St ...
- Java Regex match IP address
Reference: [1] https://www.mkyong.com/regular-expressions/how-to-validate-ip-address-with-regular-ex ...
随机推荐
- jvm中的年轻代 老年代 持久代 gc
虚拟机中的共划分为三个代:年轻代(Young Generation).老年代(Old Generation)和持久代(Permanent Generation).其中持久代主要存放的是Java类的类信 ...
- CodeForces 551E 分块
题目链接:http://codeforces.com/problemset/problem/551/E 题意:给定一个长度为N的序列. 有2个操作 1 l r v:序列第l项到第r项加v(区间加), ...
- java-集合2
浏览以下内容前,请点击并阅读 声明 下面对集中核心集合的接口分别总结 Collection接口 一般情况下,集合的实现类会有一个含有Collection类型为参数的构造器,可以由一个指定的集合类创建该 ...
- 冰球项目日志2-yjw
我们小组在12.31号进行了讨论,确定了基本的任务及分工,后面是元旦放假,大家没有做很多的东西,我也是把自己分工的部分方案想了下. 后面在1.3号,我们会再进行一次小组讨论,确定下最终的方案,然后进行 ...
- Qt写Activex插件 总结
最近写的插件功能基本完成,也遇到了一些坑,在这里记录一下. 我写的这个插件的js接口是仿造google earth的js接口,尽可能的达到与它的api一致.先从最简单的说起: 1. 导出接口中的flo ...
- jQuery常用插件
jQuery UI插件简介: jQuery UI是以 jQuery 为基础的开源 JavaScript 网页用户界面代码库.包含底层用户交互.动画.特效和可更换主题的可视控件.我们可以直接用它来构建具 ...
- *cf.4 贪心
D. Kostya the Sculptor time limit per test 3 seconds memory limit per test 256 megabytes input stand ...
- 将List下载到本地保存为Excel
直接附上代码 /// <summary> /// 将List保存为Excel /// </summary> /// <typeparam name="T&quo ...
- WPF整理-自定义一个扩展标记(custom markup extension)
"Markup extensions are used to extend the capabilities of XAML, by providing declarativeoperati ...
- eclipse SE增加Web开发插件;安装配置Apache
在eclipse SE版本点击"Help"--"Install New Software..."--"Work with"中输入" ...