• 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

  • 相关阅读:
    翻译:Razor剖析之第4部分:Razor页面
    学习第二十二天
    jQuery:选择器和事件
    学习第二十天@简单json+上传文件+Ado存储过程
    统计指定时间段内的周未(非周未)天数
    c#动态创建内存模型(笔记)
    cmd命令 任务计划 详解
    BAT教程:第四节(批处理中的变量)
    103个Windows XP运行命令
    BAT教程 :第二节(for命令详解 )
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/6598008.html
Copyright © 2020-2023  润新知