博客
关于我
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+selenium+unittest的GUI自动化框架实现
    查看>>
    python函数拟合不规则曲线_Python计算&绘图——曲线拟合问题(转)
    查看>>
    python函数定义的基本格式_零基础学python-2.19 定义函数、调用函数与默认参数
    查看>>
    Python+Selenium之数据驱动测试的实现
    查看>>
    python函数定义与使用+返回值简解
    查看>>
    Python+Selenium登录
    查看>>
    Python日期时间模块
    查看>>
    Python函数合集:足足68个内置函数请收好!
    查看>>
    Python+Selenium自动化测试项目实战
    查看>>
    Python+Selenium自动化测试项目实战【建议收藏】
    查看>>
    Python+Selenium自动化测试:Page Object模式
    查看>>
    python+selenium进行cnblog的自动化登录测试
    查看>>
    Python函数只返回第一个值而不是数据框
    查看>>
    Python+SQL实战:京东用户行为数据分析案例解析(上)
    查看>>
    Python+SQL实战:京东用户行为数据分析案例解析(下)
    查看>>
    PYTHON+Twisted+sqlanydb=ABORT()
    查看>>
    Python函数之返回值、作用域和局部变量
    查看>>
    Python+统计学 | 探索常用的数据分析统计分布
    查看>>
    python, selenium中用于切换帧的函数
    查看>>
    python函数
    查看>>