Write an efficient algorithm that searches for a value target in an m x n integer matrix matrix. This matrix has the following properties:

  • Integers in each row are sorted in ascending from left to right.
  • Integers in each column are sorted in ascending from top to bottom.

二维矩阵的搜索策略 , 从最后一行的行首开始搜索 (保证大或者小的行走方向唯一) 如果遇到小于target的元素 , 向右走一行 如果遇到大于target的元素 , 向上走一行 如果遇到等于target的元素,向上走一行 + 向右走一行

impl Solution {
       pub fn search_matrix(matrix: Vec<Vec<i32>>, target: i32) -> bool {
        let row_size = matrix.len();
        let col_size = matrix[0].len();
 
        let mut r = row_size as i32 - 1;
        let mut c = 0_i32;
 
        while r >= 0 && c < col_size as i32 {
            // 最后一层,最左侧
            if matrix[r as usize][c as usize] < target {
                c += 1;
            } else if matrix[r as usize][c as usize] > target {
                r -= 1;
            } else {
                c += 1;
                r -= 1;
                return true;
            }
        }
 
        false
    }
}

#算法/二分