-
Notifications
You must be signed in to change notification settings - Fork 0
/
p74.rs
38 lines (33 loc) · 1.08 KB
/
p74.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
#[test]
fn test() {
use method1::search_matrix;
let matrix1: Vec<Vec<i32>> = [[1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 60]]
.map(|arr| arr.to_vec())
.to_vec();
assert_eq!(search_matrix(matrix1, 3), true);
let matrix2: Vec<Vec<i32>> = [[1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 60]]
.map(|arr| arr.to_vec())
.to_vec();
assert_eq!(search_matrix(matrix2, 13), false);
}
// 二分查找,先变成一维数组吧
mod method1 {
#[allow(unused)]
pub fn search_matrix(matrix: Vec<Vec<i32>>, target: i32) -> bool {
let matrix: Vec<i32> = matrix.into_iter().flatten().collect();
// matrix.binary_search(&target).is_ok() // 作弊,rust自带二分查找
let (mut l, mut r): (usize, usize) = (0, matrix.len());
// [l, r)
while l < r {
let m: usize = l + (r - l) / 2;
if matrix[m] < target {
l = m + 1;
} else if matrix[m] > target {
r = m;
} else {
return true;
}
}
false
}
}