我一上来没读清题,想着这题这么简单,直接就上手写了:

package leetcode.day_12_05;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List; /**
* 给定两个大小分别为 m 和 n 的正序(从小到大)数组nums1 和nums2。请你找出并返回这两个正序数组的 中位数 。
* <p>
* 算法的时间复杂度应该为 O(log (m+n)) 。
* 示例 1:
* <p>
* 输入:nums1 = [1,3], nums2 = [2]
* 输出:2.00000
* 解释:合并数组 = [1,2,3] ,中位数 2
* 示例 2:
* <p>
* 输入:nums1 = [1,2], nums2 = [3,4]
* 输出:2.50000
* 解释:合并数组 = [1,2,3,4] ,中位数 (2 + 3) / 2 = 2.5
* 示例 3:
* <p>
* 输入:nums1 = [0,0], nums2 = [0,0]
* 输出:0.00000
* 示例 4:
* <p>
* 输入:nums1 = [], nums2 = [1]
* 输出:1.00000
* 示例 5:
* <p>
* 输入:nums1 = [2], nums2 = []
* 输出:2.00000
*
* @author soberw
* @Classname FindMedianSortedArrays0004
* @Description
* @Date 2021-12-05 21:54
*/
public class FindMedianSortedArrays0004 {
/**
* @param array int数组
* @description: 将int数组转换为List<Integer>
* @return: List<Integer>
* @author: soberw
* @time: 2021/12/5 22:10
*/ public List<Integer> listOf(int[] array) {
List<Integer> result = new ArrayList<>();
for (int i : array) {
result.add(i);
}
return result;
} public double findMedianSortedArrays(int[] nums1, int[] nums2) {
double middle = 0.0;
List<Integer> l1 = listOf(nums1);
List<Integer> l2 = listOf(nums2);
l2.addAll(l1);
l2.sort(Comparator.comparingInt(a -> a));
if (l2.size() % 2 == 0) {
middle = ((double) l2.get(l2.size() / 2) + (double) l2.get(l2.size() / 2 - 1)) / 2;
} else {
middle = (double) l2.get(l2.size() / 2);
}
return middle;
}
}


虽然是通过了,但是在我又看一遍题目之后,才发现难点所在。
本题要求时间复杂度控制在O(log(m+n)),而我使用了排序算法就肯定不行了。
因为Arrays.sort时间复杂度是O(nlogn),光是这就超了。
于是我又想出第二种法:

package leetcode.day_12_05;

/**
* @author soberw
* @Classname FindMedianSortedArrays0004
* @Description
* @Date 2021-12-05 21:54
*/
public class FindMedianSortedArrays0004_02 { public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int l1 = nums1.length;
int l2 = nums2.length;
//存放合并后的
int[] num = new int[l1 + l2];
// 第一个数组索引位置
int i = 0;
// 第二个数组索引位置
int j = 0;
// 定义的num数组索引位置
int n = 0; //在数组范围内添加元素致新数组
while (i < l1 && j < l2) {
if (nums1[i] < nums2[j]) {
num[n] = nums1[i++];
} else {
num[n] = nums2[j++];
}
n++;
}
// 若其中一个数组已经添加完毕,接下来只需要添加另一个即可
if (i < l1) {
for (int a = i; a < l1; a++) {
num[n++] = nums1[a];
}
} else {
for (int a = j; a < l2; a++) {
num[n++] = nums2[a];
}
} //返回中值
if (num.length % 2 == 0) {
return ((double) num[num.length / 2] + (double) num[num.length / 2 - 1]) / 2;
} else {
return num[num.length / 2];
}
} public static void main(String[] args) {
int[] nums1 = {1, 3};
int[] nums2 = {2, 5};
System.out.println(new FindMedianSortedArrays0004_02().findMedianSortedArrays(nums1, nums2));
}
}


相比于第一种虽然是快了不少,但是时间复杂度还是不够,这次的是O(m+n)。目前这能在先这样了。
欢迎评论区大神指点。

Leetcode随缘刷题之寻找两个正序数组的中位数的更多相关文章

  1. leetcode-4. 寻找两个正序数组的中位数

    leetcode-4. 寻找两个正序数组的中位数. 给定两个大小为 m 和 n 的正序(从小到大)数组 nums1 和 nums2. 请你找出这两个正序数组的中位数,并且要求算法的时间复杂度为 O(l ...

  2. leetcode 刷题(数组篇)4题 寻找两个正序数组的中位数(二分查找)

    题目描述 给定两个大小分别为 m 和 n 的正序(从小到大)数组 nums1 和 nums2.请你找出并返回这两个正序数组的 中位数 . 示例 1: 输入:nums1 = [1,3], nums2 = ...

  3. leetcode 4. Median of Two Sorted Arrays 寻找两个正序数组的中位数(困难)

    一.题目大意 标签: 查找 https://leetcode.cn/problems/median-of-two-sorted-arrays 给定两个大小分别为 m 和 n 的正序(从小到大)数组 n ...

  4. 微软面试题: LeetCode 4. 寻找两个正序数组的中位数 hard 出现次数:3

    题目描述: 给定两个大小为 m 和 n 的正序(从小到大)数组 nums1 和 nums2.请你找出并返回这两个正序数组的中位数. 进阶:你能设计一个时间复杂度为 O(log (m+n)) 的算法解决 ...

  5. 【LeetCode】4. Median of Two Sorted Arrays 寻找两个正序数组的中位数

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:数组,中位数,题解,leetcode, 力扣,python ...

  6. Leetcode4. 寻找两个正序数组的中位数

    > 简洁易懂讲清原理,讲不清你来打我~ 输入两个递增数组,输出中位数![在这里插入图片描述](https://img-blog.csdnimg.cn/25550994642144228e9862 ...

  7. [LeetCode]4.寻找两个正序数组的中位数(Java)

    原题地址: median-of-two-sorted-arrays 题目描述: 示例 1: 输入:nums1 = [1,3], nums2 = [2] 输出:2.00000 解释:合并数组 = [1, ...

  8. LeetCode-004-寻找两个正序数组的中位数

    寻找两个正序数组的中位数 题目描述:给定两个大小分别为 m 和 n 的正序(从小到大)数组 nums1 和 nums2.请你找出并返回这两个正序数组的 中位数 . 示例说明请见LeetCode官网. ...

  9. 寻找两个已序数组中的第k大元素

    寻找两个已序数组中的第k大元素 1.问题描述 给定两个数组与,其大小分别为.,假定它们都是已按照增序排序的数组,我们用尽可能快的方法去求两个数组合并后第大的元素,其中,.例如,对于数组,.我们记第大的 ...

随机推荐

  1. Swoole 协程使用示例及协程优先级

    示例一: Co::set(['hook_flags'=> SWOOLE_HOOK_ALL]); Co\run(function () { go(function() { var_dump(fil ...

  2. Linux系统使用crt登录之后如何显示横幅消息

    打开  /etc/motd 编辑内容即可 效果如下

  3. Word2010制作简单个人简历

    原文链接:https://www.toutiao.com/i6489366535050625550/ 以学习使用按钮属性为主,具体样式可以根据个人设置 选择"页面布局"选项卡,&q ...

  4. vue 前进刷新后退不刷新

    这边是router-view部门的写法: <keep-alive> <router-view v-if="$route.meta.keepAlive"/> ...

  5. Vulnhub系列:Os-hackNos

    0x01环境搭建 靶机链接: https://www.vulnhub.com/entry/hacknos-os-hacknos,401/发布日期: 2019.11.27靶机描述: 描述 难度:容易中级 ...

  6. vue3+vant h5: Rem 移动端布局适配之postcss-pxtorem和lib-flexible

    如果不引入插件的话:ui稿的px转化成rem需自己计算 根据设计稿我们需要自己计算元素的rem(假如我们将html根元素font-size设置为41.4px): 那么1rem=41.4px; ui稿上 ...

  7. 推荐一个最懂程序员的google插件

    0.前言 很多人应该也和我一样,使用google浏览器时,它的主页是真不咋地,太单调了,用起来贼不爽,想整它很久了 一打开就是上面的样子,让我看起来真心真心不爽 当然:为了这个不关技术的瞎犊子事情,曾 ...

  8. Keil MDK STM32系列(三) 基于标准外设库SPL的STM32F407开发

    Keil MDK STM32系列 Keil MDK STM32系列(一) 基于标准外设库SPL的STM32F103开发 Keil MDK STM32系列(二) 基于标准外设库SPL的STM32F401 ...

  9. Flowable实战(六)集成JPA

      上文提到,Flowable所有的表单数据都保存在一张表(act_hi_varinst)中,随着时间的推移,表中数据越来越多,再加上数据没有结构优化,查询使用效率会越来越低.   在Flowable ...

  10. host解析

    首先了解一下什么是hosts文件: hosts是一个没有扩展名的系统文件,可以用记事本等文本编辑工具打开,起作用就是将一些常用的"网址域名"与其对应的"IP地址" ...