#include<iostream>
#include<string.h>
#include<string>
#include<vector>
using namespace std;
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&&columns >= 1)
{
if (matrix[row*columns + column] == number)
{
found == true;
break;
}
else if (matrix[row*columns+column]>number)
{//如果比number大,那么就列减一
--column;
}
else
{//如果比number小,那么就行加一
++row;
}
}
}
return false;
}
void main()
{
int a[] = { 1, 2, 8, 9, 2, 4, 9, 12, 4, 7, 10, 13, 6, 8, 11, 15 };
Find(a, 4, 4, 7);
}
剑指offor,面试题3数组中的查找
最新推荐文章于 2024-08-25 17:50:36 发布