@@ -153,7 +153,76 @@ func mergeSimilarItems(items1 [][]int, items2 [][]int) [][]int {
153
153
### ** TypeScript**
154
154
155
155
``` 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
+ ```
156
193
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
+ }
157
226
```
158
227
159
228
### **...**
0 commit comments