Skip to content

Commit e80f27e

Browse files
committed
feat: add solutions to lc problems: No.2006,2007
1 parent 98329ef commit e80f27e

File tree

11 files changed

+485
-6
lines changed

11 files changed

+485
-6
lines changed

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

+64-2
Original file line numberDiff line numberDiff line change
@@ -55,27 +55,89 @@
5555
<li><code>1 &lt;= k &lt;= 99</code></li>
5656
</ul>
5757

58-
5958
## 解法
6059

6160
<!-- 这里可写通用的实现逻辑 -->
6261

62+
由于 nums 长度范围是 `[1,200]`,故可以直接双重循环遍历计数。
63+
6364
<!-- tabs:start -->
6465

6566
### **Python3**
6667

6768
<!-- 这里可写当前语言的特殊实现逻辑 -->
6869

6970
```python
70-
71+
class Solution:
72+
def countKDifference(self, nums: List[int], k: int) -> int:
73+
n = len(nums)
74+
res = 0
75+
for i in range(n):
76+
for j in range(i + 1, n):
77+
if abs(nums[i] - nums[j]) == k:
78+
res += 1
79+
return res
7180
```
7281

7382
### **Java**
7483

7584
<!-- 这里可写当前语言的特殊实现逻辑 -->
7685

7786
```java
87+
class Solution {
88+
public int countKDifference(int[] nums, int k) {
89+
int n = nums.length;
90+
int res = 0;
91+
for (int i = 0; i < n; ++i) {
92+
for (int j = i + 1; j < n; ++j) {
93+
if (Math.abs(nums[i] - nums[j]) == k) {
94+
++res;
95+
}
96+
}
97+
}
98+
return res;
99+
}
100+
}
101+
```
102+
103+
### **C++**
104+
105+
```cpp
106+
class Solution {
107+
public:
108+
int countKDifference(vector<int>& nums, int k) {
109+
int n = nums.size();
110+
int res = 0;
111+
for (int i = 0; i < n; ++i)
112+
for (int j = i + 1; j < n; ++j)
113+
if (abs(nums[i] - nums[j]) == k) ++ res;
114+
return res;
115+
}
116+
};
117+
```
78118
119+
### **Go**
120+
121+
```go
122+
func countKDifference(nums []int, k int) int {
123+
n := len(nums)
124+
res := 0
125+
for i := 0; i < n; i++ {
126+
for j := i + 1; j < n; j++ {
127+
if abs(nums[i]-nums[j]) == k {
128+
res++
129+
}
130+
}
131+
}
132+
return res
133+
}
134+
135+
func abs(x int) int {
136+
if x > 0 {
137+
return x
138+
}
139+
return -x
140+
}
79141
```
80142

81143
### **...**

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

+62-2
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,81 @@
5454
<li><code>1 &lt;= k &lt;= 99</code></li>
5555
</ul>
5656

57-
5857
## Solutions
5958

6059
<!-- tabs:start -->
6160

6261
### **Python3**
6362

6463
```python
65-
64+
class Solution:
65+
def countKDifference(self, nums: List[int], k: int) -> int:
66+
n = len(nums)
67+
res = 0
68+
for i in range(n):
69+
for j in range(i + 1, n):
70+
if abs(nums[i] - nums[j]) == k:
71+
res += 1
72+
return res
6673
```
6774

6875
### **Java**
6976

7077
```java
78+
class Solution {
79+
public int countKDifference(int[] nums, int k) {
80+
int n = nums.length;
81+
int res = 0;
82+
for (int i = 0; i < n; ++i) {
83+
for (int j = i + 1; j < n; ++j) {
84+
if (Math.abs(nums[i] - nums[j]) == k) {
85+
++res;
86+
}
87+
}
88+
}
89+
return res;
90+
}
91+
}
92+
```
93+
94+
### **C++**
95+
96+
```cpp
97+
class Solution {
98+
public:
99+
int countKDifference(vector<int>& nums, int k) {
100+
int n = nums.size();
101+
int res = 0;
102+
for (int i = 0; i < n; ++i)
103+
for (int j = i + 1; j < n; ++j)
104+
if (abs(nums[i] - nums[j]) == k) ++ res;
105+
return res;
106+
}
107+
};
108+
```
71109
110+
### **Go**
111+
112+
```go
113+
func countKDifference(nums []int, k int) int {
114+
n := len(nums)
115+
res := 0
116+
for i := 0; i < n; i++ {
117+
for j := i + 1; j < n; j++ {
118+
if abs(nums[i]-nums[j]) == k {
119+
res++
120+
}
121+
}
122+
}
123+
return res
124+
}
125+
126+
func abs(x int) int {
127+
if x > 0 {
128+
return x
129+
}
130+
return -x
131+
}
72132
```
73133

74134
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public:
3+
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;
10+
}
11+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
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+
}
9+
}
10+
}
11+
return res
12+
}
13+
14+
func abs(x int) int {
15+
if x > 0 {
16+
return x
17+
}
18+
return -x
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
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+
}
10+
}
11+
}
12+
return res;
13+
}
14+
}

solution/2000-2099/2007.Find Original Array From Doubled Array/README.md

+108-1
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,122 @@
5757
<!-- 这里可写当前语言的特殊实现逻辑 -->
5858

5959
```python
60-
60+
class Solution:
61+
def findOriginalArray(self, changed: List[int]) -> List[int]:
62+
if len(changed) % 2 != 0:
63+
return []
64+
n = 100010
65+
counter = [0] * n
66+
for x in changed:
67+
counter[x] += 1
68+
if counter[0] % 2 != 0:
69+
return []
70+
res = [0] * (counter[0] // 2)
71+
for i in range(1, n):
72+
if counter[i] == 0:
73+
continue
74+
if i * 2 > n or counter[i] > counter[i*2]:
75+
return []
76+
res.extend([i] * counter[i])
77+
counter[i*2] -= counter[i]
78+
return res
6179
```
6280

6381
### **Java**
6482

6583
<!-- 这里可写当前语言的特殊实现逻辑 -->
6684

6785
```java
86+
class Solution {
87+
public int[] findOriginalArray(int[] changed) {
88+
if (changed.length % 2 != 0) {
89+
return new int[]{};
90+
}
91+
int n = 100010;
92+
int[] counter = new int[n];
93+
for (int x : changed) {
94+
++counter[x];
95+
}
96+
if (counter[0] % 2 != 0) {
97+
return new int[]{};
98+
}
99+
int[] res = new int[changed.length / 2];
100+
int j = counter[0] / 2;
101+
for (int i = 1; i < n; ++i) {
102+
if (counter[i] == 0) {
103+
continue;
104+
}
105+
if (i * 2 >= n || counter[i] > counter[i * 2]) {
106+
return new int[]{};
107+
}
108+
counter[i * 2] -= counter[i];
109+
while (counter[i]-- > 0) {
110+
res[j++] = i;
111+
}
112+
}
113+
return res;
114+
}
115+
}
116+
```
117+
118+
### **C++**
119+
120+
```cpp
121+
class Solution {
122+
public:
123+
vector<int> findOriginalArray(vector<int>& changed) {
124+
if (changed.size() % 2 != 0) return {};
125+
int n = 100010;
126+
vector<int> counter(n);
127+
for (int x : changed) ++counter[x];
128+
if (counter[0] % 2 != 0) return {};
129+
vector<int> res(changed.size() / 2);
130+
int j = counter[0] / 2;
131+
for (int i = 1; i < n; ++i)
132+
{
133+
if (counter[i] == 0) continue;
134+
if (i * 2 >= n || counter[i] > counter[i * 2]) return {};
135+
counter[i * 2] -= counter[i];
136+
while (counter[i]--) res[j++] = i;
137+
}
138+
return res;
139+
}
140+
};
141+
```
68142
143+
### **Go**
144+
145+
```go
146+
func findOriginalArray(changed []int) []int {
147+
if len(changed)%2 != 0 {
148+
return []int{}
149+
}
150+
n := 100010
151+
counter := make([]int, n)
152+
for _, x := range changed {
153+
counter[x]++
154+
}
155+
if counter[0]%2 != 0 {
156+
return []int{}
157+
}
158+
var res []int
159+
for j := 0; j < counter[0]/2; j++ {
160+
res = append(res, 0)
161+
}
162+
for i := 1; i < n; i++ {
163+
if counter[i] == 0 {
164+
continue
165+
}
166+
if i*2 >= n || counter[i] > counter[i*2] {
167+
return []int{}
168+
}
169+
for j := 0; j < counter[i]; j++ {
170+
res = append(res, i)
171+
}
172+
counter[i*2] -= counter[i]
173+
}
174+
return res
175+
}
69176
```
70177

71178
### **...**

0 commit comments

Comments
 (0)