Skip to content

Commit b005a39

Browse files
committed
feat: add rust solution to lcof problem: No.04
剑指 Offer 04. 二维数组中的查找
1 parent 8f8dd00 commit b005a39

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

lcof/面试题04. 二维数组中的查找/README.md

+31
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,37 @@ function findNumberIn2DArray(matrix: number[][], target: number): boolean {
179179
}
180180
```
181181

182+
### **Rust**
183+
184+
```rust
185+
impl Solution {
186+
pub fn find_number_in2_d_array(matrix: Vec<Vec<i32>>, target: i32) -> bool {
187+
let len_y = matrix.len();
188+
if len_y == 0 {
189+
return false;
190+
}
191+
let len_x = matrix[0].len();
192+
if len_x == 0 {
193+
return false;
194+
}
195+
196+
let mut x = len_x - 1;
197+
let mut y = 0;
198+
while y < len_y {
199+
match target.cmp(&matrix[y][x]) {
200+
std::cmp::Ordering::Greater => y += 1,
201+
std::cmp::Ordering::Equal => return true,
202+
std::cmp::Ordering::Less => match x {
203+
0 => return false,
204+
_ => x -= 1,
205+
},
206+
}
207+
}
208+
false
209+
}
210+
}
211+
```
212+
182213
### **...**
183214

184215
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
impl Solution {
2+
pub fn find_number_in2_d_array(matrix: Vec<Vec<i32>>, target: i32) -> bool {
3+
let len_y = matrix.len();
4+
if len_y == 0 {
5+
return false;
6+
}
7+
let len_x = matrix[0].len();
8+
if len_x == 0 {
9+
return false;
10+
}
11+
12+
let mut x = len_x - 1;
13+
let mut y = 0;
14+
while y < len_y {
15+
match target.cmp(&matrix[y][x]) {
16+
std::cmp::Ordering::Greater => y += 1,
17+
std::cmp::Ordering::Equal => return true,
18+
std::cmp::Ordering::Less => match x {
19+
0 => return false,
20+
_ => x -= 1,
21+
},
22+
}
23+
}
24+
false
25+
}
26+
}

0 commit comments

Comments
 (0)