-
Notifications
You must be signed in to change notification settings - Fork 956
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
49e7373
commit 33e3923
Showing
3 changed files
with
69 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
### 题目描述 | ||
|
||
这是 LeetCode 上的 **[1108. IP 地址无效化](https://leetcode.cn/problems/defanging-an-ip-address/solution/by-ac_oier-cndi/)** ,难度为**简单**。 | ||
|
||
Tag : 「模拟」 | ||
|
||
|
||
|
||
给你一个有效的 `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` 地址 | ||
|
||
--- | ||
|
||
### 模拟 | ||
|
||
根据题意进行模拟即可。 | ||
|
||
代码: | ||
```Java | ||
class Solution { | ||
public String defangIPaddr(String s) { | ||
StringBuilder sb = new StringBuilder(); | ||
int n = s.length(), idx = -1; | ||
while (++idx < n) { | ||
char c = s.charAt(idx); | ||
if (c == '.') sb.append('['); | ||
sb.append(c); | ||
if (c == '.') sb.append(']'); | ||
} | ||
return sb.toString(); | ||
} | ||
} | ||
``` | ||
* 时间复杂度:$O(n)$ | ||
* 空间复杂度:$O(n)$ | ||
|
||
--- | ||
|
||
### 最后 | ||
|
||
这是我们「刷穿 LeetCode」系列文章的第 `No.1108` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。 | ||
|
||
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。 | ||
|
||
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。 | ||
|
||
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters