转载请注明出处<http://blog.csdn.net/qianqin_2014/article/details/51451666>
题目:
在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
如下面的数组,查找数字7,找到返回true;否则,返回false。
1 | 2 | 8 | 9 |
2 | 4 | 9 | 12 |
4 | 7 | 10 | 13 |
6 | 8 | 11 | 15 |
思路一:
第一种思路就是按照顺序查找,将数组中的元素依次与要查找的元素相比较,找到就返回true;否则,返回false
源代码:
#include<iostream>
bool SearchNum2(int arr[][4], int rows, int colums, int num)
{
bool find = false;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < colums; j++)
{
if (arr[i][j] == num)
{
find = true;
return find;
}
}
}
return find;
}
int main()
{
int arr[][4] = {
{1,2,8,9},
{2,4,9,12},
{4,7,10,13},
{6,8,11,15}
};
int num;
std::cout << "请输入要查找的数字:";
std::cin >> num;
std::cout << std::boolalpha << SearchNum2(arr, 4, 4, num) << std::endl;
system("pause");
return 0;
}
请输入要查找的数字:7
true
请按任意键继续. . .
请输入要查找的数字:5
false
请按任意键继续. . .
思路二:
思路一虽然也能够判断出数组中是否有要查找的值,但没有充分的利用该数组的优点。思路二充分利用了数组的特征:
首先选取右上角的数字9,9大于7,下一次只需在9的左边区域查找即可;8大于7,只需在8的左边区域查找,2小于7,只需在2的下方区域查找,4大于7,只需在4的下方区域查找,找到7返回true。
#include<iostream>
bool SearchNum(int *arr, int rows, int colums, int num)
{
int row = 0;
int colum = colums - 1;
bool find = false;
while (row < rows && colum >= 0)
{
int temp = row*rows + colums;
if (arr[temp] == num)
{
find = true;
break;
}
else if (temp > num)
--colums;
else
++row;
}
return find;
}
int main()
{
int arr[] = { 1, 2, 8, 9, 2, 4, 9, 12, 4, 7, 10, 13, 6, 8, 11, 15 };
int num;
std::cout << "请输入要查找的数字:";
std::cin >> num;
std::cout << std::boolalpha << SearchNum(arr, 4, 4, num) << std::endl;
system("pause");
return 0;
}
思路三:
从左下角开始查找,和思路二差不多。
注意:不能再左上角或者右下角开始查找,因为没法缩小查找区域。
上述程序并没有考虑当指针为空的情况,这对程序的鲁棒性有一定的影响!
官方源代码:
// 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;
}
运行结果:
Test1 begins: Passed.
Test2 begins: Passed.
Test3 begins: Passed.
Test4 begins: Passed.
Test5 begins: Passed.
Test6 begins: Passed.
Test7 begins: Passed.
请按任意键继续. . .
转载请注明出处<http://blog.csdn.net/qianqin_2014/article/details/51451666>