zoukankan      html  css  js  c++  java
  • [LeetCode] 468. Validate IP Address

    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.

    验证IP地址。

    题意很简单。这个题需要验证两种IP地址,IPv4和IPv6。两者的区别如下

    IPv4

    • 有三个点
    • 三个点把IP地址分成了四段
    • 每一段地址长度不超过4
    • 每一段是一个介于0 - 255之间的纯数字,不包含leading zero

    IPv6

    • 有七个冒号
    • 七个冒号把IP地址分成了八段
    • 每一段地址长度不超过4
    • 每一段可以由数字和字母的组合构成,数字的范围是在0 - 9,字母的范围是在a - f和A - F之间

    有了这些规则,代码就会比较容易实现了,需要多练。这道题有一个环节稍微麻烦一点,就是如何判断数字是否有leading zero的情形,注意代码37行,这里Java利用了string和integer的相互转换,这样如果有leading zero的话,integer转string之后就会跟原来的string不一样了。

    时间O(n) - input length

    空间O(1)

    Java实现

     1 class Solution {
     2     public String validIPAddress(String IP) {
     3         if (isIPv4(IP)) {
     4             return "IPv4";
     5         }
     6         if (isIPv6(IP)) {
     7             return "IPv6";
     8         }
     9         return "Neither";
    10     }
    11 
    12     private boolean isIPv4(String IP) {
    13         int count = 0;
    14         for (char ch : IP.toCharArray()) {
    15             if (ch == '.') {
    16                 count++;
    17             }
    18         }
    19         if (count != 3) {
    20             return false;
    21         }
    22 
    23         String[] fields = IP.split("\.");
    24         if (fields.length != 4) {
    25             return false;
    26         }
    27         for (String field : fields) {
    28             if (field.isEmpty() || field.length() > 3) {
    29                 return false;
    30             }
    31             for (int i = 0; i < field.length(); i++) {
    32                 if (!Character.isDigit(field.charAt(i))) {
    33                     return false;
    34                 }
    35             }
    36             int num = Integer.valueOf(field);
    37             if (!String.valueOf(num).equals(field) || num < 0 || num > 255) {
    38                 return false;
    39             }
    40         }
    41         return true;
    42     }
    43 
    44     private boolean isIPv6(String IP) {
    45         if (IP.length() < 15 || IP.charAt(0) == ':' || IP.charAt(IP.length() - 1) == ':') {
    46             return false;
    47         }
    48         String[] strings = IP.split(":");
    49         if (strings.length != 8) {
    50             return false;
    51         }
    52         for (String string : strings) {
    53             if (string.length() == 0 || string.length() > 4) {
    54                 return false;
    55             }
    56             for (int i = 0; i < string.length(); i++) {
    57                 char c = string.charAt(i);
    58                 if (!((c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F') || (c >= '0' && c <= '9'))) {
    59                     return false;
    60                 }
    61             }
    62         }
    63         return true;
    64     }
    65 }

    LeetCode 题目总结

  • 相关阅读:
    vue的动画组件(transition)
    vue组件的那些事($children,$refs,$parent)的使用
    vue怎么自定义指令??
    组件之间的通信:父子组件之间的通讯(子组件-改变父组件的值)
    vue的路由初识01
    总结css的使用技巧
    crontab 定时任务
    Linux crontab 命令详解
    python-面向对象
    /var/spool/postfix/maildrop 占用inode索引及磁盘空间解决办法
  • 原文地址:https://www.cnblogs.com/cnoodle/p/13150161.html
Copyright © 2011-2022 走看看