利用该数组的单调性特性,从右上角开始查找,如果当前数==目标值则返回该位置。如果当前数大于目标数的话,那么根据单调性,数据必然在矩阵的左半边,即j--。如果当前数小于目标数的话,那么同样根据单调性,数据必然在矩阵的下半边,即i++。

import java.util.*;

public class Solution {
    public int[] findElement(int[][] mat, int n, int m, int x) {
        // write code here
        int i = 0, j = m - 1;
        while(i < n && j >= 0) {
            if(mat[i][j] == x) {
                return new int[]{i,j};
            } else if(mat[i][j] > x) {
                j--;
            } else {
                i++;
            }
        }
        return new int[]{-1,-1};
    }
}