0240.搜索二维矩阵 II

方法一:Z 字形查找

时间复杂度 $O(m+n)$,空间复杂度 $O(1)$。

impl Solution {
    pub fn search_matrix(matrix: Vec<Vec<i32>>, target: i32) -> bool {
        let (m, n) = (matrix.len(), matrix[0].len());
        let (mut x, mut y) = (0, (n - 1) as i32);
        while x < m && y >= 0 {
            if matrix[x][y as usize] > target {
                y -= 1;
            } else if matrix[x][y as usize] < target {
                x += 1;
            } else {
                return true;
            }
        }
        false
    }
}