在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ Given a valid (IPv4) IP A defanged IP address replaces every period Example 1: Input: address = "1.1.1.1" Output: "1[.]1[.]1[.]1" Example 2: Input: address = "255.100.50.0" Output: "255[.]100[.]50[.]0" Constraints:
给你一个有效的 IPv4 地址 所谓无效化 IP 地址,其实就是用 示例 1: 输入:address = "1.1.1.1" 输出:"1[.]1[.]1[.]1" 示例 2: 输入:address = "255.100.50.0" 输出:"255[.]100[.]50[.]0" 提示:
Runtime: 0 ms
Memory Usage: 21.7 MB
1 class Solution { 2 func defangIPaddr(_ address: String) -> String { 3 return address.replacingOccurrences(of: ".", with: "[.]") 4 } 5 } 4ms 1 class Solution { 2 func defangIPaddr(_ address: String) -> String { 3 var defanged = "" 4 5 address.forEach { char in 6 char == "." ? defanged.append("[.]") : defanged.append(char) 7 } 8 9 return defanged 10 } 11 } 8ms 1 class Solution { 2 func defangIPaddr(_ address: String) -> String { 3 let spilttedIP = address.split(separator: ".") 4 5 return spilttedIP.joined(separator: "[.]") 6 } 7 }
|
请发表评论