题解 | #矩阵元素查找#
矩阵元素查找
https://www.nowcoder.com/practice/3afe6fabdb2c46ed98f06cfd9a20f2ce
using System;
using System.Collections.Generic;
class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param mat int整型二维数组
* @param n int整型
* @param m int整型
* @param x int整型
* @return int整型一维数组
*/
public List<int> findElement (List<List<int>> mat, int n, int m, int x) {
// write code here
if (mat == null || mat.Count == 0 || mat[0] == null || mat[0].Count == 0 ||
n == 0 || m == 0)
return null;
for (int i = 0; i < n; i++) {
if (x > mat[i][m - 1])
continue;
int nL = 0, nR = m - 1;
while (nR >= nL) {
int nM = (nL + nR) / 2;
if (mat[i][nM] > x)
nR = nM - 1;
else if (mat[i][nM] == x)
return new List<int>() {
i, nM
};
else
nL = nM + 1;
}
}
return null;
}
}

联想公司福利 1506人发布