算法分析与设计——LeetCode:4. Median of Two Sorted Arrays
来源:互联网 发布:高光谱遥感数据 编辑:程序博客网 时间:2024/06/02 07:19
题目
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) { }};
思路
我考虑过每次取两个向量的中间的数,比较大小,从而每次去掉m/2或n/2个数,但是这样算法复杂度是 O(log m*n)而不是 O(log (m+n)),而下面的方法通过递归,寻找两个向量的数中第n小的数,每次都可以去掉(m+n)/2个数,从而符合题目的要求。
代码
#include <iostream>#include <vector>using namespace std;class Solution {public: double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) { int size1 = nums1.size(), size2 = nums2.size(); int n = (size1+size2)/2; if ((size1+size2)%2 == 0) { return (findNum(nums1, size1, nums2, size2, n)+findNum(nums1, size1, nums2, size2, n+1))/2.0; } else { return findNum(nums1, size1, nums2, size2, n+1); } }private: int findNum(vector<int> nums1, int size1, vector<int> nums2, int size2, int n) {//找到两个向量所有数中第n小的数 if (size1 < size2) { return findNum(nums2, size2, nums1, size1, n);//使下面运算的size1始终大于等于size2 } if (size2 == 0) { return nums1[n-1]; } if (n == 1) { if (nums1[0] < nums2[0]) { return nums1[0]; } else { return nums2[0]; } } int n1 = n/2+n%2; int n2 = n-n1; if (n2 > size2) {//size2有可能太小,下面的nums[n2-1]会过界 n1 += n2-size2; n2 = size2; } if (nums1[n1-1] < nums2[n2-1]) { nums1.erase(nums1.begin(), nums1.begin()+n1);//截取掉前n1个数 return findNum(nums1, size1-n1, nums2, size2, n-n1); } if (nums1[n1-1] > nums2[n2-1]) { nums2.erase(nums2.begin(), nums2.begin()+n2);//姐去掉前n2个数 return findNum(nums1, size1, nums2, size2-n2, n-n2); } if (nums1[n1-1] == nums2[n2-1]) { return nums1[n1-1]; } return 0; }};int main() { vector<int> nums1, nums2; nums2 = {1}; nums1 = {2, 3, 4, 5, 6}; cout << Solution().findMedianSortedArrays(nums1, nums2) << endl; return 0;}
阅读全文
0 0
- 算法分析与设计——LeetCode:4. Median of Two Sorted Arrays
- 算法分析与设计——LeetCode Problem.4 Median of Two Sorted Arrays
- 算法分析与设计丨第三周丨LeetCode(5)——Median of Two Sorted Arrays(Hard)
- LeetCode — Median of Two Sorted Arrays
- LeetCode—Median of Two Sorted Arrays
- leetcode—Median of Two Sorted Arrays
- 【算法分析与设计】【第二周】4.Median of Two Sorted Arrays
- Leetcode——4. Median of Two Sorted Arrays
- 4. Median of Two Sorted Arrays——LeetCode OJ
- Leetcode—4.Median of Two Sorted Arrays
- LeetCode代码分析——4. Median of Two Sorted Arrays
- 算法设计与分析(3)-- Median of Two Sorted Arrays(难度:hard)
- [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
- 发个博客
- js直接插入排序
- nltk.download("stopwords")
- 天池 odps_SQL 常用函数和方法
- LWC 52:686. Repeated String Match
- 算法分析与设计——LeetCode:4. Median of Two Sorted Arrays
- Java多线程笔记三(线程通信wait/notify/notifyAll/sleep/yield/join)
- android getContext() , getApplicationContext() , getBaseContext() and “this”
- 红米手机开发者选项usb调试如何开启
- Java笔记之ArrayList类的使用
- Introducing deep learning and long-short term memory networks
- 大数据可视化-绘制景点热力图
- Java用QRCode的方式生成二维码
- 从零开始写Python爬虫 --- 1.4 正则表达式:re库