Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 15fba16

Browse files
committed
feat: add solutions to lc problem: No.2363
No.2363.Merge Similar Items
1 parent 8f28dfb commit 15fba16

File tree

5 files changed

+198
-0
lines changed

5 files changed

+198
-0
lines changed

‎solution/2300-2399/2363.Merge Similar Items/README.md‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,76 @@ func mergeSimilarItems(items1 [][]int, items2 [][]int) [][]int {
153153
### **TypeScript**
154154

155155
```ts
156+
function mergeSimilarItems(items1: number[][], items2: number[][]): number[][] {
157+
const count = new Array(1001).fill(0);
158+
for (const [v, w] of items1) {
159+
count[v] += w;
160+
}
161+
for (const [v, w] of items2) {
162+
count[v] += w;
163+
}
164+
return [...count.entries()].filter(v => v[1] !== 0);
165+
}
166+
```
167+
168+
### **Rust**
169+
170+
```rust
171+
impl Solution {
172+
pub fn merge_similar_items(items1: Vec<Vec<i32>>, items2: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
173+
let mut count = [0; 1001];
174+
for item in items1.iter() {
175+
count[item[0] as usize] += item[1];
176+
}
177+
for item in items2.iter() {
178+
count[item[0] as usize] += item[1];
179+
}
180+
count
181+
.iter()
182+
.enumerate()
183+
.filter_map(|(i, &v)| {
184+
if v == 0 {
185+
return None;
186+
}
187+
Some(vec![i as i32, v])
188+
})
189+
.collect()
190+
}
191+
}
192+
```
156193

194+
### **C**
195+
196+
```c
197+
/**
198+
* Return an array of arrays of size *returnSize.
199+
* The sizes of the arrays are returned as *returnColumnSizes array.
200+
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
201+
*/
202+
int **mergeSimilarItems(int **items1, int items1Size, int *items1ColSize, int **items2, int items2Size,
203+
int *items2ColSize, int *returnSize, int **returnColumnSizes) {
204+
int count[1001] = {0};
205+
for (int i = 0; i < items1Size; i++) {
206+
count[items1[i][0]] += items1[i][1];
207+
}
208+
for (int i = 0; i < items2Size; i++) {
209+
count[items2[i][0]] += items2[i][1];
210+
}
211+
int **ans = malloc(sizeof(int *) * (items1Size + items2Size));
212+
*returnColumnSizes = malloc(sizeof(int) * (items1Size + items2Size));
213+
int size = 0;
214+
for (int i = 0; i < 1001; i++) {
215+
if (count[i]) {
216+
ans[size] = malloc(sizeof(int) * 2);
217+
ans[size][0] = i;
218+
ans[size][1] = count[i];
219+
(*returnColumnSizes)[size] = 2;
220+
size++;
221+
}
222+
}
223+
*returnSize = size;
224+
return ans;
225+
}
157226
```
158227
159228
### **...**

‎solution/2300-2399/2363.Merge Similar Items/README_EN.md‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,76 @@ func mergeSimilarItems(items1 [][]int, items2 [][]int) [][]int {
143143
### **TypeScript**
144144

145145
```ts
146+
function mergeSimilarItems(items1: number[][], items2: number[][]): number[][] {
147+
const count = new Array(1001).fill(0);
148+
for (const [v, w] of items1) {
149+
count[v] += w;
150+
}
151+
for (const [v, w] of items2) {
152+
count[v] += w;
153+
}
154+
return [...count.entries()].filter(v => v[1] !== 0);
155+
}
156+
```
157+
158+
### **Rust**
159+
160+
```rust
161+
impl Solution {
162+
pub fn merge_similar_items(items1: Vec<Vec<i32>>, items2: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
163+
let mut count = [0; 1001];
164+
for item in items1.iter() {
165+
count[item[0] as usize] += item[1];
166+
}
167+
for item in items2.iter() {
168+
count[item[0] as usize] += item[1];
169+
}
170+
count
171+
.iter()
172+
.enumerate()
173+
.filter_map(|(i, &v)| {
174+
if v == 0 {
175+
return None;
176+
}
177+
Some(vec![i as i32, v])
178+
})
179+
.collect()
180+
}
181+
}
182+
```
146183

184+
### **C**
185+
186+
```c
187+
/**
188+
* Return an array of arrays of size *returnSize.
189+
* The sizes of the arrays are returned as *returnColumnSizes array.
190+
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
191+
*/
192+
int **mergeSimilarItems(int **items1, int items1Size, int *items1ColSize, int **items2, int items2Size,
193+
int *items2ColSize, int *returnSize, int **returnColumnSizes) {
194+
int count[1001] = {0};
195+
for (int i = 0; i < items1Size; i++) {
196+
count[items1[i][0]] += items1[i][1];
197+
}
198+
for (int i = 0; i < items2Size; i++) {
199+
count[items2[i][0]] += items2[i][1];
200+
}
201+
int **ans = malloc(sizeof(int *) * (items1Size + items2Size));
202+
*returnColumnSizes = malloc(sizeof(int) * (items1Size + items2Size));
203+
int size = 0;
204+
for (int i = 0; i < 1001; i++) {
205+
if (count[i]) {
206+
ans[size] = malloc(sizeof(int) * 2);
207+
ans[size][0] = i;
208+
ans[size][1] = count[i];
209+
(*returnColumnSizes)[size] = 2;
210+
size++;
211+
}
212+
}
213+
*returnSize = size;
214+
return ans;
215+
}
147216
```
148217
149218
### **...**
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Return an array of arrays of size *returnSize.
3+
* The sizes of the arrays are returned as *returnColumnSizes array.
4+
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
5+
*/
6+
int **mergeSimilarItems(int **items1, int items1Size, int *items1ColSize, int **items2, int items2Size,
7+
int *items2ColSize, int *returnSize, int **returnColumnSizes) {
8+
int count[1001] = {0};
9+
for (int i = 0; i < items1Size; i++) {
10+
count[items1[i][0]] += items1[i][1];
11+
}
12+
for (int i = 0; i < items2Size; i++) {
13+
count[items2[i][0]] += items2[i][1];
14+
}
15+
int **ans = malloc(sizeof(int *) * (items1Size + items2Size));
16+
*returnColumnSizes = malloc(sizeof(int) * (items1Size + items2Size));
17+
int size = 0;
18+
for (int i = 0; i < 1001; i++) {
19+
if (count[i]) {
20+
ans[size] = malloc(sizeof(int) * 2);
21+
ans[size][0] = i;
22+
ans[size][1] = count[i];
23+
(*returnColumnSizes)[size] = 2;
24+
size++;
25+
}
26+
}
27+
*returnSize = size;
28+
return ans;
29+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
impl Solution {
2+
pub fn merge_similar_items(items1: Vec<Vec<i32>>, items2: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
3+
let mut count = [0; 1001];
4+
for item in items1.iter() {
5+
count[item[0] as usize] += item[1];
6+
}
7+
for item in items2.iter() {
8+
count[item[0] as usize] += item[1];
9+
}
10+
count
11+
.iter()
12+
.enumerate()
13+
.filter_map(|(i, &v)| {
14+
if v == 0 {
15+
return None;
16+
}
17+
Some(vec![i as i32, v])
18+
})
19+
.collect()
20+
}
21+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function mergeSimilarItems(items1: number[][], items2: number[][]): number[][] {
2+
const count = new Array(1001).fill(0);
3+
for (const [v, w] of items1) {
4+
count[v] += w;
5+
}
6+
for (const [v, w] of items2) {
7+
count[v] += w;
8+
}
9+
return [...count.entries()].filter(v => v[1] !== 0);
10+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /