Interview 1: Finding in a two-dimensional array

Source: Internet
Author: User

The topic from the "sword refers to the Enterprise interview officer explaining typical programming problem" face question 3.

Topic 3: In a two-dimensional array, each row is ordered in ascending order from left to right, and each column is sorted in ascending order from top to bottom. Complete a function, enter a two-dimensional array and an integer to determine if the array contains the integer.

For example, the following two-dimensional array is the ascending ordering of each column per row. Returns true if 7 is queried in the array, or False if the number 14 is found because the array does not contain 14.

Solution Analysis:

First we select the number 8 in the lower left corner of the two-dimensional array, because 8 is greater than 7, and 8 is the first number of the fourth row, so the number 7 is not likely to appear in the row of 8, so we take this line from the area to be considered, then we need to analyze only 3 rows left. In the remainder of the matrix, the number in the lower left corner is less than 7, and the column 4 is removed from the area to be considered. Next, just analyze the remaining 3 rows and 3 columns. In the same way, you can continue to deal with the previous treatment.

The process is as follows:

Note: The gray area of the matrix is the next range to look for, and the green is the number you want to find.

The logic of the entire algorithm:

First, select the number in the lower-left corner of the array. If the number is equal to the number you are looking for, the lookup process ends, if the number is greater than the number you are looking for, the row that contains the number is excluded, and if the number is less than the number you are looking for, the column that contains the number is excluded. That is, each time you remove a row or column to narrow the search until you find the number you want to find, or find the corresponding number failed!

Of course, in addition to the lower left-hand corner of the number, you can also select the upper right corner of the number, find a similar method.

Here I use Java as the programming language to demonstrate the process of the above algorithm.

1  PackageArray;2 3 /**4  * @authorwuping5  * @versionApril 7, 2016 PM 7:11:446  */7 8  Public classFindinpartiallysortedmatrix {9 Ten      Public Static BooleanFindnumber (int[] Matrix,intRowsintcolumns, One         intNumber ) { A     BooleanFound =false; -     if(Matrix! =NULL&& rows > 0 && columns > 0) { -         //The number of the lower left corner of the two-dimensional array is the lookup criterion the         introw = Rows-1; -         intColumn = 0; -          while(Row >= 0 && column <columns) { -         if(Matrix[row * columns + column] = =Number ) { +Found =true; -              Break; +}Else if(Matrix[row * columns + column] >Number ) { A--Row; at}Else { -++column; -         } -         } -     } -     returnfound; in     } -  to      Public Static voidTest (String testname,int[] Matrix,introws, +         intColumnsintNumberBooleanexpected) { -     if(TestName! =NULL) { theSystem.out.print (TestName + "begins:"); *         Booleanresult =findnumber (Matrix, rows, columns, number); $         if(Result = =expected) {Panax NotoginsengSystem.out.print ("Passed.")); - System.out.println (); the         } +         Else { ASystem.out.print ("Failed.")); the System.out.println (); +         } -     } $     } $      -      Public Static voidMain (string[] args) { -     //this represents a two-dimensional array in the form of a one-dimensional array plus the number of rows and columns the     int[] Matrix = {1, 3, 6, 9, 2, 5, 7, 11, 4, 10, 13, 16, 8, 12, 15, 18}; -     Wuyi     //the number to find is in a two-dimensional array, but not in the upper-left or lower-right corner theTest ("Test1", Matrix, 4, 4, 7,true); -      Wu     //the number to find is not in a two-dimensional array, but within the range of the array -Test ("Test2", Matrix, 4, 4, 14,false); About      $     //The number to find is in the upper-left corner of the two-dimensional array -Test ("Test3", Matrix, 4, 4, 1,true); -      -     //the number to find is in the lower-right corner of the two-dimensional array ATest ("Test4", Matrix, 4, 4, 18,true); +      the     //the number to find is outside the lower bound of the two-dimensional array range -Test ("Test5", Matrix, 4, 4, 0,false); $      the     //the number to be found is outside the upper bounds of the two-dimensional array theTest ("Test6", Matrix, 4, 4, 20,true); the      the     //the passed two-dimensional array is empty -Test ("Test7",NULL, 4, 4, 20,false); in      the     } the  About}
View Code

The code execution results are as follows:

Interview 1: Finding in a two-dimensional array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.