Young's matrix: each row increments from left to right, and each column increments from top to bottom.
Title: Enter a young matrix and an integer to determine if the number appears in the Young's matrix.
Analysis: There are three different methods of time complexity.
The first: traversing the entire two-dimensional array, this method is the least efficient.
The second: Because the young matrix each row is incremented, is ordered, we can first determine whether the integer is greater than or equal to the first element of each row, less than or equal to the last element. If so, find the line using the binary lookup method. This method is the second most efficient.
Third: Because each row is incremented, and each column is incremented, we can compare this number to the top-right element. If the number is greater than the upper-right element, it indicates that the number is greater than the line element, so the count can only appear below the line, so the find orientation shrinks below the line. Similarly, if this number is less than the upper-right element, it indicates that the number is less than this column element, so the look-up can be narrowed to the left of this column. (It can also be compared with elements in the lower left corner), which is the most efficient way.
#define _crt_secure_no_warnings 1#include<stdio.h> #include <stdlib.h> #define Row 4#define col 3//first int Check_num (int (*p) [col], int num, int line, int rank)//traversal array {int i = 0;int j = 0;for (i = 0; i < line; i++) {for (j = 0; J < rank; J + +) {if (p[i][j] = = num) return 1; exists then returns 1}}return 0;} The second int check_num (int (*p) [col], int num, int line, int rank) {for (int i = 0; i < line; i++) {if (NUM>=P[I][0]&&A Mp;num <= p[i][rank-1])//Determine if num is possible in this line, if at the words enter if{int *left = &p[i][0];int *right = &p[i][rank-1];int * TMP = Null;while (left <= right)//binary lookup method Lookup {tmp = left + (right-left)/2;if (num>=*tmp) left = TMP + 1;if (num <= *tmp) right = tmp-1;if (num = = *tmp) return 1; exists then returns 1}}}return 0;} third int check_num (int (*p) [col], int num, int line, int rank) {int i = 0;int j = rank-1;while (I < line | | J >= 0) {if (Num < p[i][j])//If NUM is less than the last number in line I, the column is J--;if (Num>p[i][j])//if NUM is greater than the last digit of line I, the line self-increment i++;if (num = = P[i][j]) return 1; exists then returns 1}return 0;} int main () {int n = 0;int Arr[row][col] = {0};for (int i = 0; i < row; i++) {for (int j = 0; J <col; J + +) {scanf ("%d") , &arr[i][j]);}} scanf ("%d", &n); int ret = Check_num (arr, n, Row, col), if (ret==1) printf ("yes\n"); elseprintf ("no\n"); System ("Pause" ); return 0;}
A summary of the method of the third problem of sword finger offer-