zoukankan      html  css  js  c++  java
  • 寻找两个正序数组的中位数寻找两个正序数组的中位数

    4. 寻找两个正序数组的中位数
    给定两个大小为 m 和 n 的正序(从小到大)数组 nums1 和 nums2。请你找出并返回这两个正序数组的中位数。
    进阶:你能设计一个时间复杂度为 O(log (m+n)) 的算法解决此问题吗?
    示例 1:
    输入:nums1 = [1,3], nums2 = [2]
    输出:2.00000
    解释:合并数组 = [1,2,3] ,中位数 2
    示例 2:
    输入:nums1 = [1,2], nums2 = [3,4]
    输出:2.50000
    解释:合并数组 = [1,2,3,4] ,中位数 (2 + 3) / 2 = 2.5
    示例 3:
    输入:nums1 = [0,0], nums2 = [0,0]
    输出:0.00000
    示例 4:
    输入:nums1 = [], nums2 = [1]
    输出:1.00000
    示例 5:
    输入:nums1 = [2], nums2 = []
    输出:2.00000
    提示:
    nums1.length == m
    nums2.length == n
    0 <= m <= 1000
    0 <= n <= 1000
    1 <= m + n <= 2000
    -106 <= nums1[i], nums2[i] <= 106

    解法一:比较拉跨的解法;

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;
    
    
    public class code_23_medianOfTwoSortedArrays {
        public static void main(String[] args) {
            int[] nums1 = {1,2};
            int[] nums2 = {4,6};
            System.out.println(findMedianSortedArrays(nums1,nums2));
        }
        /*
         * 第一种方法比较简单,不考虑时间复杂度来解决问题, 使用一个容器接着排序找出中位数
         */
        public static double findMedianSortedArrays(int[] nums1, int[] nums2) {
            int[] new_array = new int[nums1.length + nums2.length];
            for (int i = 0; i < new_array.length; i++) {
                if (i < nums1.length) {
                    new_array[i] = nums1[i];
                }
                if (i >= nums1.length) {
                    new_array[i] = nums2[i - nums1.length];
                }
            }
            List<Integer> resultList= new ArrayList<>();
            for (int i = 0; i < new_array.length; i++) {
                resultList.add(new_array[i]);
            }
            Collections.sort(resultList,new IntegerComparator());
            if(new_array.length %2 == 0){
                return (resultList.get(resultList.size() / 2) + resultList.get(resultList.size() / 2 - 1)) / 2;
            }else {
                return resultList.get(resultList.size() / 2);
            }
        }
    
    }
    class IntegerComparator implements Comparator<Integer>{
     
        public int compare(Integer o1, Integer o2) {
            return o1 - o2;
        }
    }

    解法二:使用大根堆,小根堆解决问题:获取大根堆的堆顶弹出,

    public class code_24_medianOfTwoSortedArraysWithHeap {
        public static double findMedianSortedArrays(int[] nums1, int[] nums2) {
            PriorityQueue<Integer> SmallRootHeap = new PriorityQueue<>(new SmallRootHeapComparator());  // 小根堆
            PriorityQueue<Integer> BigRootHeap = new PriorityQueue<>(new BigRootHeapComparator());    // 大根堆
            
            // 维护大小根堆中的数据个数差值位1
            ArrayList<Integer> list = new ArrayList<>();
            for (int i = 0; i < nums1.length; i++) {
                list.add(nums1[i]);
            }
            for (int i = 0; i < nums2.length; i++) {
                list.add(nums2[i]);
            }
            NumberOfMaintainedData(list,SmallRootHeap,BigRootHeap);
            if (list.size() % 2 == 0) {
                return (SmallRootHeap.poll() + BigRootHeap.poll()) / 2;
            }
            return BigRootHeap.poll();
        }
    
        private static void NumberOfMaintainedData(ArrayList<Integer> list, PriorityQueue<Integer> smallRootHeap,
                PriorityQueue<Integer> bigRootHeap) {
            for (int i = 0; i < list.size(); i++) {
                if(smallRootHeap.size() == 0 && bigRootHeap.size() == 0){
                    bigRootHeap.add(list.get(i));
                    continue;
                }
                while(smallRootHeap.size() - bigRootHeap.size() > 1){ // 说明smallRootHeap中的数据较为多
                    bigRootHeap.add(smallRootHeap.poll());
                }
                while(bigRootHeap.size() - smallRootHeap.size() > 1){ // 说明bigRootHeap中的数据较为多
                    smallRootHeap.add(bigRootHeap.poll());
                }
                if (Math.abs(smallRootHeap.size() - bigRootHeap .size())<=1) {
                    bigRootHeap.add(list.get(i));
                }
            }
            
        }
    
        
    }
    class SmallRootHeapComparator implements Comparator<Integer> {
    
        public int compare(Integer o1, Integer o2) {
            return o1 - o2;
        }
    }
    class BigRootHeapComparator implements Comparator<Integer> {
    
        public int compare(Integer o1, Integer o2) {
            return o2 - o1;
        }
    }
  • 相关阅读:
    DOM事件
    DOM样式操作
    asp.net-枚举绑定控件
    微信站点 点击 “退回” 按钮退回到主菜单
    阻止iOS中页面弹性回滚,只允许div.phone_body的区块有弹性
    asp.net 微信开发失效汇总
    ECharts使用心得
    PV、UPV、UV简介
    微信浏览器取消缓存的方法
    Visual Studio 2015简体中文企业版/专业版下载+有效激活密钥
  • 原文地址:https://www.cnblogs.com/whr-blogs/p/leetcode_03.html
Copyright © 2011-2022 走看看