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. 寻找两个正序数组的中位数
> 简洁易懂讲清原理,讲不清你来打我~ 输入两个递增数组,输出中位数 扩展,请检查您的 PHP 配置。
转载请注明来源:https://www.cnblogs.com/hookjc/ 需要安装libcrytp,在下面的地址下载libmarypt: ftp://mcrypt.hellug.gr/pub/c ...
- bootstrap移动 pc 响应轮播
PC端效果 width100% 移动端 <!DOCTYPE html> <html lang="zh-CN"> <head> <meta ...
- byte溢出栗子
原创:转载需注明原创地址 https://www.cnblogs.com/fanerwei222/p/11634402.html byte溢出测试: byte b1 = (byte) 127; byt ...
- 通过loganalyzer展示数据库中的日志
一.安装mysql # yum -y install mariadb-server # systemctl enable --now mariadb && systemctl stat ...
- Solution -「LOJ #141」回文子串 ||「模板」双向 PAM
\(\mathcal{Description}\) Link. 给定字符串 \(s\),处理 \(q\) 次操作: 在 \(s\) 前添加字符串: 在 \(s\) 后添加字符串: 求 \(s\ ...
- Vue脚手架报错 Component name "Student" should always be multi-word vue/multi-word-component-names
报错信息分析: 新手在第一个次使用脚手架的时候难免会遇到各种各样奇怪的问题,最近在学习Vue的过程中就出现了如下问题 通过阅读报错信息可知: 是我们的组件名有一些问题,(报错信息翻译过来大概就是组件名 ...