题解 | #多数组中位数#

多数组中位数

https://www.nowcoder.com/practice/b6bb0bce88894108bfc23e9b7b012420

class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param arr1 int整型一维数组
     * @param arr2 int整型一维数组
     * @return int整型
     *
     */
    public int getUpMedian (int[] arr1, int[] arr2) {
        // write code here
        int totalSteps = (arr1.length + arr2.length + 1) / 2;
        int p1 = 0, p2 = 0, ans = 0, steps = 0;

        while (p1 < arr1.length && p2 < arr2.length && steps < totalSteps) {
            if (arr1[p1] < arr2[p2]) {
                ans = arr1[p1];
                p1++;
            } else {
                ans = arr2[p2];
                p2++;
            }
            steps++;
        }
        while (p1 < arr1.length && steps < totalSteps) {
            ans = arr1[p1];
            p1++;
            steps++;
        }
        while (p2 < arr2.length && steps < totalSteps) {
            ans = arr2[p2];
            p2++;
            steps++;
        }
        return ans;
    }
}

#我的实习求职记录#
全部评论

相关推荐

qq乃乃好喝到咩噗茶:院校后面加上211标签,放大加粗,招呼语也写上211
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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