74.搜索二维矩阵 发表于 2020-09-28 | 分类于 算法 | 本文总阅读量 次 字数统计: 178 | 阅读时长 ≈ 1 题目描述 题解二分查找先对第一列进行二分查找, 确定出目标值在哪一行, 然后在该行进行查找. 1234567891011121314151617181920212223242526272829303132333435363738public boolean searchMatrix(int[][] matrix, int target) { if (matrix.length < 1 || matrix[0].length<1 || matrix[0][0] > target || matrix[matrix.length - 1][matrix[0].length - 1] < target ) { return false; } int rowIndx = 0; int rleft = 0; int rright = matrix.length - 1; while (rleft <= rright) { int mid = rleft + (rright - rleft) / 2; if (matrix[mid][0] == target) { return true; } else if (matrix[mid][0] > target) { rright = mid - 1; } else { rleft = mid + 1; } } rowIndx = rright; rleft = 0; rright = matrix[0].length - 1; while (rleft <= rright) { int mid = rleft + (rright - rleft) / 2; if (matrix[rowIndx][mid] == target) { return true; } else if (matrix[rowIndx][mid] > target) { rright = mid - 1; } else { rleft = mid + 1; } } return false;} -------------本文结束感谢您的阅读------------- 可以请我喝杯奶茶吗 打赏 微信支付 支付宝