A Young's matrix has a two-dimensional array. Each row of the array is incremented from left to right, and each column is incremented from top to bottom. Finds if a number exists in such an array. The time complexity is less than O(N);

First understand the Young's matrix. The
Young's matrix is ​​a two-dimensional array that increases sequentially from left to right and from top to bottom. For
example: 1 2 3
          2 3 4
          3 4 5
Solution idea:
If you want to find a number in the Young's matrix, and the time complexity is less than O(N), then according to the characteristics of the array, you can start from the last element of each row of the array, if they are equal Just output it directly. If it is less than the number to be searched, use the last element of the next line to compare. If it is greater than the number after the line, you can compare it in turn.
The procedure is as follows:
#include<stdio.h>
int Reseach_sou(int arr[][3], int row,int col, int key)
{
	int i = 0;
	int j = col-1;
	while ((j >= 0) && (i <= 2))
	{
		if (arr[i][j] == key)
		{
			return 1;
		}
		else if (arr[i][j] < key)
		{
			i++;

		}
		else
		{
			j--;
		}
	}
	return 0;
}
intmain()
{
	int arr[][3] = {1, 2 ,3, 2, 3, 4, 3, 4, 5};
	int key = 0;
	int row = 3;
	int col = 3;
	scanf("%d",&key);
	Reseach_sou(arr, row, col, key);
	printf("%d ",Reseach_sou(arr, row, col, key));//If found, return 1, if found, return 0
	return 0;
}

Improvement: Returns the position of the searched element in the array
Method 1: Use structure variables
#include<stdio.h>
#include<string.h>
struct Point //Define a structure
{
	int i;
	int j;
};
struct Point Reseach_sou(int arr[][3], int row, int col, int key)
{
	int i = 0;
	int j = col-1;
	struct Point ret = {-1,-1};
	while ((j >= 0) && (i <= 2))
	{
		if (arr[i][j] == key)
		{
			ret.i = i;
			ret.j = j;
			return ret;
		}
		else if (arr[i][j] < key)
		{
			i++;
		}
		else
		{
			j--;
		}
	}
	return ret;
}
intmain()
{
	int arr[][3] = {1, 2 ,3, 2, 3, 4, 3, 4, 5};
	int row = 3;
	int col = 3;
	struct Point ret = Reseach_sou(arr, row, col, 5);//Return a structure variable
	if ((ret.i != -1)&&(ret.j != -1))
	{
		printf("Found, the coordinates are: %d %d\n",ret.i, ret.j);
	}
	else
	{
		printf("Cannot find\n");
	}
	return 0;
}

Method 2: Return parameter

#include<stdio.h>
//pi,pj is the address of i,j, *dereference is the value of i,j
int Reseach_sou (int arr [] [3], int * pi, int * pj, int key)
{
	int i = 0;
	int j = *pj-1;
	while ((j >= 0) && (i <= *pi))
	{
		if (arr[i][j] == key)
		{
			* pi = i;
			*pj = j;
			return;
		}
		else if (arr[i][j] < key)
		{
			i++;

		}
		else
		{
			j--;
		}
	}
	*pi = -1;
	*pj = -1;
}
intmain()
{
	int arr[][3] = {1, 2 ,3, 2, 3, 4, 3, 4, 5};
	int i = 3;
	int j = 3;
	int key = 0;
	scanf("%d",&key);
	Reseach_sou(arr, &i, &j, key);//Get the address of x, y
	if ((i != -1)&&(j != -1))
	{
		printf("Found, the subscript is: %d %d\n",i, j);
	}
	else
	{
		printf("Cannot find\n");
	}
	return 0;
}



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325531798&siteId=291194637