From 33e3923bb7933db9d220bfad2b01a0297c26fb9a Mon Sep 17 00:00:00 2001 From: AC_Oier Date: Tue, 21 Jun 2022 10:50:02 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8feat:=20add=201108?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "Index/\346\250\241\346\213\237.md" | 1 + ...10\347\256\200\345\215\225\357\274\211.md" | 65 +++++++++++++++++++ ...10\344\270\255\347\255\211\357\274\211.md" | 4 +- 3 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 "LeetCode/1101-1110/1108. IP \345\234\260\345\235\200\346\227\240\346\225\210\345\214\226\357\274\210\347\256\200\345\215\225\357\274\211.md" diff --git "a/Index/\346\250\241\346\213\237.md" "b/Index/\346\250\241\346\213\237.md" index 1a8768ea..4bdfe8db 100644 --- "a/Index/\346\250\241\346\213\237.md" +++ "b/Index/\346\250\241\346\213\237.md" @@ -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/) | 简单 | 🤩🤩🤩🤩 | diff --git "a/LeetCode/1101-1110/1108. IP \345\234\260\345\235\200\346\227\240\346\225\210\345\214\226\357\274\210\347\256\200\345\215\225\357\274\211.md" "b/LeetCode/1101-1110/1108. IP \345\234\260\345\235\200\346\227\240\346\225\210\345\214\226\357\274\210\347\256\200\345\215\225\357\274\211.md" new file mode 100644 index 00000000..aa67fadf --- /dev/null +++ "b/LeetCode/1101-1110/1108. IP \345\234\260\345\235\200\346\227\240\346\225\210\345\214\226\357\274\210\347\256\200\345\215\225\357\274\211.md" @@ -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 原题链接和其他优选题解。 + diff --git "a/LeetCode/1391-1400/1395. \347\273\237\350\256\241\344\275\234\346\210\230\345\215\225\344\275\215\346\225\260\357\274\210\344\270\255\347\255\211\357\274\211.md" "b/LeetCode/1391-1400/1395. \347\273\237\350\256\241\344\275\234\346\210\230\345\215\225\344\275\215\346\225\260\357\274\210\344\270\255\347\255\211\357\274\211.md" index 2b72cc39..42ee6674 100644 --- "a/LeetCode/1391-1400/1395. \347\273\237\350\256\241\344\275\234\346\210\230\345\215\225\344\275\215\346\225\260\357\274\210\344\270\255\347\255\211\357\274\211.md" +++ "b/LeetCode/1391-1400/1395. \347\273\237\350\256\241\344\275\234\346\210\230\345\215\225\344\275\215\346\225\260\357\274\210\344\270\255\347\255\211\357\274\211.md" @@ -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$ @@ -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]$ 作为三元组中点的合法方案数。