给定一个m[i][j]的矩阵
如果高校在这个矩阵里面寻找这个元素?
元素满足:
1、元素在所在行的值最大,且是所在行第一个出现
2、元素在所在列的值最大,且是所在列第一个出现条件,对于矩阵中的元素,有以下特征:
1、m[i+1][j]>=m[i][j];
2、m[i][j+1]>=m[i][j];
3、m[i+1][j+1]>=m[i][j];如下例m:
[0][0]:0    [0][1]:0    [0][2]:0    
[1][0]:0    [1][1]:3    [1][2]:3    
[2][0]:0    [2][1]:3    [2][2]:5    
[3][0]:0    [3][1]:3    [3][2]:5    
[4][0]:0    [4][1]:3    [4][2]:6 能找出三个数:分别为0,3,6;期望:
1、给出算法,有代码实现为最佳。
2、有算法时间和空间复杂度分析。

解决方案 »

  1.   


    public static int[][] getMax(int m[][]){
    int x=m.length;
    int y=m[0].length;
    int tmpx=0,tmpy=0;
    int w[][] = new int[x][y];
    int tmpMax=0;
    for(int i=0;i<x;i++){
    for(int j=0;j<y;j++){
    if(m[i][j]>tmpMax){
    tmpMax=m[i][j];
    tmpx=i;
    tmpy=j;
    }
    }
    int tmpflag=0;
    for(int j=0;j<x;j++){
    if(m[j][tmpy]>tmpMax||(m[j][tmpy]==tmpMax&&j<tmpx)){
    tmpflag=-1;
    break;
    }
    }
    //是找到的结点
    if(tmpflag==0&&tmpx!=0&&tmpy!=0){
    w[tmpx][tmpy]=tmpMax-m[tmpx-1][tmpy-1];
    //System.out.println(tmpMax==m[tmpx][tmpy]);
    }
    tmpx=0;
    tmpy=0;
    tmpMax=0;
    }
    return w;
    }大家把改进之处帖出来吧???