【LeetCode-240】Search a 2D Matrix II

来源:互联网 发布:snmp可以提供什么数据 编辑:程序博客网 时间:2024/06/07 20:35

思想全在代码里注释了,来吧,come on

/** * 从右上角进行搜索,如果target > matrix[i][j],则证明不在这一行,则i ++ * 如果target < matrix[i][j],则证明不在这一列,则j -- * 如果target = matrix[i][j],则返回 * @author mh * */public class Searcha2DMatrixII {public boolean searchMatrix(int[][] matrix, int target) {if(target < matrix[0][0] || target > matrix[matrix.length - 1][matrix[0].length - 1]){return false;}int i = 0,j = matrix[0].length - 1;while(i < matrix.length && j >= 0){if(matrix[i][j] > target){    j --;}else if(matrix[i][j] < target){i ++;}else{return true;}}//碰到边界,则返回falsereturn false;    }}


0 0