剑指offer:3-二维数组中的查找

本文介绍了一种在部分排序的二维数组中查找特定数值的方法。通过从数组的一角开始比较目标值,逐步缩小搜索范围,实现了高效的查找过程。文章提供了详细的C++实现代码,并通过多个测试案例验证了算法的有效性和准确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


代码如下:

#include <iostream>
using namespace std;

/*
//版本1:从二维数组右上角开始比较
//从一个二维数组matrix中查找数字number,数组总行数为rows,总列数为columns.
bool find_number_from_array(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)
		{
			//number刚好等于二维矩阵右上角的数字.
			if (number == matrix[row * columns + column])
			{
				found = true;
				break;
			}
			//number大于二维矩阵右上角的数字,删除该数字所在的行.
			else if(number > matrix[row * columns + column])
				++row;
			//number小于二维矩阵右上角的数字,删除该数字所在的列.
			else
				--column;
		}
	}
	//返回值found为true表示找到number,为false表示未找到number.
	return found;
}*/


//版本2:从二维数组左下角开始比较
//从一个二维数组matrix中查找数字number,数组总行数为rows,总列数为columns.
bool find_number_from_array(int *matrix,int rows,int columns,int number)
{
	bool found = false;
	if (matrix != NULL && rows > 0 && columns > 0)
	{
		//初始化行列,取左下角位置数字.
		int row = rows - 1;
		int column = 0;
		while (row >= 0 && column < columns)
		{
			//number刚好等于二维矩阵左下角的数字.
			if (number == matrix[row * columns + column])
			{
				found = true;
				break;
			}
			//number大于二维矩阵右上角的数字,删除该数字所在的行.
			else if(number > matrix[row * columns + column])
				++column;
			//number小于二维矩阵右上角的数字,删除该数字所在的列.
			else
				--row;
		}
	}
	//返回值found为true表示找到number,为false表示未找到number.
	return found;
}

int main()
{
	int a[] = {
		1,2,8,9,
		2,4,9,12,
		4,7,10,13,
		6,8,11,15
	};
	int num;
	cout << "输入想要查找的数字:" << endl;
	while(cin >> num)
	{
		if(find_number_from_array(a,4,4,num))
			cout << "找到!" << endl;
		else
			cout << "未找到!" << endl;
	}

	return 0;
}

原书参考答案:

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

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

#include <iostream>
using namespace std;

// 二维数组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 main()
{
	Test1();
	Test2();
	Test3();
	Test4();
	Test5();
	Test6();
	Test7();

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值