《剑指offer》:行列有序的二维数组查找()

题目

在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。

请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。 

输入描述:

array: 待查找的二维数组

target:查找的数字


输出描述:

查找到返回true,查找不到返回false

------------------------------------------------------------------------------------------------------------------------

思路:线性查找中的二分查找的思路,每次把范围限定在一个小方块内。

思路前提:假设二维数组是静态的,确定行数和列数。

关键词:递归、容器、析构函数

1. 确定array数组行数M和列数N。将每次查找限定在一个方块内。方块的左上角的上界为(low1, low2),右下角的下界为(high1, high2);

初始时:设(low1, low2)=(0,0), (high1, high2)=(M,N);

另(m,n)=((high1+low1)/2, (high2+low2)/2)为中间值坐标。 

2. 如果没有元素,返回false;

3. 非空情况下

如果target比左上角最小的元素小,或者比右下角最大的元素大,则返回false;

除此之外,执行循环:

如果array(m,n)比target小,则取(low1, low2)=(m,n);
如果array(m,n)比target小,则取(high1, high2)=(m,n);

4. 最终判别条件,分三种情况:


(a)同一列相邻。查找下限和查找上限为上下相邻,则分别与target对比即可。

(b)同一行相邻。查找下限和查找上限为左右相邻,则分别与target对比即可。

(c)对角相邻。查找下限和查找上限为对角线关系,此时情况比较复杂,有一个陷阱:

这里不能简单地将target和四个元素比较,而是与右上角、左下角的不确定区域进行对比。在每个区域再递归地进行查找。


------------------------------------------------------------------------------------------------------------------------

代码及测试用例(报错待调试)

#include<iostream>
#include<stdio.h>
#include<vector>
using namespace std;
class Solution {
public:
    bool Find(vector<vector<int> > array,int target) {
    	
		int M=array.size();
		int N=array[0].size();
		if (0==M || 0==N) return false;
		if (target < array[0][0] || target > array[M-1][N-1]) return false;
//		cout<<"M,N:"<<M<<","<<N<<endl;
		int low1=0, low2=0, high1=M-1, high2=N-1;
		int m=(low1+high1)/2, n=(low2+high2)/2;
		int tem;
		while (1){
//			cout<<"circle:"<<tem++;
			if (low1==high1 && low2==high2){
				if (array[low1][low2]==target) return true;
				else{
					return false;
				} 
			}
			else if ((low1==high1 && low2==high2-1) ||(low1==high1-1 && low2==high2)){
				if (array[low1][low2]==target||array[high1][high2]==target)	return true;
				else{
					return false;
				} 
			}
			else if (high1-1==low1 && high2-1==low2){
				cout<<"low1,low2:"<<low1<<","<<low2<<endl<<array[low1][low2]<<endl;
				if (array[low1][low2]==target || array[high1][high2]==target) return true;
				vector<vector<int> > array1(high1, vector<int>(N-high2));
				vector<vector<int> > array2((M-high1), vector<int>(high2));
				for (int i=0; i<high1; i++){
					for (int j=high2 ;j<N; j++){
						array1[i][j-high2]=array[i][j];
					}
				}
				for (int i=high1; i<M; i++){
					for (int j=0 ;j<high2; j++){
						array2[i-high1][j]=array[i][j];
						cout<<array[i-high1][j]<<",";
					}
				}
				bool a1 = Find(array1,target);
				bool a2 = Find(array2,target);
				cout<<endl<<"a1,a2:"<<a1<<","<<a2<<endl;
				if (1==a1||a2)	return true;
				else return false;
			} 
			else{ 
				if (array[m][n]==target) return true; 
				if (array[m][n] > target){
					high1 = m; high2 = n;
				}
				if (array[m][n] < target){
					low1 = m; low2 = n;
				}
				m=(low1+high1)/2; n=(low2+high2)/2;	
			} 
		}
	}
};
	
int main(){
	int m=40,n=40;
	int arr[m][n] = {{0,1,3,4,5,7,8,11,13,15,18,21,24,27,30,32,35,36,39,41,42,43,46,49,52,55,58,60,63,66,67,69,72,75,78,80,81,82,85,86},{1,4,6,8,11,12,15,17,18,20,23,24,27,30,33,34,38,39,42,44,47,48,51,52,55,57,59,62,64,67,70,72,75,77,81,83,84,87,90,91},{4,7,8,11,14,16,18,20,21,24,27,29,32,35,36,39,40,42,44,46,49,52,54,56,58,60,61,64,67,70,73,76,78,81,84,87,89,91,93,96},{5,8,10,13,15,19,21,23,24,27,29,31,34,37,38,41,43,45,46,49,52,55,58,59,61,64,67,69,71,72,76,78,79,83,87,90,91,94,96,97},{6,11,14,16,18,22,24,27,29,32,33,35,36,40,42,44,47,50,51,52,54,58,60,62,64,67,70,73,76,79,82,84,87,88,91,94,97,99,101,102},{9,13,16,19,21,23,25,29,31,35,38,39,42,45,48,51,54,56,57,60,63,64,67,69,72,73,74,76,79,81,85,88,90,92,95,98,100,101,104,106},{10,16,19,22,24,26,29,31,34,36,40,41,45,46,50,54,56,59,60,63,66,69,70,72,75,77,79,81,83,85,88,91,93,96,98,99,102,105,107,109},{12,18,22,25,26,29,32,33,37,39,42,44,47,50,52,57,59,61,62,66,68,71,72,74,76,80,82,84,87,90,92,94,95,98,101,102,105,107,109,112},{14,20,25,27,30,32,35,37,40,43,44,47,49,52,54,58,60,63,65,68,70,73,75,76,78,83,84,86,89,92,93,97,99,101,103,104,107,110,113,116},{17,23,28,29,32,33,38,40,42,46,49,50,52,55,56,60,63,65,68,71,74,75,78,81,84,87,89,92,95,96,99,100,102,104,106,108,110,113,115,118},{18,25,30,31,35,38,41,43,45,48,52,53,55,58,60,62,66,67,70,73,76,77,79,82,86,89,92,95,98,101,102,103,106,109,111,112,114,117,119,122},{21,28,32,34,38,40,42,45,47,50,53,56,59,60,61,63,69,71,74,75,78,80,83,84,87,90,93,98,100,104,107,110,111,113,116,117,118,120,122,123},{23,29,34,36,41,43,46,49,52,54,55,58,60,63,65,67,72,74,75,77,80,82,85,88,91,94,97,100,103,106,110,113,114,116,119,122,124,125,126,129},{24,31,35,37,43,46,48,50,55,58,60,62,63,65,68,69,73,75,78,80,81,85,86,89,93,96,99,103,106,108,113,116,118,120,122,125,128,130,132,133},{25,33,38,41,45,47,51,53,58,60,63,65,66,68,70,72,74,77,80,82,83,86,88,91,94,97,100,106,107,109,115,119,121,122,124,126,129,131,135,136},{27,34,39,43,46,48,54,55,60,63,66,69,70,73,75,76,77,79,83,84,87,90,93,95,98,99,103,109,110,113,116,121,124,126,128,131,133,136,137,139},{30,35,42,46,48,50,57,60,63,65,68,71,74,77,78,81,82,84,86,88,91,94,97,99,101,102,105,112,113,114,119,124,127,129,132,134,137,140,142,145},{33,36,45,48,51,54,60,63,66,67,71,74,77,80,82,84,85,88,91,92,95,97,98,101,103,106,108,113,115,116,121,125,129,132,134,135,140,141,143,148},{36,39,47,50,54,55,63,66,69,70,72,76,79,82,84,86,88,90,92,95,98,101,102,104,106,109,110,116,119,122,123,128,131,134,135,136,142,144,147,150},{37,40,50,53,56,57,64,67,71,73,75,79,81,85,86,88,90,93,95,96,100,103,105,108,110,112,113,117,121,125,127,131,133,135,137,139,143,147,150,153},{39,43,53,55,58,60,67,70,72,76,78,82,84,88,90,91,92,95,96,98,103,105,108,110,112,115,116,118,124,128,130,134,137,139,141,143,144,150,153,156},{41,45,54,58,59,63,70,72,75,79,82,84,87,91,93,94,96,98,99,100,106,107,111,113,116,118,121,122,127,131,133,136,140,141,143,146,148,153,156,157},{42,46,56,60,63,65,72,73,76,81,85,87,90,93,95,97,99,102,103,106,109,111,114,116,117,121,123,125,130,133,136,137,142,145,147,149,151,155,159,161},{45,47,58,62,66,69,73,74,77,82,86,89,91,94,98,100,103,105,108,110,113,114,117,119,122,124,126,129,131,136,139,140,143,148,149,151,153,157,160,162},{48,49,59,63,67,70,75,77,80,85,89,92,94,97,100,102,104,106,111,113,115,117,120,123,126,128,131,134,136,138,141,142,145,150,152,154,157,160,163,166},{49,52,61,66,68,71,78,79,82,86,90,93,97,98,101,104,107,109,112,115,118,120,123,125,128,131,133,136,139,140,144,147,149,153,155,158,159,163,166,168},{50,55,63,67,70,73,80,83,85,88,93,96,99,102,104,106,110,113,115,118,121,124,126,128,131,133,136,138,142,144,147,149,152,155,158,160,161,165,169,170},{51,57,65,69,72,75,82,84,87,89,96,97,101,105,107,108,111,114,117,120,123,127,128,131,132,136,139,141,144,147,148,152,153,157,160,162,164,167,171,173},{54,59,66,70,75,78,84,86,90,91,97,100,103,107,110,113,115,118,121,122,125,128,130,133,135,137,141,143,145,149,151,155,156,158,162,164,167,170,174,176},{55,62,69,71,78,81,86,89,92,94,100,101,105,108,113,116,118,121,124,126,128,129,132,135,137,139,143,146,149,152,155,157,159,160,164,167,170,172,175,178},{56,65,71,72,80,84,88,91,95,98,101,103,107,111,115,118,121,124,126,129,131,133,134,137,140,143,145,149,151,153,158,161,163,165,167,170,173,176,178,181},{58,68,72,75,81,86,91,94,98,101,103,106,109,114,117,119,122,125,129,131,134,136,138,140,143,146,148,150,153,156,160,163,166,169,170,172,175,178,179,182},{59,70,75,77,82,87,92,97,100,103,106,108,111,117,120,123,126,128,132,134,136,139,141,143,145,149,151,153,155,157,162,165,169,171,172,175,177,180,183,185},{61,73,78,80,85,90,94,99,101,105,107,110,113,120,123,126,129,131,134,136,139,140,142,146,147,152,154,156,158,160,164,167,171,174,176,177,180,183,184,187},{64,75,79,82,87,92,97,101,104,108,111,113,116,123,126,128,132,135,138,139,140,143,146,149,150,155,157,159,161,162,167,168,174,176,179,181,182,186,189,191},{66,77,80,85,88,95,100,103,107,111,112,116,117,126,129,131,135,137,139,141,142,144,149,151,153,156,159,162,165,166,170,172,175,179,181,184,185,188,191,193},{67,79,81,87,91,96,103,106,110,112,115,118,120,127,132,134,136,139,141,144,147,150,152,155,157,159,161,164,167,169,171,173,177,181,183,185,188,190,193,196},{69,81,83,89,92,97,104,109,111,113,118,120,123,129,133,137,139,140,143,145,148,153,156,158,160,161,164,167,170,173,175,176,178,184,187,188,190,193,196,198},{71,82,84,91,95,100,107,111,114,116,121,124,127,132,135,140,141,142,144,147,150,155,157,159,163,164,165,169,172,176,177,179,180,186,188,190,192,195,199,201},{73,83,85,92,96,103,108,114,117,120,123,125,130,135,137,143,146,149,151,152,155,157,160,161,164,167,168,170,173,178,181,184,185,189,191,193,195,198,201,204}}; 
	vector<vector<int> > Arr(m, vector<int>(n));
	for(int i=0; i<m; i++){
		for(int j=0; j<n; j++){
			Arr[i][j]=arr[i][j];
		}
	}
	Solution S;
	bool result = S.Find(Arr,22);
	cout<<result<<"\n";
	return 0;
}
发布了392 篇原创文章 · 获赞 492 · 访问量 241万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览