解题思路:
两次二分,第一次定位行,第二次定位列。
class Solution {public boolean searchMatrix(int[][] matrix, int target) {int m = matrix.length, n = matrix[0].length;int l = 0, r = m - 1;//定位行int row = -1;while (l <= r) {int mid = l + (r - l) / 2;if (matrix[mid][0] <= target && matrix[mid][n - 1] >= target) {row = mid;break;} else if (target < matrix[mid][0]) {r = mid - 1;} else {l = mid + 1;}}if (row == -1) return false;//定位列l = 0;r = n - 1;while (l <= r) {int mid = l + (r - l) / 2;//已知行row之后,就按照常规的二分来做if (target == matrix[row][mid])return true;else if (target < matrix[row][mid])r = mid - 1;elsel = mid + 1;}return false;}
}