您的位置:首页 > 职场人生

面试题3:二维数组中的查找

2016-07-01 09:05 323 查看

知识点:

创建数组,首先需要指定数组的容量大小

数组可以实现简单的哈希表,把数组的下标设为哈希表的键值(Key),数组中每一个数字设为哈希表的值(Value)

动态数组,如STL中的vector。当数据的数目超过数组容量会进行扩容,新的容量都是前一次的两倍,把之前的数据复制到新的数组中,再把之前的内存释放。这样对时间性能有负面影响,因此使用动态数组要尽量减少改变数组容量大小

数组名其实是一个指针,指向数组的第一个元素

int data1[] = { 1, 2, 3, 4, 5 };
int *data2 = data1;
cout << sizeof(data1) << endl << sizeof(data2) << endl;


输出:20 4

sizeof(data1)求整个数组大小,sizeof(data2)为指针大小

面试题3

从二维数组的右上角或者左下角向内逐渐收缩,注意接受参数是个指针,用数组表示的话只能用一维数组

自己的实现:

bool Find(int* matrix, int rows, int columns, int number)
{
if (matrix == NULL)
return false;
int row = 0;
int column = columns -1;
for (column; column > 0; column--)    //排查第一行
{
if (matrix[column] == number)
return true;
if (matrix[column] < number)
break;
}
for (row; row < rows; row++)    //确定行号后检查对应行的列
{
if (matrix[row * columns + column] == number)
return true;
if (matrix[row * columns + column] > number)
return false;
}
return false;
}


书中示例:

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;
}


完整代码:

// FindInPartiallySortedMatrix.cpp : Defines the entry point for the console application.
//

// 《剑指Offer——名企面试官精讲典型编程题》代码
// 著作权所有者:何海涛

#include "stdafx.h"

// 二维数组matrix中,每一行都从左到右递增排序,
// 每一列都从上到下递增排序
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;
}

// ====================测试代码====================
void Test(char* testName, int* matrix, int rows, int columns, int number, bool expected)
{
if(testName != NULL)
printf("%s begins: ", testName);

bool result = Find(matrix, rows, columns, number);
if(result == expected)
printf("Passed.\n");
else
printf("Failed.\n");
}

//  1   2   8   9
//  2   4   9   12
//  4   7   10  13
//  6   8   11  15
// 要查找的数在数组中
void Test1()
{
int matrix[][4] = {{1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}};
Test("Test1", (int*)matrix, 4, 4, 7, true);
}

//  1   2   8   9
//  2   4   9   12
//  4   7   10  13
//  6   8   11  15
// 要查找的数不在数组中
void Test2()
{
int matrix[][4] = {{1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}};
Test("Test2", (int*)matrix, 4, 4, 5, false);
}

//  1   2   8   9
//  2   4   9   12
//  4   7   10  13
//  6   8   11  15
// 要查找的数是数组中最小的数字
void Test3()
{
int matrix[][4] = {{1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}};
Test("Test3", (int*)matrix, 4, 4, 1, true);
}

//  1   2   8   9
//  2   4   9   12
//  4   7   10  13
//  6   8   11  15
// 要查找的数是数组中最大的数字
void Test4()
{
int matrix[][4] = {{1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}};
Test("Test4", (int*)matrix, 4, 4, 15, true);
}

//  1   2   8   9
//  2   4   9   12
//  4   7   10  13
//  6   8   11  15
// 要查找的数比数组中最小的数字还小
void Test5()
{
int matrix[][4] = {{1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}};
Test("Test5", (int*)matrix, 4, 4, 0, false);
}

//  1   2   8   9
//  2   4   9   12
//  4   7   10  13
//  6   8   11  15
// 要查找的数比数组中最大的数字还大
void Test6()
{
int matrix[][4] = {{1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}};
Test("Test6", (int*)matrix, 4, 4, 16, false);
}

// 鲁棒性测试,输入空指针
void Test7()
{
Test("Test7", NULL, 0, 0, 16, false);
}

int _tmain(int argc, _TCHAR* argv[])
{
Test1();
Test2();
Test3();
Test4();
Test5();
Test6();
Test7();

return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: