您的位置:首页 > 其它

LeetCode Validate IP Address

2017-03-22 08:58 253 查看
原题链接在这里:https://leetcode.com/problems/validate-ip-address/#/description

题目:

Write a function to check whether an 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, leading zeros in the IPv4 is invalid. For example, the address
172.16.254.01
is invalid.

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 valid 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, extra leading zeros in the IPv6 is also invalid. For example, the address
02001:0db8:85a3:0000:0000:8a2e:0370:7334
is invalid.

Note: You may assume there is no extra space or 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.

题解:

先判定是不是IPv4, 看用 '.' 隔开的部分是否符合IPv4规则.

在判定是不是IPv6, 看用 ':' 隔开的部分是否符合IPv6规则.

若都不是就return "Neither".

Time Complexity: O(IP.length()). Space: O(1), split 用的数组.

AC Java:

1 public class Solution {
2     public String validIPAddress(String IP) {
3         if(isValidIPv4(IP)){
4             return "IPv4";
5         }else if(isValidIPv6(IP)){
6             return "IPv6";
7         }
8         return "Neither";
9     }
10
11     private boolean isValidIPv4(String ip){
12         if(ip.length()<7){
13             return false;
14         }
15         if(ip.charAt(0)=='.' || ip.charAt(ip.length()-1)=='.'){
16             return false;
17         }
18         String [] tokens = ip.split("\\.");
19         if(tokens.length != 4){
20             return false;
21         }
22         for(String token : tokens){
23             if(!(isValidIPv4Token(token))){
24                 return false;
25             }
26         }
27         return true;
28     }
29
30     private boolean isValidIPv4Token(String token){
31         if(token.startsWith("0") && token.length() > 1){
32             return false;
33         }
34         try{
35             int n = Integer.valueOf(token);
36             if(n<0 || n>255){
37                 return false;
38             }
39             if(n==0 && token.charAt(0)!='0'){
40                 return false;
41             }
42         }catch(NumberFormatException nfe){
43             return false;
44         }
45         return true;
46     }
47
48     private boolean isValidIPv6(String ip){
49         if(ip.length()<15){
50             return false;
51         }
52         if(ip.charAt(0)==':' || ip.charAt(ip.length()-1)==':'){
53             return false;
54         }
55         String [] tokens = ip.split(":");
56         if(tokens.length != 8){
57             return false;
58         }
59         for(String token : tokens){
60             if(!isValidIPv6Token(token)){
61                 return false;
62             }
63         }
64         return true;
65     }
66
67     private boolean isValidIPv6Token(String token){
68         if(token.length()>4 || token.length() == 0){
69             return false;
70         }
71         for(char c : token.toCharArray()){
72             boolean isDigit = c>=48 && c<=57;
73             boolean isUpper = c>=65 && c<=70;
74             boolean isLower = c>=97 && c<=102;
75             if(!(isDigit || isUpper || isLower)){
76                 return false;
77             }
78         }
79         return true;
80     }
81 }


Reference: https://discuss.leetcode.com/topic/71444/java-simple-solution
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: