原题链接:74. Search a 2D Matrix
【思路1-Java】二分查找|T=O(logn*m)|M=O(1)
这种思路是利用一位数组二分查找的思想,先计算出数组总个数,并计算出中间元素的下标。将这个中间元素坐标映射成二维数组坐标:
public class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int r = matrix.length, c = matrix[0].length, right = c * r - 1, left = 0;
while (left <= right) {
int mid = left+(right-left)/2;
if(matrix[mid/c][mid-mid/c*c] < target) left = mid + 1;
else right = mid - 1;
}
return left < c * r && matrix[left/c][left-left/c*c] == target;
}
}