博客
关于我
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/

    你可能感兴趣的文章
    python3.6 ubuntu部署nginx、 uwsgi、 django
    查看>>
    Python3.6.3中,functools似乎不能用
    查看>>
    python3.6.5+cuda9+cudnn7.1+win10+tensorflow-gpu1.9.0下载配置
    查看>>
    python3.6使用pickle序列化class
    查看>>
    python3.6入门到高阶(全栈) day027 网络通信协议 osi七层模型
    查看>>
    Python3.6属性错误:模块‘;异步没有属性‘;运行‘;
    查看>>
    python3.6版本中的python-weka-wrapper安装失败,错误代码为%1
    查看>>
    Python3.7安装cv2踩过的坑
    查看>>
    python写txt文件
    查看>>
    python关于sorted排序函数详细解析 附实战代码(全)
    查看>>
    python3.8调用m文件,调用matlab
    查看>>
    python3.9新特性_Python 3.9 新特性速览
    查看>>
    Python3.x:遍历select下拉框获取value值
    查看>>
    python360百科_11月编程排行榜:Python超过Java成第二
    查看>>
    Python关于Paginator分页函数详细解析 附实战代码
    查看>>
    python3、ipython3、setup-tools、pip等环境搭建详细总结
    查看>>
    python3下使用cv2.imwrite存储带有中文路径图片或者绝对路径图片
    查看>>
    python3与django中@property详解
    查看>>
    python3中如何表示无穷大
    查看>>
    Python3中提示:no module named 'wxpy'
    查看>>