/*** Function: Given the m*n matrix, each row and column is sorted in ascending order to find an element. */
Two ways:
method One:
/** * Idea: If the end of the column is greater than X, then x is on the left side of the column, and if the beginning of the line is less than x, then x is below the column. Finds an element from a sub-matrix in a matrix. * @param matrix * @param elem * @return */public static Boolean findelement (int[][] matrix,int elem) {int Row=0;int Col=mat Rix[0].length-1;while (row<matrix.length&&col>=0) {if (Matrix[row][col]==elem) return True;else if ( Matrix[row][col]>elem) col--;elserow++;} return false;}
Method Two:
/** * Idea: Because each element is greater than its left and upper elements, so, if in the matrix arbitrarily draw a rectangle, its lower right corner of the element must be the largest, the upper left corner of the element must be the smallest. * Divide the matrix into four regions to recursively search the lower left and upper right areas. * @param matrix * @param elem */public void FindElement2 (int[][] matrix,int elem) {coordinate origin=new coordinate (0,0); coordinate dest=new coordinate (matrix.length-1,matrix[0].length-1); Find (Matrix, origin, dest, elem);} Public coordinate find (int[][] matrix,coordinate origin,coordinate dest,int x) {if (!origin.inbounds (matrix) | |! Dest.inbounds (Matrix)) return Null;if (matrix[origin.row][origin.column]==x) return Origin;else if (!origin.isbefore ( dest) return Null;//start and end are set to the beginning and end of the diagonal, and the matrix is not necessarily a square, so the end of the diagonal is not necessarily dest. coordinate start= (coordinate) origin.clone (); int distance=math.min (Dest.row-origin.row, Dest.column-origin.column) ; coordinate end=new coordinate (start.row+distance, start.column+distance); coordinate p=new coordinate (0,0);//The two-point lookup on the diagonal while (Start.isbefore (end)) {P.settoaverage (start, end); if (x> Matrix[p.row][p.column]) {start.row=p.row+1;start.column=p.column+1;} else{end.row=p.row-1;end.column=p.column-1;}} Divide the matrix into four regions, search the lower left and upper right areas return Partitionaandsearch (MATRIX,ORIGIN,DEST,START,X);} Public coordinate Partitionaandsearch (int[][] matrix,coordinate origin,coordinate dest,coordinate pivot,int elem) { coordinate lowerleftorigin=new coordinate (pivot.row, origin.column); coordinate lowerleftdest=new coordinate (dest.row,pivot.column-1); coordinate upperrightorigin=new coordinate (origin.row,pivot.column); coordinate upperrightdest=new coordinate (pivot.row-1,dest.column); Coordinate Lowerleft=find (matrix, Lowerleftorigin, Lowerleftdest, elem); if (Lowerleft==null) return find (Matrix, Upperrightorigin, Upperrightdest, Elem); return lowerleft;} class coordinate implements cloneable{public int row;public int column;public coordinate (int r,int c) {this.row=c; This.column=c;} public boolean inbounds (int[][] matrix) {return row>=0&&row<matrix.length&&column>=0& &column<matrix[0].length;} public boolean isbefore (coordinate p) {return this.row<=p.row&&amP;this.column<=p.column;} Public Object Clone () {return new coordinate (row,column);} public void settoaverage (coordinate min,coordinate max) {row= (Min.row+max.row)/2;column= (min.column+max.column)/2;}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
9.11 Sorting and finding (vi)--given a m*n matrix, each row and column is sorted in ascending order to find an element