LeetCode刷题:4. Median of Two Sorted Arrays
2018-02-27 23:54
381 查看
题目: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
思路:
暴力排序!
代码:/*
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
*/
#include <vector>
using namespace std;
// 定义排序比较函数
int comp_my(const void* a, const void* b)
{
return *(int*)a - *(int*)b;
}
class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
// new一个新的整数向量a,将nums1和nums2都放进a中
int *a = new int[nums1.size() + nums2.size()];
for (int i = 0; i < nums1.size(); i++)
a[i] = nums1[i];
for (int i = 0; i < nums2.size(); i++)
a[nums1.size() + i] = nums2[i];
// 对a使用标准库算法qsort()排序
qsort(a, nums1.size() + nums2.size(), sizeof(int), comp_my);
//计算medium值
int nums_tol = nums1.size() + nums2.size();
double median = (nums_tol % 2) ? a[nums_tol / 2] : (a[nums_tol / 2] + a[nums_tol / 2 - 1]) / 2.0;
return median;
}
};
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
思路:
暴力排序!
代码:/*
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
*/
#include <vector>
using namespace std;
// 定义排序比较函数
int comp_my(const void* a, const void* b)
{
return *(int*)a - *(int*)b;
}
class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
// new一个新的整数向量a,将nums1和nums2都放进a中
int *a = new int[nums1.size() + nums2.size()];
for (int i = 0; i < nums1.size(); i++)
a[i] = nums1[i];
for (int i = 0; i < nums2.size(); i++)
a[nums1.size() + i] = nums2[i];
// 对a使用标准库算法qsort()排序
qsort(a, nums1.size() + nums2.size(), sizeof(int), comp_my);
//计算medium值
int nums_tol = nums1.size() + nums2.size();
double median = (nums_tol % 2) ? a[nums_tol / 2] : (a[nums_tol / 2] + a[nums_tol / 2 - 1]) / 2.0;
return median;
}
};
相关文章推荐
- [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
- Leetcode4. Median of Two Sorted Arrays
- 4. Median of Two Sorted Arrays [LeetCode]
- leetcode4. 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题解Java | 4. Median of Two Sorted Arrays
- LeetCode4. 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(Java实现)
- LeetCode 4. Median of Two Sorted Arrays(c++)
- [LeetCode]4. Median of Two Sorted Arrays