[Swift]LeetCode468. 驗證IP地址 | Validate IP Address

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-edyfwrho-mb.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

Write a function to check whether an input string is a valid IPv4 address or IPv6 address or neither.git

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;github

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.ide

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.spa

Example 1:code

Input: "172.16.254.1"

Output: "IPv4"

Explanation: This is a valid IPv4 address, return "IPv4". 

Example 2:component

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 }
相關文章
相關標籤/搜索