# 搜索二维矩阵

<p>编写一个高效的算法来判断 <code>m x n</code> 矩阵中，是否存在一个目标值。该矩阵具有如下特性：</p><ul>	<li>每行中的整数从左到右按升序排列。</li>	<li>每行的第一个整数大于前一行的最后一个整数。</li></ul><p> </p><p><strong>示例 1：</strong></p><img alt="" src="https://img-service.csdnimg.cn/img_convert/eb4ca0d861f87843d6d732b4525b0acc.jpeg" style="width: 322px; height: 242px;" /><pre><strong>输入：</strong>matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3<strong><br />输出：</strong>true</pre><p><strong>示例 2：</strong></p><img alt="" src="https://img-service.csdnimg.cn/img_convert/f22033e9dda21437197f5e414c912a3d.jpeg" style="width: 322px; height: 242px;" /><pre><strong>输入：</strong>matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13<strong><br />输出：</strong>false</pre><p> </p><p><strong>提示：</strong></p><ul>	<li><code>m == matrix.length</code></li>	<li><code>n == matrix[i].length</code></li>	<li><code>1 <= m, n <= 100</code></li>	<li><code>-10<sup>4</sup> <= matrix[i][j], target <= 10<sup>4</sup></code></li></ul>

以下程序实现了这一功能，请你填补空白处内容：

```cpp
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
static int binary_search(int *nums, int len, int target)
{
	int low = -1;
	int high = len;
	while (low + 1 < high)
	{
		int mid = low + (high - low) / 2;
		if (target > nums[mid])
		{
			low = mid;
		}
		else
		{
			high = mid;
		}
	}
	______________________;
	else
	{
		return high;
	}
}
static bool searchMatrix(int **matrix, int matrixRowSize, int matrixColSize, int target)
{
	if (matrixRowSize == 0 || matrixColSize == 0)
	{
		return false;
	}
	if (target < matrix[0][0] || target > matrix[matrixRowSize - 1][matrixColSize - 1])
	{
		return false;
	}
	int row = 0;
	int *nums = NULL;
	if (matrixRowSize > 0)
	{
		nums = malloc(matrixRowSize * sizeof(int));
		for (row = 0; row < matrixRowSize; row++)
		{
			nums[row] = matrix[row][0];
		}
		row = binary_search(nums, matrixRowSize, target);
		if (row >= 0)
		{
			return true;
		}
		else
		{
			row = -row - 1;
			if (row == 0)
			{
				return false;
			}
			else
			{
				row--;
			}
		}
	}
	int col = binary_search(matrix[row], matrixColSize, target);
	return col >= 0;
}
int main(int argc, char **argv)
{
	int row = 3;
	int col = 4;
	int **mat = malloc(row * sizeof(int *));
	mat[0] = malloc(col * sizeof(int));
	mat[0][0] = 1;
	mat[0][1] = 3;
	mat[0][2] = 5;
	mat[0][3] = 7;
	mat[1] = malloc(col * sizeof(int));
	mat[1][0] = 10;
	mat[1][1] = 11;
	mat[1][2] = 16;
	mat[1][3] = 20;
	mat[2] = malloc(col * sizeof(int));
	mat[2][0] = 23;
	mat[2][1] = 30;
	mat[2][2] = 34;
	mat[2][3] = 50;
	printf("%s\n", searchMatrix(mat, row, col, atoi(argv[1])) ? "true" : "false");
	return 0;
}
```

## template

```cpp
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
static int binary_search(int *nums, int len, int target)
{
	int low = -1;
	int high = len;
	while (low + 1 < high)
	{
		int mid = low + (high - low) / 2;
		if (target > nums[mid])
		{
			low = mid;
		}
		else
		{
			high = mid;
		}
	}
	if (high == len || nums[high] != target)
	{
		return -high - 1;
	}
	else
	{
		return high;
	}
}
static bool searchMatrix(int **matrix, int matrixRowSize, int matrixColSize, int target)
{
	if (matrixRowSize == 0 || matrixColSize == 0)
	{
		return false;
	}
	if (target < matrix[0][0] || target > matrix[matrixRowSize - 1][matrixColSize - 1])
	{
		return false;
	}
	int row = 0;
	int *nums = NULL;
	if (matrixRowSize > 0)
	{
		nums = malloc(matrixRowSize * sizeof(int));
		for (row = 0; row < matrixRowSize; row++)
		{
			nums[row] = matrix[row][0];
		}
		row = binary_search(nums, matrixRowSize, target);
		if (row >= 0)
		{
			return true;
		}
		else
		{
			row = -row - 1;
			if (row == 0)
			{
				return false;
			}
			else
			{
				row--;
			}
		}
	}
	int col = binary_search(matrix[row], matrixColSize, target);
	return col >= 0;
}
int main(int argc, char **argv)
{
	int row = 3;
	int col = 4;
	int **mat = malloc(row * sizeof(int *));
	mat[0] = malloc(col * sizeof(int));
	mat[0][0] = 1;
	mat[0][1] = 3;
	mat[0][2] = 5;
	mat[0][3] = 7;
	mat[1] = malloc(col * sizeof(int));
	mat[1][0] = 10;
	mat[1][1] = 11;
	mat[1][2] = 16;
	mat[1][3] = 20;
	mat[2] = malloc(col * sizeof(int));
	mat[2][0] = 23;
	mat[2][1] = 30;
	mat[2][2] = 34;
	mat[2][3] = 50;
	printf("%s\n", searchMatrix(mat, row, col, atoi(argv[1])) ? "true" : "false");
	return 0;
}
```

## 答案

```cpp
if (high == len || nums[high] != target)
{
	return -high - 1;
}
```

## 选项

### A

```cpp
if (high == len || nums[high] != target)
{
	return -high + 1;
}
```

### B

```cpp
if (high == len || nums[high] != target)
{
	return high - 1;
}
```

### C

```cpp
if (high == len || nums[high] != target)
{
	return high + 1;
}
```