Skip to content

Commit b905925

Browse files
committed
feat: add solutions to lc problem: No.2006
No.2006.Count Number of Pairs With Absolute Difference K
1 parent 77680c9 commit b905925

File tree

6 files changed

+195
-59
lines changed

6 files changed

+195
-59
lines changed

solution/2000-2099/2006.Count Number of Pairs With Absolute Difference K/README.md

Lines changed: 81 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,12 @@
5959

6060
<!-- 这里可写通用的实现逻辑 -->
6161

62+
**方法一:暴力法**
63+
6264
由于 nums 长度范围是 `[1,200]`,故可以直接双重循环遍历计数。
6365

66+
**方法二:哈希表计数,一次遍历**
67+
6468
<!-- tabs:start -->
6569

6670
### **Python3**
@@ -70,13 +74,23 @@
7074
```python
7175
class Solution:
7276
def countKDifference(self, nums: List[int], k: int) -> int:
73-
n = len(nums)
74-
res = 0
77+
ans, n = 0, len(nums)
7578
for i in range(n):
7679
for j in range(i + 1, n):
7780
if abs(nums[i] - nums[j]) == k:
78-
res += 1
79-
return res
81+
ans += 1
82+
return ans
83+
```
84+
85+
```python
86+
class Solution:
87+
def countKDifference(self, nums: List[int], k: int) -> int:
88+
ans = 0
89+
counter = Counter()
90+
for num in nums:
91+
ans += counter[num - k] + counter[num + k]
92+
counter[num] += 1
93+
return ans
8094
```
8195

8296
### **Java**
@@ -86,16 +100,34 @@ class Solution:
86100
```java
87101
class Solution {
88102
public int countKDifference(int[] nums, int k) {
89-
int n = nums.length;
90-
int res = 0;
91-
for (int i = 0; i < n; ++i) {
103+
int ans = 0;
104+
for (int i = 0, n = nums.length; i < n; ++i) {
92105
for (int j = i + 1; j < n; ++j) {
93106
if (Math.abs(nums[i] - nums[j]) == k) {
94-
++res;
107+
++ans;
95108
}
96109
}
97110
}
98-
return res;
111+
return ans;
112+
}
113+
}
114+
```
115+
116+
```java
117+
class Solution {
118+
public int countKDifference(int[] nums, int k) {
119+
int ans = 0;
120+
int[] counter = new int[110];
121+
for (int num : nums) {
122+
if (num >= k) {
123+
ans += counter[num - k];
124+
}
125+
if (num + k <= 100) {
126+
ans += counter[num + k];
127+
}
128+
++counter[num];
129+
}
130+
return ans;
99131
}
100132
}
101133
```
@@ -107,11 +139,28 @@ class Solution {
107139
public:
108140
int countKDifference(vector<int>& nums, int k) {
109141
int n = nums.size();
110-
int res = 0;
142+
int ans = 0;
111143
for (int i = 0; i < n; ++i)
112144
for (int j = i + 1; j < n; ++j)
113-
if (abs(nums[i] - nums[j]) == k) ++ res;
114-
return res;
145+
if (abs(nums[i] - nums[j]) == k) ++ ans;
146+
return ans;
147+
}
148+
};
149+
```
150+
151+
```cpp
152+
class Solution {
153+
public:
154+
int countKDifference(vector<int>& nums, int k) {
155+
int ans = 0;
156+
vector<int> counter(110);
157+
for (int num : nums)
158+
{
159+
if (num >= k) ans += counter[num - k];
160+
if (num + k <= 100) ans += counter[num + k];
161+
++counter[num];
162+
}
163+
return ans;
115164
}
116165
};
117166
```
@@ -121,15 +170,15 @@ public:
121170
```go
122171
func countKDifference(nums []int, k int) int {
123172
n := len(nums)
124-
res := 0
173+
ans := 0
125174
for i := 0; i < n; i++ {
126175
for j := i + 1; j < n; j++ {
127176
if abs(nums[i]-nums[j]) == k {
128-
res++
177+
ans++
129178
}
130179
}
131180
}
132-
return res
181+
return ans
133182
}
134183

135184
func abs(x int) int {
@@ -140,6 +189,23 @@ func abs(x int) int {
140189
}
141190
```
142191

192+
```go
193+
func countKDifference(nums []int, k int) int {
194+
ans := 0
195+
counter := make([]int, 110)
196+
for _, num := range nums {
197+
if num >= k {
198+
ans += counter[num-k]
199+
}
200+
if num+k <= 100 {
201+
ans += counter[num+k]
202+
}
203+
counter[num]++
204+
}
205+
return ans
206+
}
207+
```
208+
143209
### **...**
144210

145211
```

solution/2000-2099/2006.Count Number of Pairs With Absolute Difference K/README_EN.md

Lines changed: 77 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,30 +63,58 @@
6363
```python
6464
class Solution:
6565
def countKDifference(self, nums: List[int], k: int) -> int:
66-
n = len(nums)
67-
res = 0
66+
ans, n = 0, len(nums)
6867
for i in range(n):
6968
for j in range(i + 1, n):
7069
if abs(nums[i] - nums[j]) == k:
71-
res += 1
72-
return res
70+
ans += 1
71+
return ans
72+
```
73+
74+
```python
75+
class Solution:
76+
def countKDifference(self, nums: List[int], k: int) -> int:
77+
ans = 0
78+
counter = Counter()
79+
for num in nums:
80+
ans += counter[num - k] + counter[num + k]
81+
counter[num] += 1
82+
return ans
7383
```
7484

7585
### **Java**
7686

7787
```java
7888
class Solution {
7989
public int countKDifference(int[] nums, int k) {
80-
int n = nums.length;
81-
int res = 0;
82-
for (int i = 0; i < n; ++i) {
90+
int ans = 0;
91+
for (int i = 0, n = nums.length; i < n; ++i) {
8392
for (int j = i + 1; j < n; ++j) {
8493
if (Math.abs(nums[i] - nums[j]) == k) {
85-
++res;
94+
++ans;
8695
}
8796
}
8897
}
89-
return res;
98+
return ans;
99+
}
100+
}
101+
```
102+
103+
```java
104+
class Solution {
105+
public int countKDifference(int[] nums, int k) {
106+
int ans = 0;
107+
int[] counter = new int[110];
108+
for (int num : nums) {
109+
if (num >= k) {
110+
ans += counter[num - k];
111+
}
112+
if (num + k <= 100) {
113+
ans += counter[num + k];
114+
}
115+
++counter[num];
116+
}
117+
return ans;
90118
}
91119
}
92120
```
@@ -98,11 +126,28 @@ class Solution {
98126
public:
99127
int countKDifference(vector<int>& nums, int k) {
100128
int n = nums.size();
101-
int res = 0;
129+
int ans = 0;
102130
for (int i = 0; i < n; ++i)
103131
for (int j = i + 1; j < n; ++j)
104-
if (abs(nums[i] - nums[j]) == k) ++ res;
105-
return res;
132+
if (abs(nums[i] - nums[j]) == k) ++ ans;
133+
return ans;
134+
}
135+
};
136+
```
137+
138+
```cpp
139+
class Solution {
140+
public:
141+
int countKDifference(vector<int>& nums, int k) {
142+
int ans = 0;
143+
vector<int> counter(110);
144+
for (int num : nums)
145+
{
146+
if (num >= k) ans += counter[num - k];
147+
if (num + k <= 100) ans += counter[num + k];
148+
++counter[num];
149+
}
150+
return ans;
106151
}
107152
};
108153
```
@@ -112,15 +157,15 @@ public:
112157
```go
113158
func countKDifference(nums []int, k int) int {
114159
n := len(nums)
115-
res := 0
160+
ans := 0
116161
for i := 0; i < n; i++ {
117162
for j := i + 1; j < n; j++ {
118163
if abs(nums[i]-nums[j]) == k {
119-
res++
164+
ans++
120165
}
121166
}
122167
}
123-
return res
168+
return ans
124169
}
125170

126171
func abs(x int) int {
@@ -131,6 +176,23 @@ func abs(x int) int {
131176
}
132177
```
133178

179+
```go
180+
func countKDifference(nums []int, k int) int {
181+
ans := 0
182+
counter := make([]int, 110)
183+
for _, num := range nums {
184+
if num >= k {
185+
ans += counter[num-k]
186+
}
187+
if num+k <= 100 {
188+
ans += counter[num+k]
189+
}
190+
counter[num]++
191+
}
192+
return ans
193+
}
194+
```
195+
134196
### **...**
135197

136198
```
Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
class Solution {
22
public:
33
int countKDifference(vector<int>& nums, int k) {
4-
int n = nums.size();
5-
int res = 0;
6-
for (int i = 0; i < n; ++i)
7-
for (int j = i + 1; j < n; ++j)
8-
if (abs(nums[i] - nums[j]) == k) ++ res;
9-
return res;
4+
int ans = 0;
5+
vector<int> counter(110);
6+
for (int num : nums)
7+
{
8+
if (num >= k) ans += counter[num - k];
9+
if (num + k <= 100) ans += counter[num + k];
10+
++counter[num];
11+
}
12+
return ans;
1013
}
1114
};
Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
func countKDifference(nums []int, k int) int {
2-
n := len(nums)
3-
res := 0
4-
for i := 0; i < n; i++ {
5-
for j := i + 1; j < n; j++ {
6-
if abs(nums[i]-nums[j]) == k {
7-
res++
8-
}
2+
ans := 0
3+
counter := make([]int, 110)
4+
for _, num := range nums {
5+
if num >= k {
6+
ans += counter[num-k]
97
}
8+
if num+k <= 100 {
9+
ans += counter[num+k]
10+
}
11+
counter[num]++
1012
}
11-
return res
12-
}
13-
14-
func abs(x int) int {
15-
if x > 0 {
16-
return x
17-
}
18-
return -x
13+
return ans
1914
}
Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
class Solution {
22
public int countKDifference(int[] nums, int k) {
3-
int n = nums.length;
4-
int res = 0;
5-
for (int i = 0; i < n; ++i) {
6-
for (int j = i + 1; j < n; ++j) {
7-
if (Math.abs(nums[i] - nums[j]) == k) {
8-
++res;
9-
}
3+
int ans = 0;
4+
int[] counter = new int[110];
5+
for (int num : nums) {
6+
if (num >= k) {
7+
ans += counter[num - k];
108
}
9+
if (num + k <= 100) {
10+
ans += counter[num + k];
11+
}
12+
++counter[num];
1113
}
12-
return res;
14+
return ans;
1315
}
1416
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def countKDifference(self, nums: List[int], k: int) -> int:
3+
ans = 0
4+
counter = Counter()
5+
for num in nums:
6+
ans += counter[num - k] + counter[num + k]
7+
counter[num] += 1
8+
return ans

0 commit comments

Comments
 (0)