1. Description of the problem
Write an efficient algorithm, from a mxn To find the given value in the integer matrix, the matrix has the following characteristics:
2. Methods and Ideas 2.1 Two-part search method
According to the characteristics of the matrix it is easy to think of the dichotomy, but this is a two-dimensional matrix, how to transform the problem into one-dimensional is the key. In fact, we can determine the range of rows that a value may be based on the first column of the matrix (l Imu,l Imd ) , which limu=0, made maT R Ix [0][0]≤maT R Ix [I][0]≤maT R Ix [l Imd ][0] , I∈[0,l Imd ] 。 A dichotomy is used to determine the value of the LIMD.
After you have determined the range of rows that the value might be in, line by row to find the target value, which reduces the problem to one dimension. The code is as follows:
classSolution { Public:BOOLSearchmatrix ( vector<vector<int> >& Matrix,intTarget) {if(matrix.size () = =0)return false;intI,j,mid,rows = Matrix.size (), cols = matrix[0].size ();intLIMD = rows-1, Limu =0;/ * Binary find the lowest possible line of the target value * / while(Limu < LIMD) {mid = (Limu + LIMD)/2;if(matrix[mid][0] > Target) LIMD = mid-1;Else if(matrix[mid][0] < target) Limu = mid +1;Else return true; }/* Binary search for each line */ for(i =0; I <= LIMD; i++) {intL =0, R = cols-1; while(L <= R) {mid = (L + r)/2;if(Matrix[i][mid] < target) L = mid+1;Else if(Matrix[i][mid] > Target) r = mid-1;Else return true; } }return false; }};
2.2 Divide and conquer law
Another way is to use the idea of score. Take the problem matrix as an example to find the number 5. Careful observation of the matrix, the upper right corner of the number is 15, because the matrix is the column increment, so the number 5 is not possible on the right side 15 This column, we can not consider this column, the scope is reduced by one column.
[1, 4, 7, 11]
[2, 5, 8, 12]
[3, 6, 9, 16]
[10, 13, 14, 17]
[18, 21, 23, 26]
Judging the number 11 again, the same >5 , and a column is reduced. The number 7 is also less than 5, and in a reduced column, the current matrix becomes:
[1, 4,]
[2, 5]
[3, 6]
[10, 13]
[18, 21]
Judging by the number 4 o'clock, as 5>4 , the target value is definitely not in the 4 row, go to point this line, in the judgment.
[2, 5]
[3, 6]
[10, 13]
[18, 21]
Okay, judge the number 5, and find the target value to return.
The time complexity of this algorithm O(n) , to be superior to the first algorithm, buy as follows:
classSolution { Public:BOOLSearchmatrix ( vector<vector<int> >& Matrix,intTarget) {if(matrix.size () = =0)return false;intI,j,rows = Matrix.size (), cols = matrix[0].size (); i =0; j = cols-1; while(I < rows && J >=0) {if(Matrix[i][j] = = target)return true;Else if(Matrix[i][j] > target) j--;Elsei++; }return false; }};
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Leetcode Search a 2D Matrix II (dichotomy and divide-and-conquer method to solve ordered two-dimensional array lookups)