Skip to content

Commit

Permalink
✨feat: add 1108
Browse files Browse the repository at this point in the history
  • Loading branch information
SharingSource committed Jun 21, 2022
1 parent 49e7373 commit 33e3923
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
1 change: 1 addition & 0 deletions Index/模拟.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
| [1051. 高度检查器](https://leetcode.cn/problems/height-checker/) | [LeetCode 题解链接](https://leetcode.cn/problems/height-checker/solution/by-ac_oier-8xoj/) | 简单 | 🤩🤩🤩 |
| [1078. Bigram 分词](https://leetcode-cn.com/problems/occurrences-after-bigram/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/occurrences-after-bigram/solution/gong-shui-san-xie-jian-dan-zi-fu-chuan-m-qyki/) | 简单 | 🤩🤩🤩🤩 |
| [1104. 二叉树寻路](https://leetcode-cn.com/problems/path-in-zigzag-labelled-binary-tree/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/path-in-zigzag-labelled-binary-tree/solution/gong-shui-san-xie-yi-ti-shuang-jie-mo-ni-rw2d/) | 中等 | 🤩🤩🤩 |
| [1108. IP 地址无效化](https://leetcode.cn/problems/defanging-an-ip-address/) | [LeetCode 题解链接](https://leetcode.cn/problems/defanging-an-ip-address/solution/by-ac_oier-cndi/) | 简单 | 🤩🤩 |
| [1154. 一年中的第几天](https://leetcode-cn.com/problems/day-of-the-year/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/day-of-the-year/solution/gong-shui-san-xie-jian-dan-qian-zhui-he-lwo2g/) | 简单 | 🤩🤩🤩🤩 |
| [1185. 一周中的第几天](https://leetcode-cn.com/problems/day-of-the-week/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/day-of-the-week/solution/gong-shui-san-xie-jian-dan-ri-qi-tong-ji-czt6/) | 简单 | 🤩🤩🤩🤩 |
| [1189. “气球” 的最大数量](https://leetcode-cn.com/problems/maximum-number-of-balloons/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximum-number-of-balloons/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-9px4/) | 简单 | 🤩🤩🤩🤩 |
Expand Down
65 changes: 65 additions & 0 deletions LeetCode/1101-1110/1108. IP 地址无效化(简单).md
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 原题链接和其他优选题解。

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Tag : 「树状数组」、「容斥原理」

每 $3$ 个士兵可以组成一个作战单位,分组规则如下:

* 从队伍中选出下标分别为 `i``j``k` 的 $3$ 名士兵,他们的评分分别为 $rating[i]$、$rating[j]$、rating[k]$
* 从队伍中选出下标分别为 `i``j``k` 的 $3$ 名士兵,他们的评分分别为 $rating[i]$、$rating[j]$、$rating[k]$

* 作战单位需满足: $rating[i] < rating[j] < rating[k]$ 或者 $rating[i] > rating[j] > rating[k]$ ,其中  $0 <= i < j < k < n$

Expand Down Expand Up @@ -113,6 +113,8 @@ class Solution {

### 双树状数组优化 - 枚举中点

我们考虑将 $n$ 的数据范围提升到 $1e4$ 该如何做。

上述解法的瓶颈在于我们枚举三元组中的左右端点,复杂度为 $O(n^2)$,而实际上利用三元组必然递增或递减的特性,我们可以调整为枚举终点 $j$,从而将「枚举点对」调整为「枚举中点」,复杂度为 $O(n)$。

假设当前枚举到的点为 $rs[i]$,问题转换为在 $[0, i - 1]$ 有多少比 $rs[i]$ 小/大 的数,在 $[i + 1, n - 1]$ 有多少比 $rs[i]$ 大/小 的数,然后集合「乘法」原理即可知道 $rs[i]$ 作为三元组中点的合法方案数。
Expand Down

0 comments on commit 33e3923

Please sign in to comment.