4. Median of Two Sorted Arrays
of two sorted Arrays Median
2023-09-11 14:22:48 时间
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)).
You may assume nums1 and nums2 cannot be both empty.
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
my code:
class Solution { public: double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) { vector<int> v; int num = 0; for (int i = 0; i < nums1.size(); ++i) { v.push_back(nums1[i]); num++; } for (int i = 0; i < nums2.size(); ++i) { v.push_back(nums2[i]); num++; } sort(v.begin(), v.end()); if (num % 2 == 0) { int mid = num / 2; double ans = 1.0*(v[mid] + v[mid-1])/2; return ans; } else { int mid = num / 2; double ans = 1.0*v[mid]; return ans; } } };
Runtime: 44 ms, faster than 51.19% of C++ online submissions for Median of Two Sorted Arrays.
相关文章
- Leetcode: Minimum Number of Arrows to Burst Balloons
- Leetcode: Sum of Two Integers && Summary: Bit Manipulation
- Leetcode: Power of Two
- XVII Open Cup named after E.V. Pankratiev. GP of SPb
- English Voice of <<The show >>
- 新版本gcc(gcc10.x)编译Linux内核,错误:multiple definition of `yylloc‘
- 1104 Sum of Number Segments (20 分)【难度: 一般 / 模拟 找规律】
- Bag-of-words模型入门介绍文章
- with class of same name
- [LeetCode]Sum of Two Integers
- python3 “POST data should be bytes or an iterable of bytes...”的解决方法
- POJ 3668 Game of Lines (暴力,判重)
- startActivity时报错Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag
- DISCOMAN: Dataset of Indoor SCenes for Odometry, Mapping and Navigation
- FRP represents an intersection of two programming paradigms.
- 【LeetCode】160. Intersection of Two Linked Lists
- JPA:identifier of an instance of was altered from
- [LeetCode] 1331. Rank Transform of an Array 数组序号转换
- [LeetCode] 1317. Convert Integer to the Sum of Two No-Zero Integers 将整数转换为两个无零整数的和
- [LeetCode] 1074. Number of Submatrices That Sum to Target 元素和为目标值的子矩阵数量
- [LeetCode] 421. Maximum XOR of Two Numbers in an Array 数组中异或值最大的两个数字
- [LeetCode] 350. Intersection of Two Arrays II 两个数组相交之二
- [LeetCode] 4. Median of Two Sorted Arrays 两个有序数组的中位数
- [LeetCode] 160. Intersection of Two Linked Lists 求两个链表的交点
- Leetcode——4. Median of Two Sorted Arrays