我是靠谱客的博主 潇洒翅膀,最近开发中收集的这篇文章主要介绍给定M*N矩阵,每一行、每一列都按升序排列,找出某元素,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

/**

 * 功能:给定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

最后

以上就是潇洒翅膀为你收集整理的给定M*N矩阵,每一行、每一列都按升序排列,找出某元素的全部内容,希望文章能够帮你解决给定M*N矩阵,每一行、每一列都按升序排列,找出某元素所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(46)

评论列表共有 0 条评论

立即
投稿
返回
顶部