-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths0240_search_a_2d_matrix_ii.rs
61 lines (55 loc) · 1.42 KB
/
s0240_search_a_2d_matrix_ii.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#![allow(unused)]
pub struct Solution {}
impl Solution {
// O(n + m) O(1)
pub fn search_matrix(matrix: Vec<Vec<i32>>, target: i32) -> bool {
if matrix.len() == 0 || matrix[0].len() == 0 {
return false;
}
// start out pointer in the bottom-left
let (mut row, mut col) = (matrix.len() as i32 -1, 0);
while row >= 0 && col < matrix[0].len() {
if matrix[row as usize][col] > target {
row -= 1;
} else if matrix[row as usize][col] < target {
col += 1;
} else {
return true;
}
}
false
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_240() {
assert_eq!(
Solution::search_matrix(
vec![
vec![1, 4, 7, 11, 15],
vec![2, 5, 8, 12, 19],
vec![3, 6, 9, 16, 22],
vec![10, 13, 14, 17, 24],
vec![18, 21, 23, 26, 30]
],
5
),
true
);
assert_eq!(
Solution::search_matrix(
vec![
vec![1, 4, 7, 11, 15],
vec![2, 5, 8, 12, 19],
vec![3, 6, 9, 16, 22],
vec![10, 13, 14, 17, 24],
vec![18, 21, 23, 26, 30]
],
20
),
false
);
}
}