/**
* 思路:若列的末端大于x,那么x位于该列的左边;若行的开头小于x,那么x位于列的下边。从矩阵中的子矩阵中查找元素。
* @param matrix
* @param elem
* @return
*/
public static boolean findElement(int[][] matrix,int elem){
int row=0;
int col=matrix[0].length-1;
while(row<matrix.length&&col>=0){
if(matrix[row][col]==elem)
return true;
else if(matrix[row][col]>elem)
col--;
else
row++;
}
return false;
}
方法二:
/**
* 思路:由于每一个元素都大于它左边和上边的元素,所以,若在矩阵里任意画长方形,其右下角的元素一定是最大的,左上角的元素一定是最小的。
* 将矩阵分为四个区域,以递归方式搜索左下区域和右上区域。
* @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和end 分别设为对角线的起点和终点,矩阵不一定是正方形,因此对角线的终点也不一定是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);
//在对角线上进行二分查找
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;
}
}
//将矩阵分为四个区域,搜索左下区域和右上区域
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&&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;
}
}版权声明:本文为博主原创文章,未经博主允许不得转载。
9.11排序与查找(六)——给定M*N矩阵,每一行、每一列都按升序排列,找出某元素
原文地址:http://blog.csdn.net/shangqing1123/article/details/47831189