File tree Expand file tree Collapse file tree 1 file changed +7
-1
lines changed Expand file tree Collapse file tree 1 file changed +7
-1
lines changed Original file line number Diff line number Diff line change @@ -8,6 +8,7 @@ impl Solution {
8
8
// 方法1
9
9
// 直接迭代matrix,因为有序,内部vec采用二分查找,存在target即返回true
10
10
// 迭代完返回false
11
+ // AC 4ms 2.5mb 129/129
11
12
// matrix.iter().find(|&x| x.binary_search(&target).is_ok()).is_some()
12
13
13
14
// 方法2
@@ -18,10 +19,15 @@ impl Solution {
18
19
// 循环上述三行,直到row = matrix.len() - 1,column = 0(最后一行,第一列)
19
20
// 都没有找到,说明数字不在其中,返回false
20
21
// 注意rust中column为0时减去1会溢出成最大的那个数字,所以判断条件是column < columns而不是column >= 0
22
+ // AC 4ms 2.7mb
21
23
let ( rows, columns) = ( matrix. len ( ) , if matrix. is_empty ( ) { 0 } else { matrix[ 0 ] . len ( ) } ) ;
22
24
let ( mut row, mut column) = ( 0 , columns - 1 ) ;
23
25
while row < rows && column < columns {
24
- if matrix[ row] [ column] < target { row += 1 ; } else if matrix[ row] [ column] > target { column -= 1 ; } else { return true ; }
26
+ match matrix[ row] [ column] . cmp ( & target) {
27
+ std:: cmp:: Ordering :: Greater => column -= 1 ,
28
+ std:: cmp:: Ordering :: Less => row += 1 ,
29
+ std:: cmp:: Ordering :: Equal => return true ,
30
+ }
25
31
}
26
32
false
27
33
}
You can’t perform that action at this time.
0 commit comments