Sword refers to an offer: lookup in a two-dimensional array (Java)

Source: Internet
Author: User

For example, the following two-dimensional array is each row, each column is an ascending sort. Returns True if the number 7 is found in this array, or FALSE if you look for an array of 5, because the array does not contain the number.

1 2 8 9

2 4 9 12

4 7 10 13

6 8 11 15 First we select the number in the upper-right corner of the array 9. Because 9 is greater than 7, and 9 is the first (and smallest) number in column 4th, so 7 can't be in the column where the number 9 is, so just analyze the remaining 3 columns, and in the rest of the matrix, the number in the upper-right corner is 8. Also 8 is greater than 7, so the column for 8 can also be removed. Next, just analyze the remaining two columns, and as 2 is less than 7, the 7 you are looking for may be on the right and bottom of 2.   Since the right side of the 2 has been removed, it is only possible to appear below 2, to remove 2 of the row, leaving only three rows two columns, and then the same way as before, when found that the end of the 7 o'clock. The summary rule is as follows: First, select the number in the upper-right corner of the matrix. If it is equal to the number you are looking for, the lookup process ends; if it is greater than the number you are looking for, the column that contains the number is excluded, or the row that contains the number is excluded if it is less than the number you are looking for. Each step can reduce the lookup range until you find the number you want to find, or find the failure.

Public static boolean getvaluefrommatrix (int[][] sample, int rows,                  int columns, int  num)  {            boolean found =  false;             if  (sample !=  null && rows > 0 && columns > 0)  {                  int row =  0;                 int  column = columns - 1;                  while  (row < rows && column >=  0) &NBSP;{&NBsp;                     int tempValue = sample[row][column];                      if  (num >  Tempvalue)  {                          ++row;                      } else if  (num  < tempvalue)  {                          --column;                      } else {                           found = true;                          break;                      }                  }              }                   return found;         }    

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.