Interview Topic 4: Finding in a two-dimensional array

Source: Internet
Author: User

The problem is no longer difficult to optimize

This problem has been done before.

The topics are:

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

It's a little bit more complicated to do before. The main idea is that the goal number is target, first compared with the number of the last column in the first row of the two-dimensional array, if the number compared to target is greater than or equal to target, according to the characteristics of the array in the topic, it is possible to traverse this line in this row, if any, to return true If you are not on this line, continue judging by comparing the number of the last column in the next row. The code is as follows:

1  Public Static BooleanFind (intTargetint[] Array) {2              //start with the first line3              for(inti=0;i<array.length;i++){4                 //start with the last column5                 intj = Array[i].length-1;6                 //For (int j = array[i].length-1;j>=0;j--) {7                     //if the number of the last column is larger than the target number, it is compared to each number in the row. 8                     if(Array[i][j] >=target) {9                          for(intK:array[i]) {Ten                             if(k==target) { One                                 return true; A                             } -                         } -                     } the                 //} -             } -             return false; -}

In the worst case, when the target is not found, the time complexity is O (n^2).

The more optimized code is given in the book, with the time complexity O (n), as follows:

1  Public Static BooleanFind0 (intTargetint[] Array) {2          BooleanFound =false;3          intRow = 0;4          intColumn = Array[0].length-1;5          6          if(array!=NULL&& array.length >0 && array[0].length> 0){7              8              while(Row < array.length && column >= 0){9                 if(Array[row][column] = =target) {TenFound =true; One                      Break; A}Else if(array[row][column]>target) { -column--; -}Else{ therow++; -                 } -                  -             } +               -          } +           A          returnFound; at}


The main purpose is to eliminate the extra rows and columns, narrow the scope of the method.

The number in the upper-right corner of the array is selected first, based on the attributes given to the group. If the number equals 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 stripped. If the number is less than the number you want to find, the row that contains the number is stripped. This narrows it down, or finds the number you want to find, or the lookup range is empty.

Or that sentence, optimization is very important.

Interview Topic 4: 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.