LeetCode-004-寻找两个正序数组的中位数
寻找两个正序数组的中位数
题目描述:给定两个大小分别为 m 和 n 的正序(从小到大)数组 nums1 和 nums2。请你找出并返回这两个正序数组的 中位数 。
示例说明请见LeetCode官网。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/median-of-two-sorted-arrays/
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解法一:有序数组合并
将2个数组按顺序合并到一个大数组里面,2个数组都只会遍历一次。然后在大数组中获取中位数。
解法二:待完成
思考怎么在 时间复杂度为
O(log (m+n))下完成?
public class Solution {
public static double findMedianSortedArrays(int[] nums1, int[] nums2) {
int[] allNums = new int[nums1.length + nums2.length];
int i = 0, j = 0, x = 0;
int num1 = Integer.MAX_VALUE, num2 = Integer.MAX_VALUE;
for (; i < nums1.length || j < nums2.length; ) {
if (i < nums1.length) {
num1 = nums1[i];
}
if (j < nums2.length) {
num2 = nums2[j];
}
if (num1 < num2) {
allNums[x] = num1;
i++;
} else {
allNums[x] = num2;
j++;
}
x++;
num1 = Integer.MAX_VALUE;
num2 = Integer.MAX_VALUE;
}
int count = nums1.length + nums2.length;
if (count % 2 == 1) {
return allNums[count / 2];
} else {
return ((double) (allNums[count / 2 - 1] + allNums[count / 2])) / 2;
}
}
public static void main(String[] args) {
int[] nums1 = new int[]{1, 2};
int[] nums2 = new int[]{3, 4};
System.out.println(findMedianSortedArrays(nums1, nums2));
}
}
LeetCode-004-寻找两个正序数组的中位数的更多相关文章
- 微软面试题: LeetCode 4. 寻找两个正序数组的中位数 hard 出现次数:3
题目描述: 给定两个大小为 m 和 n 的正序(从小到大)数组 nums1 和 nums2.请你找出并返回这两个正序数组的中位数. 进阶:你能设计一个时间复杂度为 O(log (m+n)) 的算法解决 ...
- [LeetCode]4.寻找两个正序数组的中位数(Java)
原题地址: median-of-two-sorted-arrays 题目描述: 示例 1: 输入:nums1 = [1,3], nums2 = [2] 输出:2.00000 解释:合并数组 = [1, ...
- leetcode-4. 寻找两个正序数组的中位数
leetcode-4. 寻找两个正序数组的中位数. 给定两个大小为 m 和 n 的正序(从小到大)数组 nums1 和 nums2. 请你找出这两个正序数组的中位数,并且要求算法的时间复杂度为 O(l ...
- leetcode 刷题(数组篇)4题 寻找两个正序数组的中位数(二分查找)
题目描述 给定两个大小分别为 m 和 n 的正序(从小到大)数组 nums1 和 nums2.请你找出并返回这两个正序数组的 中位数 . 示例 1: 输入:nums1 = [1,3], nums2 = ...
- Leetcode随缘刷题之寻找两个正序数组的中位数
我一上来没读清题,想着这题这么简单,直接就上手写了: package leetcode.day_12_05; import java.util.ArrayList; import java.util. ...
- leetcode 4. Median of Two Sorted Arrays 寻找两个正序数组的中位数(困难)
一.题目大意 标签: 查找 https://leetcode.cn/problems/median-of-two-sorted-arrays 给定两个大小分别为 m 和 n 的正序(从小到大)数组 n ...
- 【LeetCode】4. Median of Two Sorted Arrays 寻找两个正序数组的中位数
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:数组,中位数,题解,leetcode, 力扣,python ...
- Leetcode4. 寻找两个正序数组的中位数
> 简洁易懂讲清原理,讲不清你来打我~ 输入两个递增数组,输出中位数之前.设置option $ch = cur ...
- UTF-8编码规则(摘自JDK官方文档)
- Apache Http Server与Tomcat6 的负载均衡(二)
一般来说,实现Apache与Tomcat6的负载均衡有两种方式,一种是使用mod_jk,另一种是使用mod_proxy模块.本文只讨论mod_jk方式. 无论使用哪种方式,一般都要经过以下这几个步骤( ...
- LVS+Keepalived 高可用群集部署
LVS+Keepalived 高可用群集部署 1.LVS+Keepalived 高可用群集概述 2.LVS+Keepalived高可用群集部署 1.LVS+Keepalived 高可用群集概述: LV ...
- 基于双TMS320C6678 + XC7K420T的6U CPCI Express高速数据处理平台
1.板卡概述 板卡由我公司自主研发,基于6UCPCI架构,处理板包含双片TI DSP TMS320C6678芯片:一片Xilinx公司FPGA XC7K420T-1FFG1156 芯片:六个千兆网口( ...
- Linux-CPU优化之上下文切换
为什么大量进程(通常进程数大于CPU个数)的运行会导致CPU长时间处于等待时间而导致平均负债率过高呢?没有使用CPU且无不可中断的进程,这就涉及到了上下文切换. 巧妙地利用了时间片轮转的方式, CPU ...
- Spring boot 项目中put提交Date数据时出现type=Bad Request, status=400状态码
1.问题原因 经过测试发现,当客户端页面提交日期为空时会出现以下异常,如果提交日期不为空则不会出现上述问题.出现这种错误的原因是没有对代码中的Date型参数进行格式化,接收为null的日期类型参数时, ...