Write a efficient algorithm that searches for a value in a m x n matrix, return the occurrence of it.
This matrix has the following properties:
* Integers in each row is sorted from left to right.
* Integers in each column is sorted from up to bottom.
* No duplicate integers in each row or column.
Example
Consider the following matrix:
[
[1, 3, 5, 7],
[2, 4, 7, 8],
[3, 5, 9, 10]
]
Given target = 3, return 2.
Challenge
O (m+n) Time and O (1) Extra space
Solution:
1 Public classSolution {2 /**3 * @parammatrix:a List of lists of integers4 * @param: A number you want to search in the matrix5 * @return: An integer indicate the occurrence of target in the given matrix6 */7 Public intSearchmatrix (arraylist<arraylist<integer>> Matrix,inttarget) {8 intm =matrix.size ();9 if(m==0)return0;Ten intn = matrix.get (0). Size (); One if(n==0)return0; A - returnSearchmatrixrecur (matrix,target,0,0,m-1,n-1); - } the - Public intSearchmatrixrecur (arraylist<arraylist<integer>> Matrix,intTargetintX1,intY1,intX2,inty2) { - if(x2<x1 | | y2<y1)return0; - + if(X1==x2 && y1==y2) - if(Matrix.get (x1). Get (y1) ==target)return1; + Else return0; A at intMidx = (x1+x2)/2; - intMidy = (y1+y2)/2; - intMidval =Matrix.get (MIDX). get (Midy); - intres = 0; - - if(midval==target) { in //We have a to search all of the four sub matrix. -res++; toRes + = Searchmatrixrecur (matrix,target,x1,y1,midx-1,midy-1); +Res + = Searchmatrixrecur (matrix,target,midx+1,midy+1, x2,y2); -Res + = Searchmatrixrecur (Matrix,target, (x1+x2)/2+1,y1,x2, (y1+y2)/2-1); theRes + = Searchmatrixrecur (matrix,target,x1, (Y1+y2)/2+1, (x1+x2)/2-1, y2); *}Else if(midval>target) { $ intLEFTX = (x1+x2)/2;Panax Notoginseng intLefty =Y1; - intUpX =X1; the intUpy = (y1+y2)/2; + if(Target==matrix.get (LEFTX). Get (lefty)) res++; A if(Target==matrix.get (UpX). Get (Upy)) res++; the if(Target <= matrix.get (LEFTX). Get (lefty) && target <=Matrix.get (UpX). Get (Upy)) { +Res + = Searchmatrixrecur (matrix,target,x1,y1,midx-1,midy-1); -}Else if(Target <=Matrix.get (LEFTX). Get (lefty)) { $Res + = Searchmatrixrecur (Matrix,target,x1,y1, (x1+x2)/2-1, y2); $}Else if(Target <=Matrix.get (UpX). Get (Upy)) { -Res + = Searchmatrixrecur (matrix,target,x1,y1,x2, (y1+y2)/2-1); -}Else { theRes + = Searchmatrixrecur (matrix,target,x1,y1,x2, (y1+y2)/2-1); -Res + = Searchmatrixrecur (Matrix,target,upx,upy, (x1+x2)/2-1, y2);Wuyi } the}Else { - intRIGHTX = (x1+x2)/2; Wu intrighty =Y2; - intLOWX =x2; About intLowY = (y1+y2)/2; $ if(Target==matrix.get (RIGHTX). Get (righty)) res++; - if(Target==matrix.get (LOWX). Get (LowY)) res++; - if(Target >= matrix.get (RIGHTX). Get (righty) && target >=Matrix.get (LOWX). Get (LowY)) { -Res + = Searchmatrixrecur (matrix,target,midx+1,midy+1, x2,y2); A}Else if(Target >=Matrix.get (RIGHTX). Get (righty)) { +Res + = Searchmatrixrecur (Matrix,target, (x1+x2)/2+1, y1,x2,y2); the}Else if(Target >=Matrix.get (LOWX). Get (LowY)) { -Res + = Searchmatrixrecur (Matrix,target, X1, (y1+y2)/2+1, x2, y2); $}Else { theRes + = Searchmatrixrecur (Matrix,target, (x1+x2)/2+1, Y1, lowx, LowY); theRes + = Searchmatrixrecur (Matrix,target, X1, (y1+y2)/2+1, x2, y2); the } the - } in returnRes; the } the}
Lintcode-search 2D Matrix II