【每日一题】【数组复制】2021年12月26日-4. 寻找两个正序数组的中位数
给定两个大小分别为 m 和 n 的正序(从小到大)数组 nums1 和 nums2。请你找出并返回这两个正序数组的 中位数 。
算法的时间复杂度应该为 O(log (m+n)) 。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/median-of-two-sorted-arrays
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

答案
class Solution { public double findMedianSortedArrays(int[] nums1, int[] nums2) { int n1 = nums1.length, n2 = nums2.length; int[] nums = new int[n1 + n2]; System.arraycopy(nums1, 0, nums, 0, n1); System.arraycopy(nums2, 0, nums, n1, n2); Arrays.sort(nums); int len = nums.length; if(len % 2 == 0) { return (nums[len / 2 - 1] + nums[len / 2]) / 2.0; } else { return nums[len / 2]; } } }
本文来自博客园,作者:哥们要飞,转载请注明原文链接:https://www.cnblogs.com/liujinhui/p/15733577.html
                    
                
                
            
        
浙公网安备 33010602011771号