加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

[Swift]LeetCode468. 验证IP地址 | Validate IP Address

发布时间:2020-12-14 05:05:02 所属栏目:百科 来源:网络整理
导读: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,sepa

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 或?IPv6 地址。

IPv4?地址由十进制数和点来表示,每个地址包含4个十进制数,其范围为?0 -?255,?用(".")分割。比如,172.16.254.1

同时,IPv4 地址内的数不会以 0 开头。比如,地址?172.16.254.01?是不合法的。

IPv6?地址由8组16进制的数字来表示,每组表示?16 比特。这些组数字通过 (":")分割。比如,??2001:0db8:85a3:0000:0000:8a2e:0370:7334?是一个有效的地址。而且,我们可以加入一些以 0 开头的数字,字母可以使用大写,也可以是小写。所以,?2001:db8:85a3:0:0:8A2E:0370:7334?也是一个有效的 IPv6 address地址 (即,忽略 0 开头,忽略大小写)。

然而,我们不能因为某个组的值为 0,而使用一个空的组,以至于出现 (::) 的情况。?比如,?2001:0db8:85a3::8A2E:0370:7334是无效的 IPv6 地址。

同时,在 IPv6 地址中,多余的 0 也是不被允许的。比如,?02001:0db8:85a3:0000:0000:8a2e:0370:7334?是无效的。

说明:?你可以认为给定的字符串里没有空格或者其他特殊字符。

示例 1:

输入: "172.16.254.1"

输出: "IPv4"

解释: 这是一个有效的 IPv4 地址,所以返回 "IPv4"。

示例 2:

输入: "2001:0db8:85a3:0:0:8A2E:0370:7334"

输出: "IPv6"

解释: 这是一个有效的 IPv6 地址,所以返回 "IPv6"。

示例 3:

输入: "256.256.256.256"

输出: "Neither"

解释: 这个地址既不是 IPv4 也不是 IPv6 地址。

12ms
 1 class Solution {
 2     func validIPAddress(_ IP: String) -> String {
 3         if isValidIPv4(IP) {return "IPv4"}
 4         else if isValidIPv6(IP) {return "IPv6"}
 5         else 
 6         {
 7             return "Neither"
 8         }
 9     }
10     
11     func isValidIPv4(_ ip:String) -> Bool
12     {
13         if ip.count < 7 {return false}
14         if ip[0] == "." {return false}
15         if ip[ip.count-1] == "." {return false}
16         var tokens:[String] = ip.components(separatedBy:".")
17         if tokens.count != 4  {return false}
18         for token in tokens
19         {
20             if !isValidIPv4Token(token) {return false}
21         }
22         return true
23     }
24     
25     func isValidIPv4Token(_ token:String) -> Bool
26     {
27         if token.hasPrefix("0") && token.count > 1 {return false}
28         
29         if let parsedInt = Int(token)
30         {
31             if parsedInt < 0 || parsedInt > 255 {return false}
32             if parsedInt==0 && token[0] != "0" {return false}
33             return true            
34         }
35         else
36         {
37             return false
38         }       
39     }
40     
41     func isValidIPv6(_ ip:String) -> Bool
42     {
43         if ip.count < 15 {return false}
44         if ip[0] == ":" {return false}
45         if ip[ip.count-1] == ":" {return false}
46         var tokens:[String] = ip.components(separatedBy:":")
47         if tokens.count != 8  {return false}
48         for token in tokens
49         {
50             if !isValidIPv6Token(token) {return false}
51         }
52         return true
53     }
54     
55         func isValidIPv6Token(_ token:String) -> Bool
56     {
57         if token.count == 0 || token.count > 4 {return false}
58         for c in token.characters
59         {
60             var isDigit:Bool = c.ascii >= 48 && c.ascii <= 57
61             var isUppercaseAF:Bool = c.ascii >= 65 && c.ascii <= 70
62             var isLowerCaseAF:Bool = c.ascii >= 97 && c.ascii <= 102
63             if !(isDigit || isUppercaseAF || isLowerCaseAF)
64             {
65                 return false
66             }
67         }
68         return true
69     }
70 }
71 
72 extension String {        
73     //subscript函数可以检索数组中的值
74     //直接按照索引方式截取指定索引的字符
75     subscript (_ i: Int) -> Character {
76         //读取字符
77         get {return self[index(startIndex,offsetBy: i)]}
78     }
79 }
80 
81 extension Character  
82 {  
83   //属性:ASCII整数值(定义小写为整数值)
84    var ascii: Int {
85         get {
86             let s = String(self).unicodeScalars
87             return Int(s[s.startIndex].value)
88         }
89     }
90 }

12ms

 1 class Solution {
 2     func validIPAddress(_ IP: String) -> String {
 3         
 4         let ipv4 = IP.components(separatedBy: ".")
 5         
 6         if ipv4.count == 4 {
 7             
 8             return validIPv4Adress(ipv4)
 9         }
10         
11         let ipv6 = IP.components(separatedBy: ":")
12         
13         if ipv6.count == 8 {
14             
15             return validIPv6Adress(ipv6)
16         }
17         
18         return "Neither"
19     }
20     
21     /// IP v4 检验
22     private func validIPv4Adress(_ ips: [String]) -> String{
23         
24         for ip in ips {
25             
26             if !ip.isValidIPv4() {
27                 return "Neither"
28             }
29         }
30         
31         return "IPv4"
32     }
33     
34     
35     /// IP v6 检验
36     private func validIPv6Adress(_ ips: [String]) -> String{
37         
38         for ip in ips {
39             
40             if !ip.isValidIPv6() {
41                 return "Neither"
42             }
43         }
44         
45         return "IPv6"
46     }
47 }
48 
49 extension String{
50     
51     func isValidIPv4() -> Bool{
52         
53         /// 不可前缀 0
54         if self.count > 1 && self.hasPrefix("0") { return false }
55         
56         guard let ip = UInt(self) else { return false }
57         
58         if ip == 0 && self.contains("-") { return false }
59         
60         return ip <= 255
61     }
62     
63     func isValidIPv6() -> Bool{
64         
65         if self.count > 4 { return false }
66         
67         guard let ip = UInt(self,radix: 16) else { return false }
68         
69         if ip == 0 && self.contains("-") { return false }
70         
71         return true
72     }
73 }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读