mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2026-03-13 10:02:05 +08:00
添加 problem 1108
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
package leetcode
|
||||
|
||||
import "strings"
|
||||
|
||||
func defangIPaddr(address string) string {
|
||||
return strings.Replace(address, ".", "[.]", -1)
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question1108 struct {
|
||||
para1108
|
||||
ans1108
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para1108 struct {
|
||||
one string
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans1108 struct {
|
||||
one string
|
||||
}
|
||||
|
||||
func Test_Problem1108(t *testing.T) {
|
||||
|
||||
qs := []question1108{
|
||||
|
||||
question1108{
|
||||
para1108{"1.1.1.1"},
|
||||
ans1108{"1[.]1[.]1[.]1"},
|
||||
},
|
||||
|
||||
question1108{
|
||||
para1108{"255.100.50.0"},
|
||||
ans1108{"255[.]100[.]50[.]0"},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 1108------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans1108, q.para1108
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, defangIPaddr(p.one))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
||||
39
Algorithms/1108. Defanging an IP Address/README.md
Executable file
39
Algorithms/1108. Defanging an IP Address/README.md
Executable file
@@ -0,0 +1,39 @@
|
||||
# [1108. Defanging an IP Address](https://leetcode.com/problems/defanging-an-ip-address/)
|
||||
|
||||
|
||||
## 题目:
|
||||
|
||||
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 地址,其实就是用 "[.]" 代替了每个 "."。
|
||||
|
||||
|
||||
提示:
|
||||
|
||||
- 给出的 address 是一个有效的 IPv4 地址
|
||||
|
||||
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 给出一个 IP 地址,要求把点替换成 `[.]`。
|
||||
- 简单题,按照题意替换即可。
|
||||
Reference in New Issue
Block a user