面试题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
相关文章推荐
- 2016Android某公司面试题
- Android面试一天一题(吹牛题)
- Java高级开发工程师面试考纲
- 成为优秀程序员必备的七点
- Android面试一天一题(16 Day: 线程同步)
- Android面试一天一题(15 Day:ContentProvider)
- Android面试一天一题(14 Day:SharedPreferences)
- Android面试一天一题(13 Day: AsyncTask)
- Android面试一天一题(12 Day)
- Android面试一天一题(11 Day)
- Android面试一天一题(10 Day)
- Android面试一天一题(9 Day)
- Android面试一天一题(8 Day)
- Android面试一天一题(7 Day)
- Android面试一天一题(6 Day)
- Android面试一天一题(5 Day)
- Android面试一天一题(4 Day)
- Android面试一天一题(3 Day)
- Android面试一天一题(2 Day)
- iOS面试题一