Skip to content

Commit ece12e0

Browse files
committed
✨feat: add 面试题 01.02
1 parent 55c359d commit ece12e0

File tree

3 files changed

+89
-6
lines changed

3 files changed

+89
-6
lines changed

Index/模拟.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@
205205
| [2043. 简易银行系统](https://leetcode-cn.com/problems/simple-bank-system/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/simple-bank-system/solution/by-ac_oier-9pqi/) | 中等 | 🤩🤩🤩🤩 |
206206
| [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/) | 简单 | 🤩🤩🤩🤩 |
207207
| [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/) | 中等 | 🤩🤩🤩🤩 |
208+
| [面试题 01.02. 判定是否互为字符重排](https://leetcode.cn/problems/check-permutation-lcci/) | [LeetCode 题解链接](https://leetcode.cn/problems/check-permutation-lcci/solution/by-ac_oier-qj3j/) | 简单 | 🤩🤩🤩 |
208209
| [面试题 01.05. 一次编辑](https://leetcode.cn/problems/one-away-lcci/) | [LeetCode 题解链接](https://leetcode.cn/problems/one-away-lcci/solution/by-ac_oier-7ml0/) | 中等 | 🤩🤩🤩🤩 |
209210
| [面试题 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/) | 中等 | 🤩🤩🤩🤩 |
210211
| [面试题 17.11. 单词距离](https://leetcode.cn/problems/find-closest-lcci/) | [LeetCode 题解链接](https://leetcode.cn/problems/find-closest-lcci/solution/by-ac_oier-0hv9/) | 中等 | 🤩🤩🤩🤩 |

LeetCode/371-380/373. 查找和最小的K对数字(中等).md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ $$
7878

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

81-
**代码(感谢 [@Benhao](/u/himymben/) 同学提供的其他语言版本):**
81+
Java 代码:
8282
```Java
8383
class Solution {
8484
boolean flag = true;
@@ -101,7 +101,7 @@ class Solution {
101101
}
102102
}
103103
```
104-
-
104+
Python3 代码:
105105
```Python3
106106
class Solution:
107107
def kSmallestPairs(self, nums1: List[int], nums2: List[int], k: int) -> List[List[int]]:
@@ -118,7 +118,7 @@ class Solution:
118118
heapq.heappush(pq, (nums1[a] + nums2[b + 1], a, b + 1))
119119
return ans
120120
```
121-
-
121+
Golang 代码:
122122
```Golang
123123
func kSmallestPairs(nums1 []int, nums2 []int, k int) [][]int {
124124
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] }
156156
func (h *hp) Push(v interface{}) { *h = append(*h, v.([]int)) }
157157
func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v }
158158
```
159-
* 时间复杂度:令 $M$ 为 $n$、$m$ 和 $k$ 三者中的最小值,复杂度为 $O(M + k) * \log{M})$
159+
* 时间复杂度:令 $M$ 为 $n$、$m$ 和 $k$ 三者中的最小值,复杂度为 $O(M + k) \times \log{M})$
160160
* 空间复杂度:$O(M)$
161161

162162
---
@@ -182,7 +182,7 @@ func (h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1];
182182

183183
最后,在所有处理过程中,我们都可以利用答案数组的大小与 $k$ 的关系做剪枝。
184184

185-
代码:
185+
Java 代码:
186186
```Java
187187
class Solution {
188188
int[] nums1, nums2;
@@ -245,7 +245,7 @@ class Solution {
245245
}
246246
}
247247
```
248-
* 时间复杂度:假设点对和的值域大小范围为 $M$,第一次二分的复杂度为 $O((n * m) * \log{M})$;统计点对和值小于目标值 $x$ 的复杂度为 $O(n * m)$;统计所有点对和等于目标值的复杂度为 $O(\max(n * \log{m}, k))$(整个处理过程中利用了大小关系做了剪枝,大多循环都不会跑满,实际计算量会比理论分析的要低)
248+
* 时间复杂度:假设点对和的值域大小范围为 $M$,第一次二分的复杂度为 $O((n \times m) \times \log{M})$;统计点对和值小于目标值 $x$ 的复杂度为 $O(n \times m)$;统计所有点对和等于目标值的复杂度为 $O(\max(n \times \log{m}, k))$(整个处理过程中利用了大小关系做了剪枝,大多循环都不会跑满,实际计算量会比理论分析的要低)
249249
* 空间复杂度:$O(k)$
250250

251251
---
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[面试题 01.02. 判定是否互为字符重排](https://leetcode.cn/problems/check-permutation-lcci/solution/by-ac_oier-qj3j/)** ,难度为 **简单**
4+
5+
Tag : 「模拟」
6+
7+
8+
9+
给定两个字符串 `s1``s2`,请编写一个程序,确定其中一个字符串的字符重新排列后,能否变成另一个字符串。
10+
11+
示例 1:
12+
```
13+
输入: s1 = "abc", s2 = "bca"
14+
15+
输出: true
16+
```
17+
示例 2:
18+
```
19+
输入: s1 = "abc", s2 = "bad"
20+
21+
输出: false
22+
```
23+
24+
说明:
25+
* $0 <= len(s1) <= 100$
26+
* $0 <= len(s2) <= 100$
27+
28+
---
29+
30+
### 模拟
31+
32+
根据题意,对两字符串进行词频统计,统计过程中使用变量 `tot` 记录词频不同的字符个数。
33+
34+
Java 代码:
35+
```Java
36+
class Solution {
37+
public boolean CheckPermutation(String s1, String s2) {
38+
int n = s1.length(), m = s2.length(), tot = 0;
39+
if (n != m) return false;
40+
int[] cnts = new int[128];
41+
for (int i = 0; i < n; i++) {
42+
if (++cnts[s1.charAt(i)] == 1) tot++;
43+
if (--cnts[s2.charAt(i)] == 0) tot--;
44+
}
45+
return tot == 0;
46+
}
47+
}
48+
```
49+
TypeScript 代码:
50+
```TypeScript
51+
function CheckPermutation(s1: string, s2: string): boolean {
52+
let n = s1.length, m = s2.length, tot = 0
53+
if (n != m) return false
54+
const cnts = new Array<number>(128).fill(0)
55+
for (let i = 0; i < n; i++) {
56+
if (++cnts[s1.charCodeAt(i)] == 1) tot++
57+
if (--cnts[s2.charCodeAt(i)] == 0) tot--
58+
}
59+
return tot == 0
60+
};
61+
```
62+
Python3 代码:
63+
```Python3
64+
class Solution:
65+
def CheckPermutation(self, s1: str, s2: str) -> bool:
66+
return Counter(s1) == Counter(s2)
67+
```
68+
* 时间复杂度:$O(n)$
69+
* 空间复杂度:$O(C)$,其中 $C = 128$ 为字符集大小
70+
71+
---
72+
73+
### 最后
74+
75+
这是我们「刷穿 LeetCode」系列文章的第 `No.面试题 01.02` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
76+
77+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
78+
79+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
80+
81+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
82+

0 commit comments

Comments
 (0)