4. Median of Two Sorted Arrays
来源:互联网 发布:五星体育网络源 编辑:程序博客网 时间:2024/06/05 11:00
题目
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
思路:
本题,没有时间复杂度的要求,那么使用STL中函数,时间复杂度为(M+n)log(M+N),时间复杂度log(M+N)的解法暂时还没想到一个易于理解的,故之后再补上。。。
代码
class Solution {public: double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) { nums2.insert(nums2.end(),nums1.begin(),nums1.end()); int size = nums2.size(); sort(nums2.begin(),nums2.end()); if(size%2==0) return (nums2[size/2-1]+nums2[size/2])/2.0; else return nums2[size/2]; }};
阅读全文
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
- 4.Median of Two Sorted Arrays
- [LeetCode]4.Median of Two Sorted Arrays
- 4. Median of Two Sorted Arrays
- 4.Median of Two Sorted Arrays
- 4. Median of Two Sorted Arrays
- 4. Median of Two Sorted Arrays
- 【leetcode】4. Median of Two Sorted Arrays
- Leetcode-4.Median of Two Sorted Arrays
- 4. Median of Two Sorted Arrays
- 4. Median of Two Sorted Arrays
- LeetCode-4.Median of Two Sorted Arrays
- leetcoode 4. Median of Two Sorted Arrays
- Leetcode 4. Median of Two Sorted Arrays
- leetcode 4. Median of Two Sorted Arrays
- TextView使用drawable属性
- Fountains CodeForces
- 抽象类与接口的区别
- 423. Reconstruct Original Digits from English
- 一个简单的SpringMVC框架web程序
- 4. Median of Two Sorted Arrays
- “System.StackOverflowException”类型的未经处理的异常在 App_Code.qcryqgiv.dll 中发生
- javascript中的define用法
- 447. Number of Boomerangs
- yolo解
- jenkins使用Gerrit,错误checkout分支版本的解决方法
- Paragon NTFS for Mac 15 v15.0中文版
- springMVC数据传递案例------利用form表单提交数据
- truncate与delete的区别