专注于 JetBrains IDEA 全家桶,永久激活,教程
持续更新 PyCharm,IDEA,WebStorm,PhpStorm,DataGrip,RubyMine,CLion,AppCode 永久激活教程

LeetCode 4. 寻找两个有序数组的中位数

leetcode-cn.com/problems/me…

81_1.png

理解中位数:

baike.baidu.com/item/中位数/30…

81_2.png

思路:

81_3.png

public double findMedianSortedArrays(int[] nums1, int[] nums2) {
    int i1 = 0, i2 = 0, i = 0, len = nums1.length + nums2.length;
    int[] arr = new int[len];
    while (true) {
        // 每队选出一个代表
        Integer num1Val = i1 < nums1.length ? nums1[i1] : null;
        Integer num2Val = i2 < nums2.length ? nums2[i2] : null;
        // 开始比赛
        if (num1Val != null) {
            if (num2Val != null) {
                arr[i++] = num1Val < num2Val ? nums1[i1++] : nums2[i2++];
            } else {
                arr[i++] = nums1[i1++];
            }
        } else {
            if (num2Val != null) {
                arr[i++] = nums2[i2++];
            } else {
                break;
            }
        }
        if (i > len/2) {
            return len % 2 == 0 ? (arr[len / 2 - 1] + arr[len / 2]) / 2d : arr[(len + 1) / 2 - 1] * 1d;
        }
    }
    return 0d;
}

文章永久链接:https://tech.souyunku.com/43469

未经允许不得转载:搜云库技术团队 » LeetCode 4. 寻找两个有序数组的中位数

JetBrains 全家桶,激活、破解、教程

提供 JetBrains 全家桶激活码、注册码、破解补丁下载及详细激活教程,支持 IntelliJ IDEA、PyCharm、WebStorm 等工具的永久激活。无论是破解教程,还是最新激活码,均可免费获得,帮助开发者解决常见激活问题,确保轻松破解并快速使用 JetBrains 软件。获取免费的破解补丁和激活码,快速解决激活难题,全面覆盖 2024/2025 版本!

联系我们联系我们