【剑指offer】二维数组中查找_03
来源:互联网 发布:sql replace函数用法 编辑:程序博客网 时间:2024/05/16 03:58
#include <iostream.h>#include <string>//#注意知识点//1. 当问题比较复杂时,应该通过具体的例子找到规律;//2. 二维数组中,应该通过行号和列号计算出相对于数组首地址的偏移量;//3. 注意函数中的数组参数怎么定义,怎么传参数,又是怎么取元素的;//4. 注意二维数组的定义;居然都忘了;//自己写的,验证没问题/*bool Find(int *matrix,int rows,int columns,int number){if(matrix==NULL || rows<=0 || columns<=0){return false;}int i=0,j=columns-1;while(i<rows && j>=0){//不能这样访问,因为传进来的是指针matrix[i][j]if(matrix[i*columns+j]==number){return true;}else if(matrix[i*columns+j]>number){j--;}else{i++;}}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;}//========测试代码=============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");elseprintf("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);}void main(){ Test1(); Test2(); Test3(); Test4(); Test5(); Test6(); Test7();}
0 0
- 【剑指offer】二维数组中查找_03
- 剑指offer-二维数组查找
- 剑指Offer--二维数组查找
- 剑指offer 二维数组查找
- [剑指Offer]二维数组查找
- 剑指offer--<二维数组查找>
- 《剑指offer》数组——二维数组中查找
- 剑指Offer 面试题3:二维数组中查找
- 剑指offer第三题:二维数组中查找
- 码农小汪-剑指Offer之4- 在二维数组中查找
- 剑指Offer 面试题3 二维数组中查找
- 剑指offer之二维数组中查找一个数
- 剑指offer——在二维数组中查找元素
- 剑指offer 面试题3:二维数组中查找
- 剑指offer-->二维数组中的查找
- 剑指offer之二维数组中的查找
- 剑指offer:3-二维数组中的查找
- [剑指Offer]二维数组中的查找
- (21)ExtJS之使用VType验证
- ADS工程移植 Keil MDK 编译出现的警告和错误解决
- 国外的一些安全相关论坛收集
- java中的值传递和引用传递
- Qt WebView中出现cross domain ajax querying with jquery
- 【剑指offer】二维数组中查找_03
- CLI程序内创建全局的托管对象
- (22)ExtJS之表单数据加载示例
- 开博寄语
- Spring基于注解的缓存配置--web应用实例
- (23)ExtJS之表单数据异步提交
- 囚禁3
- struts2的result-type详解
- UML关系(泛化,实现,依赖,关联(聚合,组合))