题解 | #矩阵元素查找#

矩阵元素查找

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

全部评论

相关推荐

02-14 12:40
门头沟学院 Java
程序员花海:1.面试要求必须Java笔试不一定 2.难度对等秋招 远超于日常实习是因为同一批次且转正很多 竞争压力大 3.第一个加点指标,上线了就把接口性能加上去 使用本地缓存这个不算亮点 只是技术选型,要把为什么采用这个和背后的思考写出来而不是单纯堆叠技术没意义 4.八股要一直看 很容易忘记 5.拼团交易这个老问题 堆积技术 另外建议你把奖项合并到教育背景 没必要拆出来放最后
我的简历长这样
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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