Description:
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:
Integers in each row are sorted from left to right.
The first integer of each row is greater than the last integer of the previous row.
Ex:
[
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]
Given target = 3, return true.
问题描述
在从左到右,从上到下的矩阵中,查找目标元素,如果找到,返回TRUE。。。
解法一:
思路:
典型的二分查找问题,把矩阵元素看成数组,全部摊开。。
n * m matrix convert to an array => matrix[x][y] => a[x * m + y]
an array convert to n * m matrix => a[x] =>matrix[x / m][x % m];
Code:
public