# Search a 2D Matrix

> 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.
For example,

> Consider the following matrix:

```
[
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]
```

1. 对于每一行，数值是从左到右从小到大排列的.
2. 对于每一列，数值是从上到下从小到大排列的.

```c++
class Solution {
public:
bool searchMatrix(vector > &matrix, int target) {
/* we set the corner case as below:
1, if the row number of input matrix is 0, we set it false
2, if the colomun number of input matrix is 0, we set it false*/
if(matrix.size() == 0)
return false;
if(matrix[0].size() == 0)
return false;
int rowNumber = 0;
int colNumber = matrix[0].size()-1;
while(rowNumber < matrix.size() && colNumber >= 0)
{
if(target < matrix[rowNumber][colNumber])
--colNumber;
else if(target > matrix[rowNumber][colNumber])
++rowNumber;
else
return true;
}
return false;
}
};

```