Skip to content

Commit 0417d77

Browse files
committed
feat: add rust solution to lc problem: No.0532
No.0532.K-diff Pairs in an Array
1 parent e59d00b commit 0417d77

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@
6666

6767
时间复杂度 $O(n)$,其中 $n$ 表示数组 $nums$ 的长度。
6868

69+
**方法二:排序 + 双指针**
70+
71+
只需要统计组合的数量,因此可以改动原数组,对其排序,使用双指针来统计。
72+
73+
声明 `left``right` 指针,初始化为 0 和 1。根据 `abs(nums[left] - nums[right])``k` 值对比结果移动指针。
74+
75+
需要注意的是,**不能出现重复的组合**,所以移动指针时,不能仅仅是 `+1`,需要到一个不等于当前值的位置。
76+
6977
<!-- tabs:start -->
7078

7179
### **Python3**
@@ -146,6 +154,41 @@ func findPairs(nums []int, k int) int {
146154
}
147155
```
148156

157+
### **Rust**
158+
159+
```rust
160+
impl Solution {
161+
pub fn find_pairs(mut nums: Vec<i32>, k: i32) -> i32 {
162+
nums.sort();
163+
let n = nums.len();
164+
let mut res = 0;
165+
let mut left = 0;
166+
let mut right = 1;
167+
while right < n {
168+
let num = i32::abs(nums[left] - nums[right]);
169+
if num == k {
170+
res += 1;
171+
}
172+
if num <= k {
173+
right += 1;
174+
while right < n && nums[right - 1] == nums[right] {
175+
right += 1;
176+
}
177+
} else {
178+
left += 1;
179+
while left < right && nums[left - 1] == nums[left] {
180+
left += 1;
181+
}
182+
if left == right {
183+
right += 1;
184+
}
185+
}
186+
}
187+
res
188+
}
189+
}
190+
```
191+
149192
### **...**
150193

151194
```

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,41 @@ func findPairs(nums []int, k int) int {
129129
}
130130
```
131131

132+
### **Rust**
133+
134+
```rust
135+
impl Solution {
136+
pub fn find_pairs(mut nums: Vec<i32>, k: i32) -> i32 {
137+
nums.sort();
138+
let n = nums.len();
139+
let mut res = 0;
140+
let mut left = 0;
141+
let mut right = 1;
142+
while right < n {
143+
let num = i32::abs(nums[left] - nums[right]);
144+
if num == k {
145+
res += 1;
146+
}
147+
if num <= k {
148+
right += 1;
149+
while right < n && nums[right - 1] == nums[right] {
150+
right += 1;
151+
}
152+
} else {
153+
left += 1;
154+
while left < right && nums[left - 1] == nums[left] {
155+
left += 1;
156+
}
157+
if left == right {
158+
right += 1;
159+
}
160+
}
161+
}
162+
res
163+
}
164+
}
165+
```
166+
132167
### **...**
133168

134169
```
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
impl Solution {
2+
pub fn find_pairs(mut nums: Vec<i32>, k: i32) -> i32 {
3+
nums.sort();
4+
let n = nums.len();
5+
let mut res = 0;
6+
let mut left = 0;
7+
let mut right = 1;
8+
while right < n {
9+
let num = i32::abs(nums[left] - nums[right]);
10+
if num == k {
11+
res += 1;
12+
}
13+
if num <= k {
14+
right += 1;
15+
while right < n && nums[right - 1] == nums[right] {
16+
right += 1;
17+
}
18+
} else {
19+
left += 1;
20+
while left < right && nums[left - 1] == nums[left] {
21+
left += 1;
22+
}
23+
if left == right {
24+
right += 1;
25+
}
26+
}
27+
}
28+
res
29+
}
30+
}

0 commit comments

Comments
 (0)