From ece12e0d8b305bab2bafa899a61268ac16fe1a34 Mon Sep 17 00:00:00 2001 From: AC_Oier Date: Tue, 27 Sep 2022 09:45:26 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8feat:=20add=20=E9=9D=A2=E8=AF=95?= =?UTF-8?q?=E9=A2=98=2001.02?= 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\344\270\255\347\255\211\357\274\211.md" | 12 +-- ...10\347\256\200\345\215\225\357\274\211.md" | 82 +++++++++++++++++++ 3 files changed, 89 insertions(+), 6 deletions(-) create mode 100644 "LeetCode/\351\235\242\350\257\225\351\242\230/\351\235\242\350\257\225\351\242\230 01.02. \345\210\244\345\256\232\346\230\257\345\220\246\344\272\222\344\270\272\345\255\227\347\254\246\351\207\215\346\216\222\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 78243443..0dbaa5cc 100644 --- "a/Index/\346\250\241\346\213\237.md" +++ "b/Index/\346\250\241\346\213\237.md" @@ -205,6 +205,7 @@ | [2043. 简易银行系统](https://leetcode-cn.com/problems/simple-bank-system/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/simple-bank-system/solution/by-ac_oier-9pqi/) | 中等 | 🤩🤩🤩🤩 | | [2047. 句子中的有效单词数](https://leetcode-cn.com/problems/number-of-valid-words-in-a-sentence/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/number-of-valid-words-in-a-sentence/solution/gong-shui-san-xie-jian-dan-zi-fu-chuan-m-5pcz/) | 简单 | 🤩🤩🤩🤩 | | [2069. 模拟行走机器人 II](https://leetcode-cn.com/problems/walking-robot-simulation-ii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/walking-robot-simulation-ii/solution/by-ac_oier-6zib/) | 中等 | 🤩🤩🤩🤩 | +| [面试题 01.02. 判定是否互为字符重排](https://leetcode.cn/problems/check-permutation-lcci/) | [LeetCode 题解链接](https://leetcode.cn/problems/check-permutation-lcci/solution/by-ac_oier-qj3j/) | 简单 | 🤩🤩🤩 | | [面试题 01.05. 一次编辑](https://leetcode.cn/problems/one-away-lcci/) | [LeetCode 题解链接](https://leetcode.cn/problems/one-away-lcci/solution/by-ac_oier-7ml0/) | 中等 | 🤩🤩🤩🤩 | | [面试题 10.02. 变位词组](https://leetcode-cn.com/problems/group-anagrams-lcci/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/group-anagrams-lcci/solution/gong-shui-san-xie-tong-ji-bian-wei-ci-de-0iqe/) | 中等 | 🤩🤩🤩🤩 | | [面试题 17.11. 单词距离](https://leetcode.cn/problems/find-closest-lcci/) | [LeetCode 题解链接](https://leetcode.cn/problems/find-closest-lcci/solution/by-ac_oier-0hv9/) | 中等 | 🤩🤩🤩🤩 | diff --git "a/LeetCode/371-380/373. \346\237\245\346\211\276\345\222\214\346\234\200\345\260\217\347\232\204K\345\257\271\346\225\260\345\255\227\357\274\210\344\270\255\347\255\211\357\274\211.md" "b/LeetCode/371-380/373. \346\237\245\346\211\276\345\222\214\346\234\200\345\260\217\347\232\204K\345\257\271\346\225\260\345\255\227\357\274\210\344\270\255\347\255\211\357\274\211.md" index 219480cc..50299658 100644 --- "a/LeetCode/371-380/373. \346\237\245\346\211\276\345\222\214\346\234\200\345\260\217\347\232\204K\345\257\271\346\225\260\345\255\227\357\274\210\344\270\255\347\255\211\357\274\211.md" +++ "b/LeetCode/371-380/373. \346\237\245\346\211\276\345\222\214\346\234\200\345\260\217\347\232\204K\345\257\271\346\225\260\345\255\227\357\274\210\344\270\255\347\255\211\357\274\211.md" @@ -78,7 +78,7 @@ $$ 可通过「反证法」证明,每次这样的「取当前,放入下一位」的操作,可以确保当前未被加入答案的所有点对的最小值必然在优先队列(堆)中,即前 $k$ 个出堆的元素必然是所有点对的前 $k$ 小的值。 -**代码(感谢 [@Benhao](/u/himymben/) 同学提供的其他语言版本):** +Java 代码: ```Java class Solution { boolean flag = true; @@ -101,7 +101,7 @@ class Solution { } } ``` -- +Python3 代码: ```Python3 class Solution: def kSmallestPairs(self, nums1: List[int], nums2: List[int], k: int) -> List[List[int]]: @@ -118,7 +118,7 @@ class Solution: heapq.heappush(pq, (nums1[a] + nums2[b + 1], a, b + 1)) return ans ``` -- +Golang 代码: ```Golang func kSmallestPairs(nums1 []int, nums2 []int, k int) [][]int { n, m, ans := len(nums1), len(nums2), [][]int{} @@ -156,7 +156,7 @@ func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } func (h *hp) Push(v interface{}) { *h = append(*h, v.([]int)) } func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v } ``` -* 时间复杂度:令 $M$ 为 $n$、$m$ 和 $k$ 三者中的最小值,复杂度为 $O(M + k) * \log{M})$ +* 时间复杂度:令 $M$ 为 $n$、$m$ 和 $k$ 三者中的最小值,复杂度为 $O(M + k) \times \log{M})$ * 空间复杂度:$O(M)$ --- @@ -182,7 +182,7 @@ func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; 最后,在所有处理过程中,我们都可以利用答案数组的大小与 $k$ 的关系做剪枝。 -代码: +Java 代码: ```Java class Solution { int[] nums1, nums2; @@ -245,7 +245,7 @@ class Solution { } } ``` -* 时间复杂度:假设点对和的值域大小范围为 $M$,第一次二分的复杂度为 $O((n * m) * \log{M})$;统计点对和值小于目标值 $x$ 的复杂度为 $O(n * m)$;统计所有点对和等于目标值的复杂度为 $O(\max(n * \log{m}, k))$(整个处理过程中利用了大小关系做了剪枝,大多循环都不会跑满,实际计算量会比理论分析的要低) +* 时间复杂度:假设点对和的值域大小范围为 $M$,第一次二分的复杂度为 $O((n \times m) \times \log{M})$;统计点对和值小于目标值 $x$ 的复杂度为 $O(n \times m)$;统计所有点对和等于目标值的复杂度为 $O(\max(n \times \log{m}, k))$(整个处理过程中利用了大小关系做了剪枝,大多循环都不会跑满,实际计算量会比理论分析的要低) * 空间复杂度:$O(k)$ --- diff --git "a/LeetCode/\351\235\242\350\257\225\351\242\230/\351\235\242\350\257\225\351\242\230 01.02. \345\210\244\345\256\232\346\230\257\345\220\246\344\272\222\344\270\272\345\255\227\347\254\246\351\207\215\346\216\222\357\274\210\347\256\200\345\215\225\357\274\211.md" "b/LeetCode/\351\235\242\350\257\225\351\242\230/\351\235\242\350\257\225\351\242\230 01.02. \345\210\244\345\256\232\346\230\257\345\220\246\344\272\222\344\270\272\345\255\227\347\254\246\351\207\215\346\216\222\357\274\210\347\256\200\345\215\225\357\274\211.md" new file mode 100644 index 00000000..5cd453b5 --- /dev/null +++ "b/LeetCode/\351\235\242\350\257\225\351\242\230/\351\235\242\350\257\225\351\242\230 01.02. \345\210\244\345\256\232\346\230\257\345\220\246\344\272\222\344\270\272\345\255\227\347\254\246\351\207\215\346\216\222\357\274\210\347\256\200\345\215\225\357\274\211.md" @@ -0,0 +1,82 @@ +### 题目描述 + +这是 LeetCode 上的 **[面试题 01.02. 判定是否互为字符重排](https://leetcode.cn/problems/check-permutation-lcci/solution/by-ac_oier-qj3j/)** ,难度为 **简单**。 + +Tag : 「模拟」 + + + +给定两个字符串 `s1` 和 `s2`,请编写一个程序,确定其中一个字符串的字符重新排列后,能否变成另一个字符串。 + +示例 1: +``` +输入: s1 = "abc", s2 = "bca" + +输出: true +``` +示例 2: +``` +输入: s1 = "abc", s2 = "bad" + +输出: false +``` + +说明: +* $0 <= len(s1) <= 100$ +* $0 <= len(s2) <= 100$ + +--- + +### 模拟 + +根据题意,对两字符串进行词频统计,统计过程中使用变量 `tot` 记录词频不同的字符个数。 + +Java 代码: +```Java +class Solution { + public boolean CheckPermutation(String s1, String s2) { + int n = s1.length(), m = s2.length(), tot = 0; + if (n != m) return false; + int[] cnts = new int[128]; + for (int i = 0; i < n; i++) { + if (++cnts[s1.charAt(i)] == 1) tot++; + if (--cnts[s2.charAt(i)] == 0) tot--; + } + return tot == 0; + } +} +``` +TypeScript 代码: +```TypeScript +function CheckPermutation(s1: string, s2: string): boolean { + let n = s1.length, m = s2.length, tot = 0 + if (n != m) return false + const cnts = new Array(128).fill(0) + for (let i = 0; i < n; i++) { + if (++cnts[s1.charCodeAt(i)] == 1) tot++ + if (--cnts[s2.charCodeAt(i)] == 0) tot-- + } + return tot == 0 +}; +``` +Python3 代码: +```Python3 +class Solution: + def CheckPermutation(self, s1: str, s2: str) -> bool: + return Counter(s1) == Counter(s2) +``` +* 时间复杂度:$O(n)$ +* 空间复杂度:$O(C)$,其中 $C = 128$ 为字符集大小 + +--- + +### 最后 + +这是我们「刷穿 LeetCode」系列文章的第 `No.面试题 01.02` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。 + +在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。 + +为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。 + +在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。 +