The sword refers to the search in the offer _ interview question 3 _ two-dimensional array (simple questions cannot be ignored), and the sword refers to the offer

Source: Internet
Author: User

The sword refers to the search in the offer _ interview question 3 _ two-dimensional array (simple questions cannot be ignored), and the sword refers to the offer

Question:In a two-dimensional array, each row is sorted in ascending order from left to right. Each column is sorted in ascending order from top to bottom. Complete a function, input such a two-dimensional array and an integer to determine whether the array contains the integer.

The array is as follows:

Find the following rules for an integer in the array:

1. Select a number in the array. If it is equal to the target, the search ends.

2. If the selected number is smaller than the target to be searched, the target to be searched should be on the right or bottom of the currently selected position

3. If the selected number is greater than the target to be searched, the target to be searched should be on the left or top of the selected position.


Key question: How do I select an integer in the array?

A: in a row, the rightmost number is the largest. In a column, the number at the top is the smallest. Therefore, when selecting a number, you must start from here.

Instance analysis: Search for Target 7, and select a number from the upper right corner.


The instance search process is summarized as follows:

1. If the selected number is equal to the number to be searched, the search ends.

2. If the number is greater than the number to be searched, remove the column where the number is located (because the selected number is at the top of the column and has been minimized)

3. If the number is smaller than the number to be searched, remove the row where the number is located (because the selected number is on the rightmost side of the row, which is already the largest)


After learning about the above rules, start to write code. I made several mistakes in the process of writing, or found several blind spots of knowledge. I will list them one by one, remember them, and encourage them.

Error Code 1:

The following code makes a very serious error,First, compilation fails.

The reason is that an error occurs when the function passes a two-dimensional array. I use a [] []. When a function transmits a two-dimensional array, it passes a pointer and cannot omit high dimensions. Otherwise, the compiler cannot properly address the pointer. It should be changed to a [] [4]. (The two-dimensional array I set is 4 rows and 4 columns)

Bool Search (int a [] [], int number, int rows, int columns) {int I, j; int r = rows; int c = columns; int n = number; for (I = 0; I <r;) {for (j = c-1; j> = 0;) {if (a [I] [j] = n) {cout <"found, Location:" <I <',' <j <endl; return true ;} else if (a [I] [j] <n) {I ++; break;} else {j -- ;}} cout <"not found" <endl; return false ;}

After correcting the preceding error, it seems that the code can be passed, or you can find several numbers, such as 7, 9, and 15. However, there are a lot of deficiencies, without considering the input of a null pointer:

Insufficient Code 2:

The following code corrected the above errors and deficiencies. It seems that the code has been completed, but in factA big problem is hidden. When I use a number that is smaller than the smallest number in the array, an endless loop occurs.The reason is that there are two for loops.

The number is smaller than the smallest number in the array. Therefore, the variable j in the column is reduced until it is reduced to less than 0 and changed to-1. The inner loop exists, but I remains unchanged, as a result, the outer loop will not end and re-enter the internal loop, which leads to an endless loop.

(The key is the following code. In other cases, for example, if NULL is passed, it is correct to find the number that exists in the two-dimensional array and find the number that is greater than the maximum number in the array, so that the problem can be found. This should be noted in that testing must be complete. It is best to think about test cases before writing code)

Bool Search (int a [] [4], int number, int rows, int columns) {int I, j; int r = rows; // row int c = columns; // The int n = number in the column; // The array to be searched // you must add this segment to prevent null pointers from being input, consider the problem comprehensively if (a = NULL | r <= 0 | c <= 0) {cout <"two-dimensional array problem" <endl; return false;} for (I = 0; I <r;) {for (j = c-1; j> = 0 ;) {if (a [I] [j] = n) {cout <"found at:" <I <', '<j <endl; return true;} else if (a [I] [j] <n) {I ++; break; // used to jump out of the inner loop} else {j -- ;}} cout <"not found" <endl; return false ;}

Improved code 3:

The following code corrected the above shortcomings and successfully met the test requirements.

Bool Search (int a [] [4], int number, int rows, int columns) {int I, j; int r = rows; int c = columns; int n = number;/** you must add this segment to prevent null pointers from being input, consider the problem comprehensively */if (a = NULL | r <= 0 | c <= 0) {cout <"two-dimensional array problem" <endl; return false;} I = 0; j = c-1; while (I <r & j> = 0) {if (a [I] [j] = n) {cout <"found, Location:" <I <',' <j <endl; return true ;} else if (a [I] [j] <n) {I ++; // break;} else {j --;}} cout <"not found" <endl; return false ;}

Code 3 is not a requirement of the question:

The main reason is that when I tested the number 8, the above Code only found one 8, and another 8 was not found. I improved it.

Bool Search (int a [] [4], int number, int rows, int columns) {int I, j; int r = rows; int c = columns; int n = number; int k = 0;/** you must add this segment to prevent null pointers from being input, consider the problem comprehensively */if (a = NULL | r <= 0 | c <= 0) {cout <"two-dimensional array problem" <endl; return false;} I = 0; j = c-1; while (I <r & j> = 0) {if (a [I] [j] = n) {cout <"found. The location is:" <I <',' <j <endl; k ++; j --; // return true ;} else if (a [I] [j] <n) {I ++ ;// Break;} else {j --;} if (k! = 0) return true; else {cout <"not found" <endl; return false ;}}

The main function is as follows:

int main(){    int a[4][4] = {        {1,2,8,9},        {2,4,9,12},        {4,7,10,13},        {6,8,11,15}    };    Search(a,1,4,4);    Search(a,15,4,4);    Search(a,7,4,4);    Search(a,8,4,4);    Search(a,0,4,4);    Search(a,16,4,4);    Search(NULL,1,4,4);    return 0;}

Result chart:


/* Accumulate little bit by bit, my one small step O ~ */


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.