首页 > 其他分享 >[LeetCode]004-寻找两个正序数组的中位数

[LeetCode]004-寻找两个正序数组的中位数

时间:2022-12-15 22:55:21浏览次数:40  
标签:正序 int nums1 findKth 004 return LeetCode nums2 size

>>>传送门

题目

给定两个大小分别为 mn 的正序(从小到大)数组 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

提示

  • nums1.length == m
  • nums2.length == n
  • 0 <= m <= 1000
  • 0 <= n <= 1000
  • 1 <= m + n <= 2000
  • -106 <= nums1[i], nums2[i] <= 106

题解

class Solution {
public:
    double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
        int total = nums1.size() + nums2.size();
        if (total % 2 == 0) {
            int left = findKth(nums1, 0, nums2, 0, total / 2);
            int right = findKth(nums1, 0, nums2, 0, total / 2 + 1);
            return (left + right) / 2.0;
        }
        else {
            return findKth(nums1, 0, nums2, 0, total / 2 + 1);
        }
    }

    int findKth(vector<int>& nums1, int i, vector<int>& nums2, int j, int k) {
        if (nums2.size() - j < nums1.size() - i) return findKth(nums2, j, nums1, i, k);
        if (nums1.size() == i) return nums2[j + k - 1];
        if (k == 1) return min(nums1[i], nums2[j]);

        int si = min((int)nums1.size(), i + k / 2), sj = j + k / 2;
        if (nums1[si - 1] < nums2[sj - 1]) {
            return findKth(nums1, si, nums2, j, k - (si - i));
        }
        else {
            return findKth(nums1, i, nums2, sj, k - (sj - j));
        }
    }
};

标签:正序,int,nums1,findKth,004,return,LeetCode,nums2,size
From: https://www.cnblogs.com/yuyork/p/16986188.html

相关文章

  • LeetCode HOT 100:旋转图像
    题目:48.旋转图像题目描述:给你一个正方形矩阵数组,将其中的所有元素都顺时针旋转90度,得到旋转之后的矩阵数组。本题要求必须在原地修改,不能使用额外空间。思路:看到这个......
  • LeetCode 题解 2487. 从链表中移除节点
    2487.从链表中移除节点-力扣(Leetcode)题解思路一:递归逆序structListNode*removeNodes(structListNode*head){if(head->next==NULL)//遍历到链表末尾......
  • LeetCode HOT 100:全排列
    题目:46.全排列题目描述:给你一个没有重复元素的数组,返回其所有可能的全排列。全排列是什么呢?举个简单的例子,数组[1,2,3],其中一个排列为[2,1,3],该数组所有的全排列为[......
  • R语言代做编程辅导因子实验设计STA305/1004 Assignment(附答案)
    全文链接:http://tecdat.cn/?p=30896(AdaptedfromWu,Hamada,2009)Thefollowingexperimentwasperformedatapulpmill.Plantperformanceisbasedonpulpbri......
  • [LeetCode]003-无重复字符的最长子串
    >>>传送门题目给定一个字符串s,请你找出其中不含有重复字符的最长子串的长度。示例示例1输入:s="abcabcbb"输出:3解释:因为无重复字符的最长子串是"abc",......
  • ogg在启动应用进程时报错OGG-00412
    问题描述:ogg在启动应用进程时报错OGG-00412,如下所示:源端:IP192.168.133.108数据库oracle10.2.0.464位,实例名:orcl主机名:leo-10g-ogg+oel5.1164位目标端:IP192.......
  • LeetCode80. 删除排序数组中的重复项 II
    给定一个排序数组,你需要在​​原地​​删除重复出现的元素,使得每个元素最多出现两次,返回移除后数组的新长度。不要使用额外的数组空间,你必须在​​原地​​修改输入数组并在......
  • LeetCode-Java-872. Leaf-Similar Trees
    题目Consideralltheleavesofabinarytree.Fromlefttorightorder,thevaluesofthoseleavesformaleafvaluesequence.假装有图Forexample,inthegiven......
  • LeetCode-Java-136. Single Number
    题目Givenanon-emptyarrayofintegers,everyelementappearstwiceexceptforone.Findthatsingleone.Note:Youralgorithmshouldhavealinearruntimecompl......
  • LeetCode-Java-637. Average of Levels in Binary Tree
    题目Givenanon-emptybinarytree,returntheaveragevalueofthenodesoneachlevelintheformofanarray.Example1:Input:3/\920/\15......