给定M*N矩阵,每一行、每一列都按升序排列,找出某元素

/**

 * 功能:给定M*N矩阵,每一行、每一列都按升序排列,找出某元素。

 */

 

两种方法:

 

方法一:

[java] view plain copy

 

  1. /** 
  2.  * 思路:若列的末端大于x,那么x位于该列的左边;若行的开头小于x,那么x位于列的下边。从矩阵中的子矩阵中查找元素。 
  3.  * @param matrix 
  4.  * @param elem 
  5.  * @return 
  6.  */  
  7. public static boolean findElement(int[][] matrix,int elem){  
  8.     int row=0;  
  9.     int col=matrix[0].length-1;  
  10.       
  11.     while(row<matrix.length&&col>=0){  
  12.         if(matrix[row][col]==elem)  
  13.             return true;  
  14.         else if(matrix[row][col]>elem)  
  15.             col--;  
  16.         else  
  17.             row++;  
  18.     }  
  19.     return false;  
  20. }  

 

方法二:

 

[java] view plain copy

 

  1. /** 
  2.  * 思路:由于每一个元素都大于它左边和上边的元素,所以,若在矩阵里任意画长方形,其右下角的元素一定是最大的,左上角的元素一定是最小的。 
  3.  * 将矩阵分为四个区域,以递归方式搜索左下区域和右上区域。 
  4.  * @param matrix 
  5.  * @param elem 
  6.  */  
  7. public void findElement2(int[][] matrix,int elem){  
  8.     Coordinate origin=new Coordinate(0,0);  
  9.     Coordinate dest=new Coordinate(matrix.length-1,matrix[0].length-1);  
  10.     find(matrix, origin, dest, elem);  
  11. }  
  12.   
  13. public Coordinate find(int[][] matrix,Coordinate origin,Coordinate dest,int x){  
  14.     if(!origin.inBounds(matrix)||!dest.inBounds(matrix))  
  15.         return null;  
  16.       
  17.     if(matrix[origin.row][origin.column]==x)  
  18.         return origin;  
  19.     else if(!origin.isBefore(dest))  
  20.         return null;  
  21.       
  22.     //start和end 分别设为对角线的起点和终点,矩阵不一定是正方形,因此对角线的终点也不一定是dest。  
  23.     Coordinate start=(Coordinate) origin.clone();  
  24.     int distance=Math.min(dest.row-origin.row, dest.column-origin.column);  
  25.       
  26.     Coordinate end=new Coordinate(start.row+distance, start.column+distance);  
  27.       
  28.     Coordinate p=new Coordinate(0,0);  
  29.       
  30.     //在对角线上进行二分查找  
  31.     while(start.isBefore(end)){  
  32.         p.setToAverage(start, end);  
  33.         if(x>matrix[p.row][p.column]){  
  34.             start.row=p.row+1;  
  35.             start.column=p.column+1;  
  36.         }else{  
  37.             end.row=p.row-1;  
  38.             end.column=p.column-1;  
  39.         }  
  40.     }  
  41.     //将矩阵分为四个区域,搜索左下区域和右上区域  
  42.     return partitionAandSearch(matrix,origin,dest,start,x);  
  43.       
  44. }  
  45.   
  46. public Coordinate partitionAandSearch(int[][] matrix,Coordinate origin,Coordinate dest,Coordinate pivot,int elem){  
  47.     Coordinate lowerLeftOrigin=new Coordinate(pivot.row, origin.column);  
  48.     Coordinate lowerLeftDest=new Coordinate(dest.row,pivot.column-1);  
  49.       
  50.     Coordinate upperRightOrigin=new Coordinate(origin.row,pivot.column);  
  51.     Coordinate upperRightDest=new Coordinate(pivot.row-1,dest.column);  
  52.       
  53.     Coordinate lowerLeft=find(matrix, lowerLeftOrigin, lowerLeftDest, elem);  
  54.     if(lowerLeft==null)  
  55.         return find(matrix, upperRightOrigin, upperRightDest, elem);  
  56.     return lowerLeft;  
  57. }  
  58.   
  59.   
  60.   
  61. lass Coordinate implements Cloneable{  
  62. public int row;  
  63. public int column;  
  64. public Coordinate(int r,int c){  
  65.     this.row=c;  
  66.     this.column=c;  
  67. }  
  68.   
  69. public boolean inBounds(int[][] matrix){  
  70.     return row>=0&&row<matrix.length&&column>=0&&column<matrix[0].length;  
  71. }  
  72.   
  73. public boolean isBefore(Coordinate p){  
  74.     return this.row<=p.row&&this.column<=p.column;  
  75. }  
  76.   
  77. public Object clone(){  
  78.     return new Coordinate(row,column);  
  79. }  
  80.   
  81. public void setToAverage(Coordinate min,Coordinate max){  
  82.     row=(min.row+max.row)/2;  
  83.     column=(min.column+max.column)/2;  
  84. }

或者

package com.huanchuang.arvin.vo;

public class Finder {
    private String findElement(int[][] matrix, int target) {
        int row = 0, column = 0;
        // 只要行还没有达到最大值就继续执行
        while (row < matrix.length) {
            int colMax = matrix[row].length - 1;// 用于获取矩阵每一行的最大值
            // 因为是行和列都是赠序的,只要指定的数在每一行的最小值和最大值之间,就返回true
            if (matrix[row][column] <= target && matrix[row][colMax] >= target) {
                for (int i = 0; i < matrix[row].length; i++) {
                    if (matrix[row][i] == target) {
                        return "matrix[" + row + "][" + i + "]";
                    }
                }
            } else {// 否则的话就自动去下一行进行比较
                row++;
            }
        }
        return "matrix[-1][-1]";// 返回-1表示不存在
    }

    public static void main(String[] args) {
        int matrix[][] = { { 1, 2, 3 }, { 4, 5 }, { 7, 8, 9 } };
        Finder finder = new Finder();
        String location = finder.findElement(matrix, 6);
        System.out.println("位置" + location);
    }

}

 

或者:

方法一:从矩阵的右上角开始找

 

[cpp] view plain copy

 

  1. bool HasElement(vector<vector<int>> martix,int elem)  
  2. {  
  3.     if(martix.empty() || martix[0].empty())  
  4.         return false;  
  5.     int row=0,col=martix[0].size()-1;//右上角元素  
  6.     while(row<martix.size() && col>=0)  
  7.     {  
  8.         if(martix[row][col]==elem)  
  9.             return true;  
  10.         else if(martix[row][col]>elem)  
  11.             col--;  
  12.         else  
  13.             row++;  
  14.     }  
  15.     return false;  
  16. }  


 

 

    方法二:从矩阵的左下角开始找

 

[cpp] view plain copy

 

  1. bool HasElement(vector<vector<int>> martix,int elem)  
  2. {  
  3.     if(martix.empty() || martix[0].empty())  
  4.         return false;  
  5.     int row=martix.size()-1,col=0;//左下角元素  
  6.     while(row>=0 && col<martix[0].size())  
  7.     {  
  8.         if(martix[row][col]==elem)  
  9.             return true;  
  10.         else if(martix[row][col]<elem)  
  11.             col++;  
  12.         else  
  13.             row--;  
  14.     }  
  15.     return false;  
  16. }

转载于:https://my.oschina.net/u/2822116/blog/793325

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值