leetcode 4 Median of Two Sorted Arrays(有坑待填)
来源:互联网 发布:仁霸玻璃优化软件 编辑:程序博客网 时间:2024/05/18 02:46
题目:
https://leetcode.com/problems/median-of-two-sorted-arrays/description/
题意:
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)).
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
给定两个有序数组,求两个数组合并的中位数
思路:
题目要求用
代码:
class Solution {public: double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) { vector<int> ans; ans.reserve(nums1.size() + nums2.size()); vector<int>::size_type i = 0, j = 0; while(i < nums1.size() && j < nums2.size()) { if(nums1[i] < nums2[j]) ans.emplace_back(nums1[i++]); else ans.emplace_back(nums2[j++]); } while(i < nums1.size()) ans.emplace_back(nums1[i++]); while(j < nums2.size()) ans.emplace_back(nums2[j++]); if(ans.size() % 2 != 0) return ans[ans.size()/2]; else return (ans[ans.size()/2-1] + ans[ans.size()/2]) / 2.0; }};
阅读全文
0 0
- Leetcode 4 Median of Two Sorted Arrays
- LeetCode 4 - Median of Two Sorted Arrays
- Leetcode 4 Median of Two Sorted Arrays
- Leetcode 4 Median of Two Sorted Arrays
- [leetcode 4] Median of Two Sorted Arrays
- LeetCode 4:《Median of Two Sorted Arrays》
- [Leetcode] 4 - Median of Two Sorted Arrays
- leetcode|4|Median of Two Sorted Arrays
- [Leetcode]4Median of Two Sorted Arrays
- leetcode 4 Median of Two Sorted Arrays
- LeetCode #4 Median of Two Sorted Arrays
- LeetCode-4-Median of Two Sorted Arrays
- LeetCode 4 Median of Two Sorted Arrays
- leetcode 4 Median of Two Sorted Arrays
- LeetCode 4 Median of Two Sorted Arrays
- Leetcode[4] Median of Two Sorted Arrays
- LeetCode 4 - Median of Two Sorted Arrays
- leetcode 4 -- Median of Two Sorted Arrays
- Maven学习总结系列七:聚合与继承
- 总结获取原生JS(javascript)的父节点、子节点、兄弟节点
- 231. Power of Two
- 动态代理完成字符集编码过滤器
- 每天一道LeetCode-----逆序链表
- leetcode 4 Median of Two Sorted Arrays(有坑待填)
- Spring Boot---(2)Spring Boot 配置文件详解
- Python3+OpenCV学习笔记(一):图像加载、显示和保存
- opencv3/C++图像边缘提取
- C++ Primer 12章习题
- Hibernate框架笔记整理--二
- androidstudio打包失败
- Leetcode——第136题——Single Number
- Python format 格式化函数 转载菜鸟教程