[Swift]LeetCode1108. IP 地址無效化 | Defanging an IP Address

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(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

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

  • The given 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 地址 

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