LeetCode - 4. 寻找两个正序数组的中位数
题目来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/median-of-two-sorted-arrays
题目描述
给定两个大小为 m 和 n 的正序(从小到大)数组 nums1
和 nums2
。请你找出并返回这两个正序数组的中位数。
输入样例
示例 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
示例 3:
输入:nums1 = [0,0], nums2 = [0,0]
输出:0.00000
示例 4:
输入:nums1 = [], nums2 = [1]
输出:1.00000
示例 5:
输入:nums1 = [2], nums2 = []
输出:2.00000
解题思路
将两个数组合并成一个数组,去中间的两个数或者一个数。
解题代码
//#include <vector>
//#include <iostream>using namespace std;class Solution {
public:double findMedianSortedArrays(vector<int> &nums1, vector<int> &nums2) {int n1 = nums1.size();int n2 = nums2.size();int n = n1 + n2;int mergeVector[n];int index = 0;int index1 = 0;int index2 = 0;while (index <= n/2 && index1 < n1 && index2 < n2) {if (nums1[index1] < nums2[index2]) {mergeVector[index] = nums1[index1];index1++;} else {mergeVector[index] = nums2[index2];index2++;}index++;}while (index <= n/2 && index1 < n1) {mergeVector[index] = nums1[index1];index1++;index++;}while (index <= n/2 && index2 < n2) {mergeVector[index] = nums2[index2];index2++;index++;}if (n%2 ==0){return ((double) (mergeVector[n/2]+mergeVector[n/2-1]))/2;} else{return mergeVector[n/2];}}
};
//int main() {
//
// Solution solution;
// vector<int> nums1;
// vector<int> nums2;
// nums2.push_back(1);
// nums2.push_back(2);
// nums1.push_back(3);
//
// double res = solution.findMedianSortedArrays(nums1,nums2);
// cout << res;
// return 0;
//
//}
解题结果
LeetCode - 4. 寻找两个正序数组的中位数相关推荐
- [二分搜索|快速选择] leetcode 4 寻找两个正序数组的中位数
[二分搜索|快速选择] leetcode 4 寻找两个正序数组的中位数 1.题目 题目链接 给定两个大小为 m 和 n 的正序(从小到大)数组 nums1 和 nums2.请你找出并返回这两个正序数组 ...
- LeetCode 4 寻找两个正序数组的中位数
https://leetcode-cn.com/problems/median-of-two-sorted-arra 解决方案 Go 版本 func findMedianSortedArrays(nu ...
- LeetCode 04寻找两个正序数组的中位数(困难)二分法
题目描述: 呕心沥血的一个题解,点赞关注收藏,一键三联,一起加入我们打卡! 题目描述: 给定两个大小为 m 和 n 的正序(从小到大)数组 nums1 和 nums2. 请你找出这两个正序数组的中位数 ...
- 【LeetCode】4.寻找两个正序数组的中位数
4.寻找两个正序数组的中位数 一.问题描述 给定两个大小分别为 m 和 n 的正序(从小到大)数组 nums1 和 nums2.请你找出并返回这两个正序数组的 中位数 . 二.问题简化 所谓中位数,就 ...
- 【LeetCode】【HOT】4. 寻找两个正序数组的中位数(二分查找)
[LeetCode][HOT]4. 寻找两个正序数组的中位数 文章目录 [LeetCode][HOT]4. 寻找两个正序数组的中位数 package hot;public class Solution ...
- 2022-6-13 全O(1)的数据结构,两数相加,无重复字符的最长子串,寻找两个正序数组的中位数,盛最多水的容器,......
1. 全 O(1) 的数据结构 Design a data structure to store the strings' count with the ability to return the s ...
- 寻找两个正序数组的中位数
寻找两个正序数组的中位数 时隔许久,我又回来了. 题目:给定两个大小分别为m和n的正序(从小到大)数组nums1和nums2.请你找出并返回这两个正序数组的中位数. 要求算法的时间复杂度为O(log( ...
- java打乱一组正序数字,Leetcode︱4.Median of Two Sorted Arrays寻找两个正序数组的中位数.java...
题目 给定两个大小分别为 m 和 n 的正序(从小到大)数组 nums1 和 nums2.请你找出并返回这两个正序数组的 中位数 . 示例 : 输入:nums1 = [1,3], nums2 = [2 ...
- Leetcode每日必刷题库第4题,如何寻找两个正序数组的中位数?
题目: 给定两个大小为 m 和 n 的正序(从小到大)数组 nums1 和 nums2. 请你找出这两个正序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n)). 你可以假设 nums ...
最新文章
- Unity Shader 阴影
- digit函数(信息学奥赛一本通-T1164)
- Android之使用ViewPager实现图片展示(最简单的)
- 阿里云-流量控制策略
- extjs TabPanel 监听激活事件
- ie6中兼容性问题总结
- 文件打印服务器解决方案
- 运用现代信息技术 推进环评大数据建设
- 使用python调用DLL文件
- 生活中的逻辑谬误05.特例谬误
- 计算机 蓝牙鼠标卡顿,蓝牙鼠标卡顿不流畅怎么办 最新win10无线鼠标卡顿解决办法...
- AutoSar之微控制器抽象层MCAL
- java 多字段分组_在Java 8中按多个字段名称分组
- 一款自动太阳能照明灯电路设计
- Java 银联支付官网demo测试及项目整合代码
- mysql 搜索条件包含与被包含
- 体验极速——在旭日X3派上使用双频1300M USB无线网卡
- 字体随浏览器缩放变化
- PASCAL VOC 2012数据集及其增强版介绍
- 明白的糊涂账 各家单反测光系统全解析
热门文章
- python help()函数(查看特定模块、关键词、函数等用法)
- Intel Realsense D435报错:RuntimeError: Acquire failed!
- Intel Realsense D435 摄像头插入电脑无法监测(识别)的可能原因及解决方案 USB SCP overflow
- PyQ4标准输入框——QInputDialog(一)
- 浙江农林大学第二十一届程序设计竞赛校选拔赛(同步)
- MySQL show binlog events命令查看binlog日志内容
- 训练集 测试集 验证集_Python机器学习实战:划分训练集和检验集
- HTTP状态码:204 No Content(总结HTTP状态码)
- Java后端架构开荒实战(一)——基础设施
- spring mvc @ModelAttribute 基本类型 自定义对象解析流程