Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 66f296d

Browse files
feat: add solutions to lc problem: No.0532 (doocs#4164)
No.0532.K-diff Pairs in an Array
1 parent 1b22bc7 commit 66f296d

File tree

8 files changed

+207
-175
lines changed

8 files changed

+207
-175
lines changed

‎solution/0500-0599/0532.K-diff Pairs in an Array/README.md‎

Lines changed: 70 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,13 @@ tags:
7777

7878
### 方法一:哈希表
7979

80-
由于 $k$ 是一个定值,因此用哈希表 $ans$ 记录数对的较小值,就能够确定较大的值。最后返回 ans 的大小作为答案。
80+
由于 $k$ 是一个定值,我们可以用一个哈希表 $\textit{ans}$ 记录数对的较小值,就能够确定较大的值。最后返回 $\textit{ans}$ 的大小作为答案。
8181

82-
遍历数组 $nums,ドル当前遍历到的数 $nums[j],ドル我们记为 $v,ドル用哈希表 $vis$ 记录此前遍历到的所有数字。若 $v-k$ 在 $vis$ 中,则将 $v-k$ 添加至 $ans$;若 $v+k$ 在 $vis$ 中,则将 $v$ 添加至 $ans$
82+
遍历数组 $\textit{nums},ドル当前遍历到的数 $x,ドル我们用哈希表 $\textit{vis}$ 记录此前遍历到的所有数字。若 $x-k$ 在 $\textit{vis}$ 中,则将 $x-k$ 添加至 $\textit{ans}$;若 $x+k$ 在 $\textit{vis}$ 中,则将 $x$ 添加至 $\textit{ans}$。然后我们将 $x$ 添加至 $\textit{vis}$。继续遍历数组 $\textit{nums}$ 直至遍历结束
8383

84-
时间复杂度 $O(n),ドル其中 $n$ 表示数组 $nums$ 的长度。
84+
最后返回 $\textit{ans}$ 的大小作为答案。
85+
86+
时间复杂度 $O(n),ドル空间复杂度 $O(n)$。其中 $n$ 为数组 $\textit{nums}$ 的长度。
8587

8688
<!-- tabs:start -->
8789

@@ -90,13 +92,14 @@ tags:
9092
```python
9193
class Solution:
9294
def findPairs(self, nums: List[int], k: int) -> int:
93-
vis, ans = set(), set()
94-
for v in nums:
95-
if v - k in vis:
96-
ans.add(v - k)
97-
if v + k in vis:
98-
ans.add(v)
99-
vis.add(v)
95+
ans = set()
96+
vis = set()
97+
for x in nums:
98+
if x - k in vis:
99+
ans.add(x - k)
100+
if x + k in vis:
101+
ans.add(x)
102+
vis.add(x)
100103
return len(ans)
101104
```
102105

@@ -105,16 +108,16 @@ class Solution:
105108
```java
106109
class Solution {
107110
public int findPairs(int[] nums, int k) {
108-
Set<Integer> vis = new HashSet<>();
109111
Set<Integer> ans = new HashSet<>();
110-
for (int v : nums) {
111-
if (vis.contains(v - k)) {
112-
ans.add(v - k);
112+
Set<Integer> vis = new HashSet<>();
113+
for (int x : nums) {
114+
if (vis.contains(x - k)) {
115+
ans.add(x - k);
113116
}
114-
if (vis.contains(v + k)) {
115-
ans.add(v);
117+
if (vis.contains(x + k)) {
118+
ans.add(x);
116119
}
117-
vis.add(v);
120+
vis.add(x);
118121
}
119122
return ans.size();
120123
}
@@ -127,12 +130,15 @@ class Solution {
127130
class Solution {
128131
public:
129132
int findPairs(vector<int>& nums, int k) {
130-
unordered_set<int> vis;
131-
unordered_set<int> ans;
132-
for (int& v : nums) {
133-
if (vis.count(v - k)) ans.insert(v - k);
134-
if (vis.count(v + k)) ans.insert(v);
135-
vis.insert(v);
133+
unordered_set<int> ans, vis;
134+
for (int x : nums) {
135+
if (vis.count(x - k)) {
136+
ans.insert(x - k);
137+
}
138+
if (vis.count(x + k)) {
139+
ans.insert(x);
140+
}
141+
vis.insert(x);
136142
}
137143
return ans.size();
138144
}
@@ -143,52 +149,61 @@ public:
143149
144150
```go
145151
func findPairs(nums []int, k int) int {
146-
vis := map[int]bool{}
147-
ans := map[int]bool{}
148-
for _, v := range nums {
149-
if vis[v-k] {
150-
ans[v-k] = true
152+
ans := make(map[int]struct{})
153+
vis := make(map[int]struct{})
154+
155+
for _, x := range nums {
156+
if _, ok := vis[x-k]; ok {
157+
ans[x-k] = struct{}{}
151158
}
152-
if vis[v+k] {
153-
ans[v] = true
159+
if _, ok := vis[x+k]; ok {
160+
ans[x] = struct{}{}
154161
}
155-
vis[v] = true
162+
vis[x] = struct{}{}
156163
}
157164
return len(ans)
158165
}
159166
```
160167

168+
#### TypeScript
169+
170+
```ts
171+
function findPairs(nums: number[], k: number): number {
172+
const ans = new Set<number>();
173+
const vis = new Set<number>();
174+
for (const x of nums) {
175+
if (vis.has(x - k)) {
176+
ans.add(x - k);
177+
}
178+
if (vis.has(x + k)) {
179+
ans.add(x);
180+
}
181+
vis.add(x);
182+
}
183+
return ans.size;
184+
}
185+
```
186+
161187
#### Rust
162188

163189
```rust
190+
use std::collections::HashSet;
191+
164192
impl Solution {
165-
pub fn find_pairs(mut nums: Vec<i32>, k: i32) -> i32 {
166-
nums.sort();
167-
let n = nums.len();
168-
let mut res = 0;
169-
let mut left = 0;
170-
let mut right = 1;
171-
while right < n {
172-
let num = i32::abs(nums[left] - nums[right]);
173-
if num == k {
174-
res += 1;
193+
pub fn find_pairs(nums: Vec<i32>, k: i32) -> i32 {
194+
let mut ans = HashSet::new();
195+
let mut vis = HashSet::new();
196+
197+
for &x in &nums {
198+
if vis.contains(&(x - k)) {
199+
ans.insert(x - k);
175200
}
176-
if num <= k {
177-
right += 1;
178-
while right < n && nums[right - 1] == nums[right] {
179-
right += 1;
180-
}
181-
} else {
182-
left += 1;
183-
while left < right && nums[left - 1] == nums[left] {
184-
left += 1;
185-
}
186-
if left == right {
187-
right += 1;
188-
}
201+
if vis.contains(&(x + k)) {
202+
ans.insert(x);
189203
}
204+
vis.insert(x);
190205
}
191-
res
206+
ans.len() asi32
192207
}
193208
}
194209
```
@@ -197,16 +212,4 @@ impl Solution {
197212

198213
<!-- solution:end -->
199214

200-
<!-- solution:start -->
201-
202-
### 方法二:排序 + 双指针
203-
204-
只需要统计组合的数量,因此可以改动原数组,对其排序,使用双指针来统计。
205-
206-
声明 `left``right` 指针,初始化为 0 和 1。根据 `abs(nums[left] - nums[right])``k` 值对比结果移动指针。
207-
208-
需要注意的是,**不能出现重复的组合**,所以移动指针时,不能仅仅是 `+1`,需要到一个不等于当前值的位置。
209-
210-
<!-- solution:end -->
211-
212215
<!-- problem:end -->

‎solution/0500-0599/0532.K-diff Pairs in an Array/README_EN.md‎

Lines changed: 74 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,15 @@ Although we have two 1s in the input, we should only return the number of <stron
7373

7474
<!-- solution:start -->
7575

76-
### Solution 1
76+
### Solution 1: Hash Table
77+
78+
Since $k$ is a fixed value, we can use a hash table $\textit{ans}$ to record the smaller value of the pairs, which allows us to determine the larger value. Finally, we return the size of $\textit{ans}$ as the answer.
79+
80+
We traverse the array $\textit{nums}$. For the current number $x,ドル we use a hash table $\textit{vis}$ to record all the numbers that have been traversed. If $x-k$ is in $\textit{vis},ドル we add $x-k$ to $\textit{ans}$. If $x+k$ is in $\textit{vis},ドル we add $x$ to $\textit{ans}$. Then, we add $x$ to $\textit{vis}$. Continue traversing the array $\textit{nums}$ until the end.
81+
82+
Finally, we return the size of $\textit{ans}$ as the answer.
83+
84+
The time complexity is $O(n),ドル and the space complexity is $O(n)$. Here, $n$ is the length of the array $\textit{nums}$.
7785

7886
<!-- tabs:start -->
7987

@@ -82,13 +90,14 @@ Although we have two 1s in the input, we should only return the number of <stron
8290
```python
8391
class Solution:
8492
def findPairs(self, nums: List[int], k: int) -> int:
85-
vis, ans = set(), set()
86-
for v in nums:
87-
if v - k in vis:
88-
ans.add(v - k)
89-
if v + k in vis:
90-
ans.add(v)
91-
vis.add(v)
93+
ans = set()
94+
vis = set()
95+
for x in nums:
96+
if x - k in vis:
97+
ans.add(x - k)
98+
if x + k in vis:
99+
ans.add(x)
100+
vis.add(x)
92101
return len(ans)
93102
```
94103

@@ -97,16 +106,16 @@ class Solution:
97106
```java
98107
class Solution {
99108
public int findPairs(int[] nums, int k) {
100-
Set<Integer> vis = new HashSet<>();
101109
Set<Integer> ans = new HashSet<>();
102-
for (int v : nums) {
103-
if (vis.contains(v - k)) {
104-
ans.add(v - k);
110+
Set<Integer> vis = new HashSet<>();
111+
for (int x : nums) {
112+
if (vis.contains(x - k)) {
113+
ans.add(x - k);
105114
}
106-
if (vis.contains(v + k)) {
107-
ans.add(v);
115+
if (vis.contains(x + k)) {
116+
ans.add(x);
108117
}
109-
vis.add(v);
118+
vis.add(x);
110119
}
111120
return ans.size();
112121
}
@@ -119,12 +128,15 @@ class Solution {
119128
class Solution {
120129
public:
121130
int findPairs(vector<int>& nums, int k) {
122-
unordered_set<int> vis;
123-
unordered_set<int> ans;
124-
for (int& v : nums) {
125-
if (vis.count(v - k)) ans.insert(v - k);
126-
if (vis.count(v + k)) ans.insert(v);
127-
vis.insert(v);
131+
unordered_set<int> ans, vis;
132+
for (int x : nums) {
133+
if (vis.count(x - k)) {
134+
ans.insert(x - k);
135+
}
136+
if (vis.count(x + k)) {
137+
ans.insert(x);
138+
}
139+
vis.insert(x);
128140
}
129141
return ans.size();
130142
}
@@ -135,52 +147,61 @@ public:
135147
136148
```go
137149
func findPairs(nums []int, k int) int {
138-
vis := map[int]bool{}
139-
ans := map[int]bool{}
140-
for _, v := range nums {
141-
if vis[v-k] {
142-
ans[v-k] = true
150+
ans := make(map[int]struct{})
151+
vis := make(map[int]struct{})
152+
153+
for _, x := range nums {
154+
if _, ok := vis[x-k]; ok {
155+
ans[x-k] = struct{}{}
143156
}
144-
if vis[v+k] {
145-
ans[v] = true
157+
if _, ok := vis[x+k]; ok {
158+
ans[x] = struct{}{}
146159
}
147-
vis[v] = true
160+
vis[x] = struct{}{}
148161
}
149162
return len(ans)
150163
}
151164
```
152165

166+
#### TypeScript
167+
168+
```ts
169+
function findPairs(nums: number[], k: number): number {
170+
const ans = new Set<number>();
171+
const vis = new Set<number>();
172+
for (const x of nums) {
173+
if (vis.has(x - k)) {
174+
ans.add(x - k);
175+
}
176+
if (vis.has(x + k)) {
177+
ans.add(x);
178+
}
179+
vis.add(x);
180+
}
181+
return ans.size;
182+
}
183+
```
184+
153185
#### Rust
154186

155187
```rust
188+
use std::collections::HashSet;
189+
156190
impl Solution {
157-
pub fn find_pairs(mut nums: Vec<i32>, k: i32) -> i32 {
158-
nums.sort();
159-
let n = nums.len();
160-
let mut res = 0;
161-
let mut left = 0;
162-
let mut right = 1;
163-
while right < n {
164-
let num = i32::abs(nums[left] - nums[right]);
165-
if num == k {
166-
res += 1;
191+
pub fn find_pairs(nums: Vec<i32>, k: i32) -> i32 {
192+
let mut ans = HashSet::new();
193+
let mut vis = HashSet::new();
194+
195+
for &x in &nums {
196+
if vis.contains(&(x - k)) {
197+
ans.insert(x - k);
167198
}
168-
if num <= k {
169-
right += 1;
170-
while right < n && nums[right - 1] == nums[right] {
171-
right += 1;
172-
}
173-
} else {
174-
left += 1;
175-
while left < right && nums[left - 1] == nums[left] {
176-
left += 1;
177-
}
178-
if left == right {
179-
right += 1;
180-
}
199+
if vis.contains(&(x + k)) {
200+
ans.insert(x);
181201
}
202+
vis.insert(x);
182203
}
183-
res
204+
ans.len() asi32
184205
}
185206
}
186207
```
Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
class Solution {
22
public:
33
int findPairs(vector<int>& nums, int k) {
4-
unordered_set<int> vis;
5-
unordered_set<int> ans;
6-
for (int& v : nums) {
7-
if (vis.count(v - k)) ans.insert(v - k);
8-
if (vis.count(v + k)) ans.insert(v);
9-
vis.insert(v);
4+
unordered_set<int> ans, vis;
5+
for (int x : nums) {
6+
if (vis.count(x - k)) {
7+
ans.insert(x - k);
8+
}
9+
if (vis.count(x + k)) {
10+
ans.insert(x);
11+
}
12+
vis.insert(x);
1013
}
1114
return ans.size();
1215
}
13-
};
16+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /