思路:从左下或者右上开始进行搜索,因为比如从左下开始搜索,若目标数大于此时的数,接下来只能向右搜,若小于,接着只能向上搜。若是从左上开始进行搜索,若目标数大于此时的数,会有两个方向可以走。
public class Solution { public boolean searchMatrix(int[][] matrix, int target) { int m = matrix.length;//行数 int n = matrix[0].length;//列数 int r = m-1,c =0; while(r >= 0 && c < n) { int tmp = matrix[r][c]; if(target == tmp) return true; else if(target < tmp) r--; else c++; } return false; } }