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

    原题链接在这里: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 class Solution {
      2     public String validIPAddress(String IP) {
      3         if(IP == null){
      4             return "Neither";
      5         }
      6         
      7         IP = IP.trim();
      8         if(IP.length() == 0){
      9             return "Neither";
     10         }
     11         
     12         if(isIPv4(IP)){
     13             return "IPv4";
     14         }
     15         
     16         if(isIPv6(IP)){
     17             return "IPv6";
     18         }
     19         
     20         return "Neither";
     21     }
     22     
     23     private boolean isIPv4(String s){
     24         if(s.charAt(s.length()-1) == '.'){
     25             return false;
     26         }
     27         
     28         String [] parts = s.split("\.");
     29         if(parts.length != 4){
     30             return false;
     31         }
     32         
     33         for(String part : parts){
     34             if(!isLegalIPv4(part)){
     35                 return false;
     36             }
     37         }
     38         
     39         return true;
     40     }
     41     
     42     private boolean isLegalIPv4(String part){
     43         if(part == null || part.length() == 0){
     44             return false;
     45         }
     46         
     47         if(part.charAt(0) == '0' && part.length()>1){
     48             return false;
     49         }
     50         try{
     51             int n = Integer.valueOf(part);
     52             if(n<0 || n>255){
     53                 return false;
     54             }
     55             
     56             if(n==0 && !part.equals("0")){
     57                 return false;
     58             }
     59         }catch(NumberFormatException e){
     60             return false;
     61         }
     62         
     63         return true;
     64     }
     65     
     66     private boolean isIPv6(String s){
     67         if(s.charAt(s.length()-1) == ':'){
     68             return false;
     69         }
     70         
     71         String [] parts = s.split(":");
     72         if(parts.length != 8){
     73             return false;
     74         }
     75         
     76         for(String part : parts){
     77             if(!isLegalIPv6(part)){
     78                 return false;
     79             }
     80         }
     81         
     82         return true;
     83     }
     84     
     85     private boolean isLegalIPv6(String part){
     86         if(part.length() == 0 || part.length() > 4){
     87             return false;
     88         }
     89         
     90         for(char c : part.toCharArray()){            
     91             boolean isDigit = c>='0' && c<='9';
     92             boolean isUpper = c>='A' && c<='F';
     93             boolean isLower = c>='a' && c<='f';
     94             if(!(isDigit || isUpper || isLower)){
     95                 return false;
     96             }
     97         }
     98         
     99         return true;
    100     }
    101 }

    Reference: https://discuss.leetcode.com/topic/71444/java-simple-solution

  • 相关阅读:
    Azure 中 Linux 虚拟机的大小
    排查在 Azure 中创建、重启 Linux VM 或调整其大小时发生的分配故障
    如何在 Azure 中的 Linux 经典虚拟机上设置终结点
    针对通过 SSH 连接到 Azure Linux VM 时发生的失败、错误或被拒绝问题进行故障排除
    Linux 内核超时导致虚拟机无法正常启动
    Java并发编程(十三)同步容器类
    可以开发着玩一下的web项目
    org.tmatesoft.svn.core.SVNCancelException: svn: E200015: authentication canc
    FastDFS单机搭建以及java客户端Demo
    做前端(单纯页面和js)遇到的问题辑录(一)
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/6598008.html
Copyright © 2011-2022 走看看