Skip to content

Commit da8ccaa

Browse files
committed
feat: add rust solution to lc problem: No.0240
No.0240.Search a 2D Matrix II
1 parent 666b55c commit da8ccaa

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

solution/0200-0299/0240.Search a 2D Matrix II/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,29 @@ public class Solution {
181181
}
182182
```
183183

184+
### **Rust**
185+
186+
```rust
187+
use std::cmp::Ordering;
188+
189+
impl Solution {
190+
pub fn search_matrix(matrix: Vec<Vec<i32>>, target: i32) -> bool {
191+
let m = matrix.len();
192+
let n = matrix[0].len();
193+
let mut i = 0;
194+
let mut j = n;
195+
while i < m && j > 0 {
196+
match target.cmp(&matrix[i][j - 1]) {
197+
Ordering::Less => j -= 1,
198+
Ordering::Greater => i += 1,
199+
Ordering::Equal => return true,
200+
}
201+
}
202+
false
203+
}
204+
}
205+
```
206+
184207
### **...**
185208

186209
```

solution/0200-0299/0240.Search a 2D Matrix II/README_EN.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,29 @@ public class Solution {
169169
}
170170
```
171171

172+
### **Rust**
173+
174+
```rust
175+
use std::cmp::Ordering;
176+
177+
impl Solution {
178+
pub fn search_matrix(matrix: Vec<Vec<i32>>, target: i32) -> bool {
179+
let m = matrix.len();
180+
let n = matrix[0].len();
181+
let mut i = 0;
182+
let mut j = n;
183+
while i < m && j > 0 {
184+
match target.cmp(&matrix[i][j - 1]) {
185+
Ordering::Less => j -= 1,
186+
Ordering::Greater => i += 1,
187+
Ordering::Equal => return true,
188+
}
189+
}
190+
false
191+
}
192+
}
193+
```
194+
172195
### **...**
173196

174197
```
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use std::cmp::Ordering;
2+
3+
impl Solution {
4+
pub fn search_matrix(matrix: Vec<Vec<i32>>, target: i32) -> bool {
5+
let m = matrix.len();
6+
let n = matrix[0].len();
7+
let mut i = 0;
8+
let mut j = n;
9+
while i < m && j > 0 {
10+
match target.cmp(&matrix[i][j - 1]) {
11+
Ordering::Less => j -= 1,
12+
Ordering::Greater => i += 1,
13+
Ordering::Equal => return true,
14+
}
15+
}
16+
false
17+
}
18+
}

0 commit comments

Comments
 (0)