博客
关于我
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 | pyautogui,一个超酷的 Python 库!
    查看>>
    python | pybaobabdt,一个超强的 决策树可视化 Python 库!
    查看>>
    python | pycco,一个神奇的 Python 库!
    查看>>
    python | pyg2plot,一个有趣的 数据可视化 Python 库!
    查看>>
    python | pymc,一个超强的 Python 库!
    查看>>
    python | pynsist,一个强大的 Python 库!
    查看>>
    python | pyparsing,一个强大的 Python 库!
    查看>>
    python | pyqtgraph,一个神奇的 Python 库!
    查看>>
    python读取文本文件数据
    查看>>
    python | Python mock对象与测试替身
    查看>>
    python | Python pandas实现数据追加和合并的最佳方法
    查看>>
    python | Python 中检查一个数字是否是三态数
    查看>>
    python | Python 蒙特卡洛模拟
    查看>>
    python | python-docx,一个超厉害的 Python 库!
    查看>>
    python | Python中使用@property装饰器
    查看>>
    python | Python中的functools模块高级应用
    查看>>
    python | Python中的itertools模块使用技巧
    查看>>
    python | Python中的事件驱动编程模型
    查看>>
    python | Python中的内存池与缓存机制
    查看>>
    python | Python中的弱引用与内存管理
    查看>>