题解 | #矩阵元素查找#

矩阵元素查找

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;
    }
}

全部评论

相关推荐

狄文君:多段项目经历 + 专业技能拉满,这简历很能打,坐等好消息!
点赞 评论 收藏
分享
1jian10:48h没写面评会变成这样
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务