Skip to content

Commit 595d426

Browse files
author
ruislan
committed
updated q240
1 parent bf3aad0 commit 595d426

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/q/q240.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ impl Solution {
88
// 方法1
99
// 直接迭代matrix,因为有序,内部vec采用二分查找,存在target即返回true
1010
// 迭代完返回false
11+
// AC 4ms 2.5mb 129/129
1112
// matrix.iter().find(|&x| x.binary_search(&target).is_ok()).is_some()
1213

1314
// 方法2
@@ -18,10 +19,15 @@ impl Solution {
1819
// 循环上述三行,直到row = matrix.len() - 1,column = 0(最后一行,第一列)
1920
// 都没有找到,说明数字不在其中,返回false
2021
// 注意rust中column为0时减去1会溢出成最大的那个数字,所以判断条件是column < columns而不是column >= 0
22+
// AC 4ms 2.7mb
2123
let (rows, columns) = (matrix.len(), if matrix.is_empty() { 0 } else { matrix[0].len() });
2224
let (mut row, mut column) = (0, columns - 1);
2325
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+
}
2531
}
2632
false
2733
}

0 commit comments

Comments
 (0)