Skip to content

IP 地址无效化

仲灏2022-11-08约 1 分钟

IP 地址无效化

CategoryDifficultyLikesDislikes
algorithmsEasy (85.59%)122-

<details style="color: rgb(225, 228, 232); font-family: -apple-system, "system-ui", "Segoe WPC", "Segoe UI", system-ui, Ubuntu, "Droid Sans", sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><summary><strong>Tags</strong></summary><p style="margin-top: 0px; margin-bottom: 0.7em;"><a href="https://leetcode.com/tag/Unknown" title="https://leetcode.com/tag/Unknown" style="color: var(--vscode-textLink-foreground); text-decoration: none;"><code style="color: var(--vscode-textLink-foreground); font-family: var(--vscode-editor-font-family, "SF Mono", Monaco, Menlo, Consolas, "Ubuntu Mono", "Liberation Mono", "DejaVu Sans Mono", "Courier New", monospace); font-size: 1em; line-height: 1.357em; white-space: pre-wrap;"></code></a></p></details>

<details style="color: rgb(225, 228, 232); font-family: -apple-system, "system-ui", "Segoe WPC", "Segoe UI", system-ui, Ubuntu, "Droid Sans", sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><summary><strong>Companies</strong></summary><p style="margin-top: 0px; margin-bottom: 0.7em;"><code style="color: var(--vscode-textPreformat-foreground); font-family: var(--vscode-editor-font-family, "SF Mono", Monaco, Menlo, Consolas, "Ubuntu Mono", "Liberation Mono", "DejaVu Sans Mono", "Courier New", monospace); font-size: 1em; line-height: 1.357em; white-space: pre-wrap;"></code></p></details>

给你一个有效的 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 地址

Discussion | Solution

tsx
/*
 * @Author: 仲灏<izhaong@outlook.com>🌶🌶🌶
 * @Date: 2022-11-08 13:28:15
 * @LastEditTime: 2022-11-08 13:36:16
 * @LastEditors: 仲灏<izhaong@outlook.com>🌶🌶🌶
 * @Description:  
 * @FilePath: /loan-home/Users/izhaong/izhaong/Project_me/leetcode/1108.ip-地址无效化.ts
 */
/*
 * @lc app=leetcode.cn id=1108 lang=typescript
 *
 * [1108] IP 地址无效化
 *
 * https://leetcode.cn/problems/defanging-an-ip-address/description/
 *
 * algorithms
 * Easy (85.59%)
 * Likes:    122
 * Dislikes: 0
 * Total Accepted:    107.5K
 * Total Submissions: 125.6K
 * Testcase Example:  '"1.1.1.1"'
 *
 * 给你一个有效的 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 地址
 * 
 * 
 */

// @lc code=start
function defangIPaddr(address: string): string {
    // return address.replaceAll('.', '[.]')
    return address.split('\.').join('[.]')
};
// @lc code=end