博客
关于我
leetcode 004.寻找两个有序数组的中位数
阅读量:110 次
发布时间:2019-02-26

本文共 2012 字,大约阅读时间需要 6 分钟。

??????????????????????????

?????????

  • ??????????????????????????????
  • ???
    • ????????nums????????????
    • ?????i?j??????????????
    • ???????????????????????????????????????
  • ??????O(m + n)??????????????
  • ??????O(m + n)????????????
  • ?????????

  • ????????????k?????k?(m + n)/2?(m + n)/2 + 1?
  • ???
    • ?????index1?index2??????????????
    • ????????k???????
    • ??????????????????????????????????
  • ??????O(log(m + n))???????????????
  • ??????O(1)?????????????????
  • ?????????

  • ???????????????????
  • ???????????????????????????????????
  • ??????O(log(m + n))???????????
  • ??????O(1)?????????????????
  • ????

    • ?????????????????????????????????
    • ?????????????????????????
    • ???????????????????????

    ????

    ????????????

    class Solution {    public double findMedianSortedArrays(int[] nums1, int[] nums2) {        int len1 = nums1.length, len2 = nums2.length;        int totalLength = len1 + len2;        if (totalLength % 2 == 1) {            int midIndex = totalLength / 2;            return getKthElement(nums1, nums2, midIndex + 1);        } else {            int midIndex1 = totalLength / 2 - 1;            int midIndex2 = totalLength / 2;            double median = (getKthElement(nums1, nums2, midIndex1 + 1) + getKthElement(nums1, nums2, midIndex2 + 1)) / 2.0;            return median;        }    }    public int getKthElement(int[] nums1, int[] nums2, int k) {        int l1 = nums1.length, l2 = nums2.length;        int i = 0, j = 0;        while (true) {            if (i == l1) return nums2[j + k - 1];            if (j == l2) return nums1[i + k - 1];            if (k == 1) return Math.min(nums1[i], nums2[j]);            int half = k / 2;            int ni1 = Math.min(i + half, l1) - 1;            int ni2 = Math.min(j + half, l2) - 1;            int p1 = nums1[ni1], p2 = nums2[ni2];            if (p1 <= p2) {                k -= (ni1 - i + 1);                i = ni1 + 1;            } else {                k -= (ni2 - j + 1);                j = ni2 + 1;            }        }    }}

    ??

    ?????????????????????????????????????????????????????????????????????????

    转载地址:http://qtqk.baihongyu.com/

    你可能感兴趣的文章
    Python函数之返回值、作用域和局部变量
    查看>>
    Python+统计学 | 探索常用的数据分析统计分布
    查看>>
    python, selenium中用于切换帧的函数
    查看>>
    python函数
    查看>>
    python----线程、进程、协程的区别及多线程详解
    查看>>
    python---5
    查看>>
    python---使用celery分布式系统异步发送短信(云通讯)来验证登录
    查看>>
    python--003--百分号字符串拼接、format
    查看>>
    Python--10、线程
    查看>>
    Python--Redis实战:第四章:数据安全与性能保障:第4节:复制
    查看>>
    python--基本代码规范
    查看>>
    Python--抽象类接口类
    查看>>
    PYTHON--欧拉习题二
    查看>>
    python--高级应用总结
    查看>>
    python-520表白代码,我看谁说程序员不懂浪漫,送你几套表白代码,一步到位
    查看>>
    Python-Asyncio TypeError:无法在‘;等待‘;表达式中使用对象DICT
    查看>>
    Python-AttributeError:‘;dict&39;对象没有属性‘;序列‘;
    查看>>
    Python-ldap:是否可以在不显式写入密码的情况下进行绑定?
    查看>>
    python出现 unknown encoding: idna 的处理方法
    查看>>
    Python出现 RuntimeError: dictionary changed size during iteration 的解决方法
    查看>>