Leetcode Search a 2D Matrix

Source: Internet
Author: User

The original title link is here: https://leetcode.com/problems/search-a-2d-matrix/

Reference this article: http://www.cnblogs.com/springfor/p/3857959.html

Can be treated as a one-dimensional matrix, but the crux of the problem is how to convert the matrix m*n to 1 D, and then use the two-point lookup method to solve problems.

The focus of the transformation is on the location of each point, and in the matrix representation we are accustomed to using (I,J) to represent a point, which is used here so that it hinders our use (mid/n, mid%n) to represent.

For example, the example in question I can translate into:

Position:0 1 2 3 4 5 6 7 8 9 10 11

Values:1 3 5 7 10 11 16 20 23 30 34 50

row:0 0 0 0 1 1 1 1 2 2 2 2

Column:0 1 2 3 0 1 2 3 0 1 2 3

Where: Number of rows m=3, number of columns n=4

As above, this is the corresponding table that transforms the matrix into a 1-row array. So the initial value for the binary lookup method is: Low=0,high=m*n-1 (the number of total values, since starting from 0 so minus 1). In order to be able to find the given 2D matrix, we still need to determine the number of rows and columns, as can be seen in the above table, the number of rows is position/n, and the number of columns is position%n, so it is easy to locate the desired value in the original matrix. The rest of the solution is the same as the binary search method.

Time complexity is O (log (m*n)) because the total number of binary lookups is m*n.

The second method is to do two-time binary search, first find the row, the second time to find the column.

Note: In method two, when you jump out of the loop to find the line, R definitely points to the line with the start element that is smaller than target, and L definitely points to the line where the starting element is larger than target, so row should select R.

Also note that R is possible for-1. This is corner case if all elements are larger than target, the last R will be reduced to-1. Be sure to note that it is within the range of 0 to length-1 before using index.

The initial value of R should be noted-1.

AC Java:

1  Public classSolution {2      Public BooleanSearchmatrix (int[] Matrix,inttarget) {3         /*4 //method 15 if (Matrix = = NULL | | matrix.length = 0 | | matrix[0].length = = 0) {6 return false;7         }8 int m = matrix.length;9 int n = matrix[0].length;Ten int l = 0; One int r = m*n-1; A While (l<=r) { - int mid = L + (r-l)/2; - if (matrix[mid/n][mid%n] = = target) { the return true; - }else if (matrix[mid/n][mid%n] > target) { - r = mid-1; - }else{ + L = mid+1; -             } +         } A return false; at         */ -         //Method 2 -         if(Matrix = =NULL|| Matrix.length = = 0 | | Matrix[0].length = = 0){ -             return false; -         } -         intm =matrix.length; in         intn = matrix[0].length; -         intL = 0;  to         intR = m-1;//Error +          while(l<=S) { -             intMID = L + (r-l)/2; the             if(Matrix[mid][0] = =target) { *                 return true; $}Else if(Matrix[mid][0] >target) {Panax Notoginsengr = Mid-1; -}Else{ theL = mid+1; +             } A         } the         introw = R;//when jumping out of loop, R definitely points to the start element that is smaller than target row, L definitely point to start I than target big row +         //Corner case, if the first value of the first row is greater than target, then the row is-1 when you jump out of the loop. Definitely not the target value. -         if(Row < 0){ $             return false; $         } -L = 0; -r = n-1; the          while(l<=R) { -             intMID = L + (r-l)/2;Wuyi             if(Matrix[row][mid] = =target) { the                 return true; -}Else if(Matrix[row][mid] >target) { Wur = Mid-1; -}Else{ AboutL = mid+1; $             } -         } -         return false; -     } A}

Leetcode Search a 2D Matrix

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.