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 ...
随机推荐
- 首师大附中互测题:50229234海岛帝国:独立之战【C002】
[C002]50229234海岛帝国:独立之战[难度C]———————————————————————————————————————————————————————————————————————— ...
- js 字符串中的\n不会换行
var str1=aaaaaaa\nbbbbbbb; alert(str1); //不换行 ???不知所以然 解决办法: while (str1.indexOf("\\n") & ...
- 【转】如何提高意志力&如何坚持每天学习
第一篇如何提高意志力 有一种品质可以使一个人在碌碌无为的平庸之辈中脱颖而出,这个品质不是天资,不是教育,也不是智商,而是自律.有了自律,一切皆有可能,无,则连最简单的目标都显得遥不可及.–西奥多·罗斯 ...
- 我的jQuery源码读后感
(function(window, undefined) { // 构造jQuery对象 var jQuery = (function() { var jQuery = function(select ...
- Web服务器基础学习
1)Socket通信相当于两个人通过电话联系,Http协议相当于电话联系时所使用的中文2)Http1.1前均为短连接,1.1版本为长连接,即服务器接收一次请求并发送响应后会等待一段时间看浏览器是否在这 ...
- java分享第十三天(fastjson生成和解析json数据,序列化和反序列化数据)
fastjson简介:Fastjson是一个Java语言编写的高性能功能完善的JSON库.fastjson采用独创的算法,将parse的速度提升到极致,超过所有json库,包括曾经号称最快的jack ...
- java分享第七天-03(递归打印文件目录的树状结构)
public static void main(String[] args) { File file= new File("e:/list"); printFile(file, 0 ...
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) B. Batch Sort
链接 题意:输入n,m,表示一个n行m列的矩阵,每一行数字都是1-m,顺序可能是乱的,每一行可以交换任意2个数的位置,并且可以交换任意2列的所有数 问是否可以使每一行严格递增 思路:暴力枚举所有可能的 ...
- Swift与OC混编
OC调用Swift的方法:添加 import "xxxx-Swift.h" 头文件即可 Swift调用OC的方法:需要建立桥接: xxxx-Bridging-Header.h 头文 ...
- UI控件闪烁及刷新
我们常常在一个窗口上放置很多控件,在改变窗口大小时,控件会跟着一起闪烁... 此时,可以将窗口添加WS_CLIPCHILDREN属性即可.(如果包含多层,都需要WS_CLIPCHILDREN属性) 默 ...