leetcode-剑指04-OK

// language c
// 剑指04
// https://leetcode-cn.com/problems/er-wei-shu-zu-zhong-de-cha-zhao-lcof/

bool findNumberIn2DArray(int** matrix, int matrixSize, int* matrixColSize, int target){
	// 单行查找的函数的函数
	if((matrixSize==0) ||(matrixColSize[0]==0))
		return false;
	bool find(int arr[],int size, int x){
		for(int i =0; i<size; i++){
			if(arr[i] == x)
				return true;
		}
		return false;
	}


	//内置一个函数来求
	bool findinside(int up,int down, int left, int right, int x){
		if(down == up){
			int new1[right-left+1];
			for(int i = 0; i<right-left+1; i++){
				new1[i] = matrix[up][left +i];
			}
			return find(new1, right-left+1, x);
		}


		if(left == right){
			int new2[down-up+1];
			for(int i = 0; i<down-up+1; i++){
				new2[i] = matrix[up+i][left];
			}
			return find(new2, down-up+1, x);
		}


		if(x<matrix[up][left])
			return false;
		if(x>matrix[down][right])
			return false;
		bool ans = 	findinside(up,(up+down)/2,left, (left+right)/2,target)||\
					findinside((up+down)/2+1,down,left, (left+right)/2,target)||\
					findinside(up,(up+down)/2,(left+right)/2+1,right ,target)||\
					findinside((up+down)/2+1,down,(left+right)/2+1,right,target );
		return ans;
	}

	return findinside(0,matrixSize-1,0,matrixColSize[0]-1,target);
}
posted @ 2021-01-30 09:00  RougeBW  阅读(38)  评论(0编辑  收藏  举报