Skip to content

Commit

Permalink
✨feat: add 面试题 01.02
Browse files Browse the repository at this point in the history
  • Loading branch information
SharingSource committed Sep 27, 2022
1 parent 55c359d commit ece12e0
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 6 deletions.
1 change: 1 addition & 0 deletions Index/模拟.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/) | 中等 | 🤩🤩🤩🤩 |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ $$

可通过「反证法」证明,每次这样的「取当前,放入下一位」的操作,可以确保当前未被加入答案的所有点对的最小值必然在优先队列(堆)中,即前 $k$ 个出堆的元素必然是所有点对的前 $k$ 小的值。

**代码(感谢 [@Benhao](/u/himymben/) 同学提供的其他语言版本):**
Java 代码:
```Java
class Solution {
boolean flag = true;
Expand All @@ -101,7 +101,7 @@ class Solution {
}
}
```
-
Python3 代码:
```Python3
class Solution:
def kSmallestPairs(self, nums1: List[int], nums2: List[int], k: int) -> List[List[int]]:
Expand All @@ -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{}
Expand Down Expand Up @@ -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)$

---
Expand All @@ -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;
Expand Down Expand Up @@ -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)$

---
Expand Down
Original file line number Diff line number Diff line change
@@ -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<number>(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 原题链接和其他优选题解。

0 comments on commit ece12e0

Please sign in to comment.