Skip to content

Commit 00ac913

Browse files
authored
feat: add ts solution to lc problem: No.2570 (doocs#898)
No.2570. Merge Two 2D Arrays by Summing Values
1 parent a401602 commit 00ac913

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

solution/2500-2599/2570.Merge Two 2D Arrays by Summing Values/README.md

+22
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,28 @@ func mergeArrays(nums1 [][]int, nums2 [][]int) (ans [][]int) {
160160
}
161161
```
162162

163+
### **TypeScript**
164+
165+
```ts
166+
function mergeArrays(nums1: number[][], nums2: number[][]): number[][] {
167+
const n = 1001;
168+
const cnt = new Array(n).fill(0);
169+
for (const [a, b] of nums1) {
170+
cnt[a] += b;
171+
}
172+
for (const [a, b] of nums2) {
173+
cnt[a] += b;
174+
}
175+
const ans: number[][] = [];
176+
for (let i = 0; i < n; ++i) {
177+
if (cnt[i] > 0) {
178+
ans.push([i, cnt[i]]);
179+
}
180+
}
181+
return ans;
182+
}
183+
```
184+
163185
### **...**
164186

165187
```

solution/2500-2599/2570.Merge Two 2D Arrays by Summing Values/README_EN.md

+22
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,28 @@ func mergeArrays(nums1 [][]int, nums2 [][]int) (ans [][]int) {
144144
}
145145
```
146146

147+
### **TypeScript**
148+
149+
```ts
150+
function mergeArrays(nums1: number[][], nums2: number[][]): number[][] {
151+
const n = 1001;
152+
const cnt = new Array(n).fill(0);
153+
for (const [a, b] of nums1) {
154+
cnt[a] += b;
155+
}
156+
for (const [a, b] of nums2) {
157+
cnt[a] += b;
158+
}
159+
const ans: number[][] = [];
160+
for (let i = 0; i < n; ++i) {
161+
if (cnt[i] > 0) {
162+
ans.push([i, cnt[i]]);
163+
}
164+
}
165+
return ans;
166+
}
167+
```
168+
147169
### **...**
148170

149171
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function mergeArrays(nums1: number[][], nums2: number[][]): number[][] {
2+
const n = 1001;
3+
const cnt = new Array(n).fill(0);
4+
for (const [a, b] of nums1) {
5+
cnt[a] += b;
6+
}
7+
for (const [a, b] of nums2) {
8+
cnt[a] += b;
9+
}
10+
const ans: number[][] = [];
11+
for (let i = 0; i < n; ++i) {
12+
if (cnt[i] > 0) {
13+
ans.push([i, cnt[i]]);
14+
}
15+
}
16+
return ans;
17+
}

0 commit comments

Comments
 (0)