Skip to content

Commit bf83b15

Browse files
authored
feat: add rust solution to lc problem: No.2605 (#1160)
Signed-off-by: xiaolatiao <1628652790@qq.com>
1 parent 1983877 commit bf83b15

File tree

3 files changed

+157
-0
lines changed

3 files changed

+157
-0
lines changed

Diff for: solution/2600-2699/2605.Form Smallest Number From Two Digit Arrays/README.md

+61
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,67 @@ function numberOfTrailingZeros(i: number): number {
424424
}
425425
```
426426

427+
### **Rust**
428+
429+
```rust
430+
impl Solution {
431+
pub fn min_number(nums1: Vec<i32>, nums2: Vec<i32>) -> i32 {
432+
let mut ans = 100;
433+
434+
for &a in &nums1 {
435+
for &b in &nums2 {
436+
if a == b {
437+
ans = std::cmp::min(ans, a);
438+
} else {
439+
ans = std::cmp::min(ans, std::cmp::min(a * 10 + b, b * 10 + a));
440+
}
441+
442+
}
443+
}
444+
445+
ans
446+
}
447+
}
448+
```
449+
450+
```rust
451+
use std::collections::HashMap;
452+
453+
impl Solution {
454+
pub fn min_number(nums1: Vec<i32>, nums2: Vec<i32>) -> i32 {
455+
let mut h1: HashMap<i32, bool> = HashMap::new();
456+
457+
for &n in &nums1 {
458+
h1.insert(n, true);
459+
}
460+
461+
let mut h2: HashMap<i32, bool> = HashMap::new();
462+
for &n in &nums2 {
463+
h2.insert(n, true);
464+
}
465+
466+
let mut a = 0;
467+
let mut b = 0;
468+
for i in 1..10 {
469+
if h1.contains_key(&i) && h2.contains_key(&i) {
470+
return i;
471+
}
472+
473+
if a == 0 && h1.contains_key(&i) {
474+
a = i;
475+
}
476+
477+
if b == 0 && h2.contains_key(&i) {
478+
b = i;
479+
}
480+
}
481+
482+
483+
std::cmp::min(a * 10 + b, b * 10 + a)
484+
}
485+
}
486+
```
487+
427488
### **...**
428489

429490
```

Diff for: solution/2600-2699/2605.Form Smallest Number From Two Digit Arrays/README_EN.md

+61
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,67 @@ function numberOfTrailingZeros(i: number): number {
416416
}
417417
```
418418

419+
### **Rust**
420+
421+
```rust
422+
impl Solution {
423+
pub fn min_number(nums1: Vec<i32>, nums2: Vec<i32>) -> i32 {
424+
let mut ans = 100;
425+
426+
for &a in &nums1 {
427+
for &b in &nums2 {
428+
if a == b {
429+
ans = std::cmp::min(ans, a);
430+
} else {
431+
ans = std::cmp::min(ans, std::cmp::min(a * 10 + b, b * 10 + a));
432+
}
433+
434+
}
435+
}
436+
437+
ans
438+
}
439+
}
440+
```
441+
442+
```rust
443+
use std::collections::HashMap;
444+
445+
impl Solution {
446+
pub fn min_number(nums1: Vec<i32>, nums2: Vec<i32>) -> i32 {
447+
let mut h1: HashMap<i32, bool> = HashMap::new();
448+
449+
for &n in &nums1 {
450+
h1.insert(n, true);
451+
}
452+
453+
let mut h2: HashMap<i32, bool> = HashMap::new();
454+
for &n in &nums2 {
455+
h2.insert(n, true);
456+
}
457+
458+
let mut a = 0;
459+
let mut b = 0;
460+
for i in 1..10 {
461+
if h1.contains_key(&i) && h2.contains_key(&i) {
462+
return i;
463+
}
464+
465+
if a == 0 && h1.contains_key(&i) {
466+
a = i;
467+
}
468+
469+
if b == 0 && h2.contains_key(&i) {
470+
b = i;
471+
}
472+
}
473+
474+
475+
std::cmp::min(a * 10 + b, b * 10 + a)
476+
}
477+
}
478+
```
479+
419480
### **...**
420481

421482
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use std::collections::HashMap;
2+
3+
impl Solution {
4+
pub fn min_number(nums1: Vec<i32>, nums2: Vec<i32>) -> i32 {
5+
let mut h1: HashMap<i32, bool> = HashMap::new();
6+
7+
for &n in &nums1 {
8+
h1.insert(n, true);
9+
}
10+
11+
let mut h2: HashMap<i32, bool> = HashMap::new();
12+
for &n in &nums2 {
13+
h2.insert(n, true);
14+
}
15+
16+
let mut a = 0;
17+
let mut b = 0;
18+
for i in 1..10 {
19+
if h1.contains_key(&i) && h2.contains_key(&i) {
20+
return i;
21+
}
22+
23+
if a == 0 && h1.contains_key(&i) {
24+
a = i;
25+
}
26+
27+
if b == 0 && h2.contains_key(&i) {
28+
b = i;
29+
}
30+
}
31+
32+
33+
std::cmp::min(a * 10 + b, b * 10 + a)
34+
}
35+
}

0 commit comments

Comments
 (0)