★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-qhqkfjts-ky.html
➤若是连接不是山青咏芝的博客园地址,则多是爬取做者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持做者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
Given a valid (IPv4) IP address
, return a defanged version of that IP address.git
A defanged IP address replaces every period "."
with "[.]"
. github
Example 1:微信
Input: address = "1.1.1.1" Output: "1[.]1[.]1[.]1"
Example 2:app
Input: address = "255.100.50.0" Output: "255[.]100[.]50[.]0"
Constraints:spa
address
is a valid IPv4 address. 给你一个有效的 IPv4 地址 address
,返回这个 IP 地址的无效化版本。code
所谓无效化 IP 地址,其实就是用 "[.]"
代替了每一个 "."
。 htm
示例 1:blog
输入:address = "1.1.1.1" 输出:"1[.]1[.]1[.]1"
示例 2:ci
输入:address = "255.100.50.0" 输出:"255[.]100[.]50[.]0"
提示:
address
是一个有效的 IPv4 地址 1 class Solution { 2 func defangIPaddr(_ address: String) -> String { 3 return address.replacingOccurrences(of: ".", with: "[.]") 4 } 5 }
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 }