File tree Expand file tree Collapse file tree 3 files changed +108
-0
lines changed
solution/0500-0599/0532.K-diff Pairs in an Array Expand file tree Collapse file tree 3 files changed +108
-0
lines changed Original file line number Diff line number Diff line change 66
66
67
67
时间复杂度 $O(n)$,其中 $n$ 表示数组 $nums$ 的长度。
68
68
69
+ ** 方法二:排序 + 双指针**
70
+
71
+ 只需要统计组合的数量,因此可以改动原数组,对其排序,使用双指针来统计。
72
+
73
+ 声明 ` left ` 与 ` right ` 指针,初始化为 0 和 1。根据 ` abs(nums[left] - nums[right]) ` 与 ` k ` 值对比结果移动指针。
74
+
75
+ 需要注意的是,** 不能出现重复的组合** ,所以移动指针时,不能仅仅是 ` +1 ` ,需要到一个不等于当前值的位置。
76
+
69
77
<!-- tabs:start -->
70
78
71
79
### ** Python3**
@@ -146,6 +154,41 @@ func findPairs(nums []int, k int) int {
146
154
}
147
155
```
148
156
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
+
149
192
### ** ...**
150
193
151
194
```
Original file line number Diff line number Diff line change @@ -129,6 +129,41 @@ func findPairs(nums []int, k int) int {
129
129
}
130
130
```
131
131
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
+
132
167
### ** ...**
133
168
134
169
```
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments