• 题目描述

There are two sorted arrays nums1 and nums2 of size m and n respectively.

Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).

You may assume nums1 and nums2 cannot be both empty.

分别有大小为m和n的有序数组nums1和nums2。

求两个排序数组的中值。总的运行时复杂度应该是O(log (m+n))。

您可以假设nums1和nums2不能同时为空。

Example 1:

nums1 = [1, 3]
nums2 = [2] The median is 2.0

Example 2:

nums1 = [1, 2]
nums2 = [3, 4] The median is (2 + 3)/2 = 2.5
  • 思路分析:

需求是输出两个有序数组的中值,想要实现很是简单,最直接粗暴的方法是把两个数组合并,找到相应元素,时间复杂度为O(m+n)。但题目明确要求时间复杂度需要控制在O(log(m+n)),就必须思考优化版方法啦。

小陌目前的优化方法是用寻找第k小数(因为给出的两个数组皆有序,则k对应(m+n)/2):

每次比较两个数组的第k/2个大的数:

  1. nums1[k/2] = nums[k/2],那么这个数就是我们要找的第K小数
  2. nums1[k/2] > nums[k/2],则说明第K小数不在nums2中前k/2个元素,可排除
  3. nums1[k/2] < nums[k/2],与2同理,排除nums1中前k/2个元素

    如此操作,每次都能排除k/2的元素(递归过程中,可能会出现数组个数不到k/2的情况,并不会影响算法,从另一个数组进行补充,多取相应元素即可)。

当k=1时,找到第K小数,结束递归

  • 原码附录:
  1. 简单粗暴法
    class Solution {
    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
    int all = nums1.length+nums2.length;
    int[] result = new int[all];
    for(int i=0;i<all;i++){
    result[i] = (i<nums1.length)?nums1[i]:nums2[i-nums1.length];
    }
    Arrays.sort(result);
    return (all%2) ==1 ? result[(all-1)/2]:(double)(result[all/2]+result[all/2-1])/2;
    }
    }
  2. top k算法思想
class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
if((nums1.length+nums2.length)%2==0){
return (findK(nums1,0,nums1.length-1,nums2,0,nums2.length-1,(nums1.length+nums2.length)/2)
+findK(nums1,0,nums1.length-1,nums2,0,nums2.length-1,(nums1.length+nums2.length)/2+1))/2.0;
}
else{
return findK(nums1,0,nums1.length-1,nums2,0,nums2.length-1,(nums1.length+nums2.length)/2+1);
}
} public int findK(int[] nums1,int n1,int n2,int[] nums2,int m1,int m2,int k){
int i = n2 - n1 +1;
int j = m2 - m1 +1; if(i>j){
return findK(nums2,m1,m2,nums1,n1,n2,k);
}
if(i == 0){
return nums2[m1+k-1];
}
if(k == 1){
return Math.min(nums1[n1],nums2[m1]);
} int mid1 = Math.min(k/2,i);
int mid2 = k - mid1;
if(nums1[n1+mid1-1]==nums2[m1+mid2-1]){
return nums1[n1+mid1-1];
}
else if(nums1[n1+mid1-1]>nums2[m1+mid2-1]){
return findK(nums1,n1,n1+mid1-1,nums2,m1+mid2,m2,k-mid2);
}
else{
return findK(nums1,n1+mid1,n2,nums2,m1,m2+mid2-1,k-mid1);
}
}
}

以下内容为可耻的自我推销,完全可忽略(。•́ωก̀。).。

此处厚脸皮的安利小透明公众号 林夏夏

夏夏大大的英雄梦,
就是能和最喜欢的你,
分享一道算法题,
温习一个编程上易忽视小细节,
品一份书香氤氲,
道最后一句晚安ヾ(◍°∇°◍)ノ゙

Median of Two Sorted Arrays---LeetCode进阶路④的更多相关文章

  1. Median of Two Sorted Arrays LeetCode Java

    两排序好的数组,找中位数 描述There are two sorted arrays A and B of size m and n respectively. Find the median of ...

  2. 3.Median of Two Sorted Arrays Leetcode

    难度系数:5星 /*************************************************************************** 题目:有两个排好序的数组,要求 ...

  3. 4. Median of Two Sorted Arrays(topK-logk)

    4. Median of Two Sorted Arrays 题目 There are two sorted arrays nums1 and nums2 of size m and n respec ...

  4. 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays

    一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...

  5. LeetCode 4 Median of Two Sorted Arrays (两个数组的mid值)

    题目来源:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 an ...

  6. LeetCode(3) || Median of Two Sorted Arrays

    LeetCode(3) || Median of Two Sorted Arrays 题记 之前做了3题,感觉难度一般,没想到突然来了这道比较难的,星期六花了一天的时间才做完,可见以前基础太差了. 题 ...

  7. Kotlin实现LeetCode算法题之Median of Two Sorted Arrays

    题目Median of Two Sorted Arrays(难度Hard) 方案1,数组合并&排序调用Java方法 import java.util.* class Solution { fu ...

  8. Leetcode 4. Median of Two Sorted Arrays(二分)

    4. Median of Two Sorted Arrays 题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/ Descr ...

  9. leetcode第四题:Median of Two Sorted Arrays (java)

    Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find t ...

  10. LeetCode 4. Median of Two Sorted Arrays & 归并排序

    Median of Two Sorted Arrays 搜索时间复杂度的时候,看到归并排序比较适合这个题目.中位数直接取即可,所以重点是排序. 再来看看治阶段,我们需要将两个已经有序的子序列合并成一个 ...

随机推荐

  1. Scala面向对象之创建对象,重载构造方法,继承抽象类实现接口

    package com.wyh.day01 object ScalaClass { def main(args: Array[String]): Unit = { val student = new ...

  2. FormCreate设计器v5.6发布—AI加持的低代码表单设计器正式上线!

    近期DeepSeek可谓是刷遍全网,当然,在DeepSeek等AI技术的推动下,人工智能正以惊人的速度改变着各行各业.AI不仅是一种技术趋势,更是未来生产力的核心驱动力. 如今,FormCreate设 ...

  3. PDManer 入门教程:超强代码生成工具!

    PDManer 入门教程:超强代码生成工具!https://www.51cto.com/article/753161.html

  4. SQL注入之WAF绕过注入

    绕过WAF: WAF防御原理: 简单来说waf就是解析http请求,检测http请求中的参数是否存在恶意的攻击行为,如果请求中的参数和waf中的规则库所匹配,那么waf则判断此条请求为攻击行为并进行阻 ...

  5. 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异

    从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异 引言 在开发 Web 应用时,处理 HTTP 错误响应是常见的任务,尤其是在客户端代码中捕获并向用户展示错误信息.然而,当使用 HTTP ...

  6. CNVD挖掘思路

    CNVD挖掘思路 CNVD获取条件 首先,先来了解一下目前cnvd发证资格 1.事件型 事件型漏洞必须是三大运营商(移动.联通.电信)的中高危漏洞,或者党政机关.重要行业单位.科研院所.重要企事业单位 ...

  7. go 结构体多字段多因素排序

    前言 有时候我们需要处理一份数据,需要多个字段作为条件,联合进行排序. 代码 package main import ( "fmt" "sort" ) // 数 ...

  8. BUUCTF---RSA4

    题目 点击查看代码 N = 33131032421200003002021431224423222240014241042341310444114020300324300210433321420203 ...

  9. 【Java】各种代码块的执行顺序

    静态代码块:用staitc声明,jvm加载类时执行,仅执行一次 构造代码块:类中直接用{}定义,每一次创建对象时执行. 执行顺序优先级:静态块,main(),构造块,构造方法. 构造函数 public ...

  10. 根据二叉树的前序和中序构建树,并按层次输出(C++)vector存树

    L2-006 树的遍历 #include <bits/stdc++.h> #define int long long using namespace std; #define endl ' ...