题解 | #矩阵元素查找#

矩阵元素查找

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

全部评论

相关推荐

肖先生~:那年秋招闯进一位少年,人们都清楚:成功对他来说只是时间问题
点赞 评论 收藏
分享
点赞 评论 收藏
分享
友友们,我实在是不太明白,校招的话现在大多也是提前实习,然后转正也是需要考核的,考核通过才能转正,那这跟实习转正有什么区别啊
苦闷的仰泳鲈鱼刷了1...:提前实习,是让你提前熟悉业务的,后续是入职后可以减少试用期的(大部分是包入职的);转正实习,要是hc不够或者其他原因,让你正式offer可能都没有,这个风险很大。 ---个人看法和了解到的。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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