二维数组中的查找
二维数组中的查找
https://www.nowcoder.com/practice/abc3fe2ce8e146608e868a70efebf62e?tpId=13&tqId=11154&rp=1&ru=%2Fta%2Fcoding-interviews&qru=%2Fta%2Fcoding-interviews%2Fquestion-ranking&tab=answerKey
使用双指针,从左下角开始寻找,利用行列都是递增的属性去进行遍历
public boolean findNumberIn2DArray(int[][] matrix, int target) { if(matrix.length == 0) return false; int rows = matrix.length; int cols = matrix[0].length; int bottom = rows-1; int left = 0; while(bottom >= 0 && left < cols){ int nowNum = matrix[bottom][left]; if(nowNum == target){ return true; }else if(nowNum < target){ // 往右找 left++; }else { bottom--; //往上找 } } return false; }
剑指offer 文章被收录于专栏
为刷过的每一道题都书写一篇题解,便于重复练习~