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

[Swift]LeetCode468. 验证IP地址 | Validate IP Address,第1张

概述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

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) -> Bool12     {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 tokens19         {20             if !isValIDIPv4Token(token) {return false}21         }22         return true23     }24     25     func isValIDIPv4Token(_ token:String) -> Bool26     {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         else36         {37             return false38         }       39     }40     41     func isValIDIPv6(_ ip:String) -> Bool42     {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 tokens49         {50             if !isValIDIPv6Token(token) {return false}51         }52         return true53     }54     55         func isValIDIPv6Token(_ token:String) -> Bool56     {57         if token.count == 0 || token.count > 4 {return false}58         for c in token.characters59         {60             var isDigit:Bool = c.ascii >= 48 && c.ascii <= 5761             var isUppercaseAF:Bool = c.ascii >= 65 && c.ascii <= 7062             var isLowerCaseAF:Bool = c.ascii >= 97 && c.ascii <= 10263             if !(isDigit || isUppercaseAF || isLowerCaseAF)64             {65                 return false66             }67         }68         return true69     }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).unicodeScalars87             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         /// 不可前缀 054         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 <= 25561     }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 true72     }73 }
总结

以上是内存溢出为你收集整理的[Swift]LeetCode468. 验证IP地址 | Validate IP Address全部内容,希望文章能够帮你解决[Swift]LeetCode468. 验证IP地址 | Validate IP Address所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1020035.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存