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 b563d43

Browse files
authored
feat: add rust solution to lc problem: No.2570 (doocs#1204)
1 parent 382cda4 commit b563d43

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,33 @@ function mergeArrays(nums1: number[][], nums2: number[][]): number[][] {
182182
}
183183
```
184184

185+
### **Rust**
186+
187+
```rust
188+
impl Solution {
189+
pub fn merge_arrays(nums1: Vec<Vec<i32>>, nums2: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
190+
let mut cnt = vec![0; 1001];
191+
192+
for x in &nums1 {
193+
cnt[x[0] as usize] += x[1];
194+
}
195+
196+
for x in &nums2 {
197+
cnt[x[0] as usize] += x[1];
198+
}
199+
200+
let mut ans = vec![];
201+
for i in 0..cnt.len() {
202+
if cnt[i] > 0 {
203+
ans.push(vec![i as i32, cnt[i] as i32]);
204+
}
205+
}
206+
207+
ans
208+
}
209+
}
210+
```
211+
185212
### **...**
186213

187214
```

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,33 @@ function mergeArrays(nums1: number[][], nums2: number[][]): number[][] {
166166
}
167167
```
168168

169+
### **Rust**
170+
171+
```rust
172+
impl Solution {
173+
pub fn merge_arrays(nums1: Vec<Vec<i32>>, nums2: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
174+
let mut cnt = vec![0; 1001];
175+
176+
for x in &nums1 {
177+
cnt[x[0] as usize] += x[1];
178+
}
179+
180+
for x in &nums2 {
181+
cnt[x[0] as usize] += x[1];
182+
}
183+
184+
let mut ans = vec![];
185+
for i in 0..cnt.len() {
186+
if cnt[i] > 0 {
187+
ans.push(vec![i as i32, cnt[i] as i32]);
188+
}
189+
}
190+
191+
ans
192+
}
193+
}
194+
```
195+
169196
### **...**
170197

171198
```
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
impl Solution {
2+
pub fn merge_arrays(nums1: Vec<Vec<i32>>, nums2: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
3+
let mut cnt = vec![0; 1001];
4+
5+
for x in &nums1 {
6+
cnt[x[0] as usize] += x[1];
7+
}
8+
9+
for x in &nums2 {
10+
cnt[x[0] as usize] += x[1];
11+
}
12+
13+
let mut ans = vec![];
14+
for i in 0..cnt.len() {
15+
if cnt[i] > 0 {
16+
ans.push(vec![i as i32, cnt[i] as i32]);
17+
}
18+
}
19+
20+
ans
21+
}
22+
}

0 commit comments

Comments
(0)

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