[Swift]LeetCode1108. IP 地址无效化 | Defanging an IP Address
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/11112213.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
Given a valid (IPv4) IP address
, return a defanged version of that IP address.
A defanged IP address replaces every period "."
with "[.]"
.
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:
- The given
address
is a valid IPv4 address.
给你一个有效的 IPv4 地址 address
,返回这个 IP 地址的无效化版本。
所谓无效化 IP 地址,其实就是用 "[.]"
代替了每个 "."
。
示例 1:
输入:address = "1.1.1.1" 输出:"1[.]1[.]1[.]1"
示例 2:
输入:address = "255.100.50.0" 输出:"255[.]100[.]50[.]0"
提示:
- 给出的
address
是一个有效的 IPv4 地址
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 }