Solution to the two-dimensional "ordered" array query problem, array Query
| Question: In a two-dimensional array, each row is sorted in ascending order from left to right, and a column is sorted in ascending order from top to bottom. Please complete a function, enter a two-dimensional array and an integer to determine whether the array contains the integer. |
For example, the following two-dimensional arrays are sorted in ascending order for each row and no column. If the number 7 is searched in this array, true is returned (obtained). If the number 5 is searched, false is returned because the array does not contain this number.
| 1 |
2 |
8 |
9 |
| 2 |
4 |
9 |
12 |
| 4 |
7 |
10 |
13 |
| 6 |
8 |
11 |
15 |
As shown in
Three cases.
Analysis:It can be seen that when it is not equal to the number to be searched, the two areas to be searched overlap. What should we do? We can
Select a number from an array and compare it with the number to be searched., The situation will become simpler. For example, select 9 in the upper-right corner of the array.
Because 9 is greater than 7And is the first (minimum) number in the 4th column. Therefore, 7 cannot appear in the column where the number 9 is located. So we
Remove this column from the region to be consideredThen, you only need to analyze the remaining three columns (as shown in (). In the remaining matrix, the number in the upper right corner is 8, the same
8> 7, So
We can also remove the column where 8 is located.. Next, we only need to analyze the remaining two columns (as shown in (B ). In the array consisting of the remaining two columns, number 2 is in the upper right corner of the number.
2 less than 7, The 7 to be searched may be on the right side of 2, or under 2. In the previous step, we have found that the columns on the Right of 2 have been removed. Each page means 7 cannot appear on the Right of 2, so 7 may only appear below 2. So we
Remove the row where number 2 is located., The remaining three rows and two columns of the Value Analysis (as shown in (c ). In the remaining number, 4 is in the upper right corner.
4 rows are also excludedAnd the remaining two rows and two columns of numbers (d) are in the remaining two rows and two columns with four numbers,
In the upper-right corner, it is exactly the number 7 we are looking., So
Search ProcessYou can
End.
| 1 |
2 |
8 |
9 |
| 2 |
4 |
9 |
12 |
| 4 |
7 |
10 |
13 |
| 6 |
8 |
11 |
15 |
| () |
| 1 |
2 |
8 |
9 |
| 2 |
4 |
9 |
12 |
| 4 |
7 |
10 |
13 |
| 6 |
8 |
11 |
15 |
| (B) |
| 1 |
2 |
8 |
9 |
| 2 |
4 |
9 |
12 |
| 4 |
7 |
10 |
13 |
| 6 |
8 |
11 |
15 |
| (C) |
| 1 |
2 |
8 |
9 |
| 2 |
4 |
9 |
12 |
| 4 |
7 |
10 |
13 |
| 6 |
8 |
11 |
15 |
| (D) |
Note: The area of the color in the matrix is the range of the next search.
SummaryTo sum up the above search process, we find the following rule: first, select the number in the upper right corner of the array. If this number
EqualNumber to be searched,
SearchProcess
End; If the number is
GreaterNumber to be searched,
RemoveThe Digit
Column; If the number is
LessNumber to be searched,
RemoveThe value of this number
Line.
Coding implementationCore algorithms:
/***** @ Param num the two-dimensional array to be searched * @ param rows the number of rows * @ param columns the number of columns * @ param number the number to be searched * @ return whether to find the number to be searched (number) */public static Boolean Find (int num [] [], int rows, int columns, int number) {Boolean found = false; int row = 0; int column = columns-1; if (rows> 0 & columns> 0) {while (row <rows & column> = 0) {if (num [row] [column] = number) // {found = true; break;} else if (num [row] [column]> number) {-- column; // delete column} else {+ row; // Delete row }}return found ;}
Test:
Public static void main (String [] args) {// initialize the value of the number int num [] [] = {,}, {2, 4, 9, 12, }, {6, 8, 11, 15}; System. out. println (Find (num, 7); // System in the array. out. println (Find (num, 5); // 5 is not in the array}
Result:
truefalse