【LeetCode】LeetCode——第4题:Median of Two Sorted Arrays
来源:互联网 发布:h3c路由器封端口 编辑:程序博客网 时间:2024/05/17 08:39
4. Median of Two Sorted Arrays
My SubmissionsTotal Accepted: 91193 Total Submissions: 491566 Difficulty: Hard
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)).
Subscribe to see which companies asked this question
题目的大概意思是:给定长度分别为n、m的两个有序数组nums1、nums2,在O(log(n+m))的时间复杂度下求出这n+m个数的中位数。
这个题难度等级:难。
最开始想到的就是将这两组数放到一个数组里面再排序,然后求该数组的中位数。
代码如下:
class Solution {public:double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2){vector<int> nums;if (nums1.size() > nums2.size()){nums = nums1;for (unsigned int i = 0; i < nums2.size(); ++i){nums.push_back(nums2[i]);}}else{nums = nums2;for (unsigned int i = 0; i < nums1.size(); ++i){nums.push_back(nums1[i]);}}sort(nums.begin(), nums.end());if (nums.size() & true){return nums[nums.size() / 2];}else{return (nums[nums.size() / 2] + nums[(nums.size() / 2) - 1]) / 2.0;}}};当然,这种办法能够AC掉该题,Runtime: 56 ms。但是这并不是很好的一种解法,因为没有充分的利用两个数组有序的性质。
下面给出网上看到的一种解法:
class Solution { int myMin(int x, int y) { return x < y ? x : y; } int findNumber(vector<int>& nums1, vector<int>& nums2, int findKth) { int length_1 = (int) nums1.size(); int length_2 = (int) nums2.size(); if (length_1 == 0) return nums2[ findKth - 1 ]; if (length_2 == 0) return nums1[ findKth - 1 ]; int left_1 = 0, left_2 = 0; int mid_1 = 0, mid_2 = 0; int k; do { if (length_1 - left_1 == 0) return nums2[ findKth - 1 ]; if (length_2 - left_2 == 0) return nums1[ findKth - 1 ]; if (findKth == 1) return myMin(nums1[left_1], nums2[left_2]); k = findKth / 2; if (length_2 - left_2 >= length_1 - left_1) { if (length_1 - left_1 < k) k = length_1 - left_1; mid_1 = left_1 + (k - 1); mid_2 = left_2 + (findKth - k - 1); } else { if (length_2 - left_2 < k) k = length_2 - left_2; mid_2 = left_2 + (k - 1); mid_1 = left_1 + (findKth - k - 1); } if (nums1[mid_1] < nums2[mid_2]) { findKth -= mid_1 - left_1 + 1; left_1 = mid_1 + 1; } else { findKth -= mid_2 - left_2 + 1; left_2 = mid_2 + 1; } } while(true); }public: double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) { int totNumber = (int) nums1.size() + (int) nums2.size(); int findKth = totNumber / 2; if (totNumber % 2 == 1) return findNumber(nums1, nums2, findKth + 1); else return (findNumber(nums1, nums2, findKth) + findNumber(nums1, nums2, findKth + 1)) / 2.0; }};这种解法利用了数组有序的性质和二分法,因此比较快速AC掉该题,Runtime: 40 ms。这种方法的大致原理是:
0 0
- 【LeetCode】LeetCode——第4题:Median of Two Sorted Arrays
- leetcode 第4题 Median of Two Sorted Arrays
- LeetCode — Median of Two Sorted Arrays
- LeetCode—Median of Two Sorted Arrays
- leetcode—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
- leetcode|4|Median of Two Sorted Arrays
- [Leetcode]4Median of Two Sorted Arrays
- 南京理工大学第八届程序设计大赛count_prime
- 卷积神经网络-图像卷积的概念
- Python virtualenv安装库报错SSL: CERTIFICATE_VERIFY_FAILED
- POJ2115 C Looooops(拓展欧几里德)
- CI中的路由规则
- 【LeetCode】LeetCode——第4题:Median of Two Sorted Arrays
- HelloChart--PreviewChartView(预览统计图)
- DataPickerView最简单用法。。直接拿过去用就行
- iOS NSString 截取,objectAtIndex,rangeOfString,stringWithContentsOfFile,NSEnumerator
- cocos2dx 安卓makefile Android.mk,动态引用cpp地址
- IOS路线图
- 把makefile 需要编译的源码编译全部删除或者重命名会怎么样?
- Ext中关于Ext.QuickTips.init()的使用
- AVR开发笔记