二维数组中的查找

题意描述:在一个二维数组中,每一行都按从左到右的递增顺序排序,每一列也按照从上到下的递增顺序排序
完成函数,输入一个这样的一个二维数组和一个整数,判断是否含有该整数。例如:
1   2   8    9
2   4   9    12
4   7   10  13
6   8   11  15

如果查找7返回true,如果查找5返回false


解题思路:依次从右上遍历,如果当前值等于给定值,返回true
          如果当前值大于给定值,则排除当前值所在整列,然后左移一列再遍历,当左边没有列时返回false
          如果当前值小于给定值,则排除当前值所在整行,然后下移一行再遍历,当下面没有行时返回false
          之所以这样做是因为可以缩小查找范围(从左下、右上开始均可)

bool find(int* matrix, int rows, int columns, int num) {
	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] == num) {
				found = true;
				break;
			}
			else if (matrix[row*columns + column] > num)
				column--;
			else row++;
		}
	}

	return found;
}
int main()
{
	int array[] = { 1,2,8,9,2,4,9,12,4,7,10,13,6,8,11,15 };

	bool found = find(array, 4, 4, 7);
	if (found) cout << "true" << endl;
	else cout << "false" << endl;

	found = find(array, 4, 4, 5);
	if (found) cout << "true" << endl;
	else cout << "false" << endl;

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值