/*面试题3
题目:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。编写一个函数,输入一个这样的二维数组和一个整数,判断数组中是否含有该函数。
总结如下的查找规律:首先选取数组中右上角的数字。相等则查找过程结束;如果该数字大于要查找的数字,则剔除这个数字所在的列;如果该数字小于要查找的数字,剔除这个数字所在的行。*/
bool Find(int* matrix,int rows,int columns,int number)
{
bool found = false;
if(matrix != NULL && rows > 0 && columns > 0)
{
int row = 0;
int column = columns -1;
while(row < rows && column >= 0)
{
if(matrix[row * columns +column] == number)
{
found =true;
break;
}
else if(matrix[row * columns + column] > number)
--column;
else
++ row;
}
}
return found;
}
//测试用例包括二维数组中包含的数字,不包含的数字,特殊输入(空指针)测试。
//二维数组在内存空间中占据连续的空间。
题目:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。编写一个函数,输入一个这样的二维数组和一个整数,判断数组中是否含有该函数。
总结如下的查找规律:首先选取数组中右上角的数字。相等则查找过程结束;如果该数字大于要查找的数字,则剔除这个数字所在的列;如果该数字小于要查找的数字,剔除这个数字所在的行。*/
bool Find(int* matrix,int rows,int columns,int number)
{
bool found = false;
if(matrix != NULL && rows > 0 && columns > 0)
{
int row = 0;
int column = columns -1;
while(row < rows && column >= 0)
{
if(matrix[row * columns +column] == number)
{
found =true;
break;
}
else if(matrix[row * columns + column] > number)
--column;
else
++ row;
}
}
return found;
}
//测试用例包括二维数组中包含的数字,不包含的数字,特殊输入(空指针)测试。
//二维数组在内存空间中占据连续的空间。