Skip to content

Commit 20d5131

Browse files
committed
✨feat: Add 2006
1 parent bfcf7be commit 20d5131

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed

Index/哈希表.md

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
| [1748. 唯一元素的和](https://leetcode-cn.com/problems/sum-of-unique-elements/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/sum-of-unique-elements/solution/gong-shui-san-xie-yi-ti-shuang-jie-pai-x-atnd/) | 简单 | 🤩🤩🤩🤩 |
5555
| [1838. 最高频元素的频数](https://leetcode-cn.com/problems/frequency-of-the-most-frequent-element/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/frequency-of-the-most-frequent-element/solution/gong-shui-san-xie-cong-mei-ju-dao-pai-xu-kxnk/) | 中等 | 🤩🤩🤩 |
5656
| [1995. 统计特殊四元组](https://leetcode-cn.com/problems/count-special-quadruplets/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/count-special-quadruplets/solution/gong-shui-san-xie-yi-ti-si-jie-mei-ju-ha-gmhv/) | 简单 | 🤩🤩🤩🤩 |
57+
| [2006. 差的绝对值为 K 的数对数目](https://leetcode-cn.com/problems/count-number-of-pairs-with-absolute-difference-k/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/count-number-of-pairs-with-absolute-difference-k/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-1jel/) | 简单 | 🤩🤩🤩🤩 |
5758
| [2013. 检测正方形](https://leetcode-cn.com/problems/detect-squares/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/detect-squares/solution/gong-shui-san-xie-jian-dan-ha-xi-biao-yu-748e/) | 中等 | 🤩🤩🤩🤩🤩 |
5859
| [2034. 股票价格波动](https://leetcode-cn.com/problems/stock-price-fluctuation/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/stock-price-fluctuation/solution/gong-shui-san-xie-shu-ju-jie-gou-mo-ni-t-u6f4/) | 中等 | 🤩🤩🤩🤩 |
5960
| [面试题 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/) | 中等 | 🤩🤩🤩🤩 |

Index/模拟.md

+1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@
109109
| [1894. 找到需要补充粉笔的学生编号](https://leetcode-cn.com/problems/find-the-student-that-will-replace-the-chalk/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/find-the-student-that-will-replace-the-chalk/solution/gong-shui-san-xie-yi-ti-shuang-jie-qian-kpqsk/) | 中等 | 🤩🤩🤩🤩 |
110110
| [1995. 统计特殊四元组](https://leetcode-cn.com/problems/count-special-quadruplets/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/count-special-quadruplets/solution/gong-shui-san-xie-yi-ti-si-jie-mei-ju-ha-gmhv/) | 简单 | 🤩🤩🤩🤩 |
111111
| [2000. 反转单词前缀](https://leetcode-cn.com/problems/reverse-prefix-of-word/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/reverse-prefix-of-word/solution/gong-shui-san-xie-jian-dan-shuang-zhi-zh-dp9u/) | 简单 | 🤩🤩🤩🤩 |
112+
| [2006. 差的绝对值为 K 的数对数目](https://leetcode-cn.com/problems/count-number-of-pairs-with-absolute-difference-k/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/count-number-of-pairs-with-absolute-difference-k/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-1jel/) | 简单 | 🤩🤩🤩🤩 |
112113
| [2022. 将一维数组转变成二维数组](https://leetcode-cn.com/problems/convert-1d-array-into-2d-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/convert-1d-array-into-2d-array/solution/gong-shui-san-xie-jiang-2021-de-1-gai-ch-qc1a/) | 简单 | 🤩🤩🤩🤩 |
113114
| [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/) | 简单 | 🤩🤩🤩🤩 |
114115
| [面试题 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/) | 中等 | 🤩🤩🤩🤩 |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[2006. 差的绝对值为 K 的数对数目](https://leetcode-cn.com/problems/count-number-of-pairs-with-absolute-difference-k/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-1jel/)** ,难度为 **简单**
4+
5+
Tag : 「模拟」、「哈希表」
6+
7+
8+
9+
给你一个整数数组 `nums` 和一个整数 `k` ,请你返回数对 $(i, j)$ 的数目,满足 $i < j$ 且`|nums[i] - nums[j]| == k`
10+
11+
`|x|` 的值定义为:
12+
13+
* 如果 $x >= 0$ ,那么值为 $x$ 。
14+
* 如果 $x < 0$ ,那么值为 $-x$ 。
15+
16+
示例 1:
17+
```
18+
输入:nums = [1,2,2,1], k = 1
19+
20+
输出:4
21+
22+
解释:差的绝对值为 1 的数对为:
23+
- [1,2,2,1]
24+
- [1,2,2,1]
25+
- [1,2,2,1]
26+
- [1,2,2,1]
27+
```
28+
示例 2:
29+
```
30+
输入:nums = [1,3], k = 3
31+
32+
输出:0
33+
34+
解释:没有任何数对差的绝对值为 3 。
35+
```
36+
示例 3:
37+
```
38+
输入:nums = [3,2,1,5,4], k = 2
39+
40+
输出:3
41+
42+
解释:差的绝对值为 2 的数对为:
43+
- [3,2,1,5,4]
44+
- [3,2,1,5,4]
45+
- [3,2,1,5,4]
46+
```
47+
48+
提示:
49+
* $1 <= nums.length <= 200$
50+
* $1 <= nums[i] <= 100$
51+
* $1 <= k <= 99$
52+
53+
---
54+
55+
### 朴素解法
56+
57+
数据范围很小,直接根据题意模拟即可。
58+
59+
代码:
60+
```Java
61+
class Solution {
62+
public int countKDifference(int[] nums, int k) {
63+
int n = nums.length, ans = 0;
64+
for (int i = 0; i < n; i++) {
65+
for (int j = i + 1; j < n; j++) {
66+
if (Math.abs(nums[i] - nums[j]) == k) ans++;
67+
}
68+
}
69+
return ans;
70+
}
71+
}
72+
```
73+
* 时间复杂度:$O(n^2)$
74+
* 空间复杂度:$O(1)$
75+
76+
---
77+
78+
### 哈希表
79+
80+
对于每个 $t = nums[j]$ 而言,是要找与其相对差值为 $k$ 且下标比其小的数(即 $t - k$ 和 $t + k$),可以采取边遍历边记录某个数出现次数,从而将复杂度优化到 $O(n)$。
81+
82+
再利用数据范围 $1 <= nums[i] <= k$,可以直接使用数组充当哈希表。
83+
84+
代码:
85+
```Java
86+
class Solution {
87+
public int countKDifference(int[] nums, int k) {
88+
int[] cnts = new int[110];
89+
int n = nums.length, ans = 0;
90+
for (int i = 0; i < n; i++) {
91+
int t = nums[i];
92+
if (t - k >= 1) ans += cnts[t - k];
93+
if (t + k <= 100) ans += cnts[t + k];
94+
cnts[t]++;
95+
}
96+
return ans;
97+
}
98+
}
99+
```
100+
* 时间复杂度:$O(n)$
101+
* 空间复杂度:$O(C)$
102+
103+
---
104+
105+
### 最后
106+
107+
这是我们「刷穿 LeetCode」系列文章的第 `No.2006` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
108+
109+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
110+
111+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
112+
113+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
114+

0 commit comments

Comments
 (0)