Sword Point offer interview question: 2. Find in two-d arrays

Source: Internet
Author: User

Title: Finding in a two-dimensional array

title: 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 each row, each column increments the sort. Returns True if the number 7 is found in this array, or False if the number 5 is found because the array does not contain the number.

Second, the idea of solving problems

First, select the number in the upper-right 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 column that contains the number is excluded, and if the number is less than the number you are looking for, the row that contains the number is excluded. That is, if the number you are looking for is not in the upper-right corner of the array, each time you exclude a row or column from the array's look-up, each step narrows the search until you find the number you are looking for, or the lookup range is empty.

For example, the steps to find the number 7 in the two-dimensional array above are as follows:

(the area with the shaded background in the matrix is the scope of the next lookup)

Third, solve the problem 3.1 code implementation
    //in a two-dimensional array matrix, each row is incremented from left to right,//Each column is sorted from top to bottom     Public Static BOOLFind (int[,] Matrix,intRowsintColumnsintNumber ) {        BOOLIsfind =false; if(Matrix! =NULL&& rows >0&& columns >0)        {            //start with the first line            introw =0; //start with the last column            intcolumn = Columns-1; //rows: Top to bottom, columns: right to left             while(Row < rows && column >=0)            {                if(matrix[row, column] = =Number ) {Isfind=true;  Break; }                Else if(Matrix[row, column] >Number ) {Column--; }                Else{row++; }            }        }        returnIsfind; }

In the previous analysis, each of us selects the upper-right number in the range of array lookups. Similarly, we can also select numbers in the lower left corner. But we can't choose the upper-left or lower-right corner. In the upper-left corner, for example, the initial number 1 is in the upper-left corner of the initial array, and since 1 is less than 7, 7 should be on the right or bottom of 1. At this point we can neither exclude 1 rows from the lookup range, nor reject the 1 columns, so that we cannot narrow the scope of the lookup.

3.2 Unit Test

(1) The number to find is in the array

[TestMethod] Public voidFindTest1 () {//1 2 8 9//2 4 9//4 7//6 8//The number to find in the array        int[,] matrix = {{1,2,8,9}, {2,4,9, A}, {4,7,Ten, -}, {6,8, One, the } }; //the number of rows and columns can be obtained through the GetLength () method//assert.areequal (Program.find (Matrix, Matrix. GetLength (0), Matrix. GetLength (1), 7), true);Assert.AreEqual (Program.find (Matrix,4,4,7),true); }

(2) The number to find is not in the array

[TestMethod] Public voidFindTest2 () {//1 2 8 9//2 4 9//4 7//6 8//the number to find is not in the array        int[,] matrix = {{1,2,8,9}, {2,4,9, A}, {4,7,Ten, -}, {6,8, One, the}}; Assert.AreEqual (Program.find (Matrix,4,4,5),false); }

(3) The number to find is the smallest number in the array

[TestMethod] Public voidFindTest3 () {//1 2 8 9//2 4 9//4 7//6 8//the number to find is the smallest number in the array        int[,] matrix = {{1,2,8,9}, {2,4,9, A}, {4,7,Ten, -}, {6,8, One, the } }; Assert.AreEqual (Program.find (Matrix,4,4,1),true); }

(4) The number to find is the largest number in the array

[TestMethod] Public voidFindTest4 () {//1 2 8 9//2 4 9//4 7//6 8//the number to find is the largest number in the array        int[,] matrix = {{1,2,8,9}, {2,4,9, A}, {4,7,Ten, -}, {6,8, One, the } }; Assert.AreEqual (Program.find (Matrix,4,4, the),true); }

(5) The number to find is smaller than the smallest number in the array

[TestMethod] Public voidFindTest5 () {//1 2 8 9//2 4 9//4 7//6 8//the number to look for is smaller than the smallest number in the array        int[,] matrix = {{1,2,8,9}, {2,4,9, A}, {4,7,Ten, -}, {6,8, One, the } }; Assert.AreEqual (Program.find (Matrix,4,4,0),false); }

(6) The number to find is larger than the largest number in the array

[TestMethod] Public voidFindTest6 () {//1 2 8 9//2 4 9//4 7//6 8//the number to look for is larger than the largest number in the array        int[,] matrix = {{1,2,8,9}, {2,4,9, A}, {4,7,Ten, -}, {6,8, One, the } }; Assert.AreEqual (Program.find (Matrix,4,4, -),false); }

(7) Robustness test, input null pointer

    [TestMethod]    publicvoid  FindTest7 ()    {        // robustness test, input null pointer        Assert.AreEqual (Program.find (null00false);    

Unit Test Results:

Zhou Xurong

Source: http://edisonchou.cnblogs.com

The copyright of this article is owned by the author and the blog Park, welcome reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to give the original link.

Sword Point offer interview question: 2. Find in two-d arrays

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.