http://oj.leetcode.com/problems/median-of-two-sorted-arrays/
There are two sorted arrays A and B 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)).
思路:
(1)时间复杂度O(log(m+n)),联想到递归法;
(2)问题形式化为:查找两个数组的第K大小的数findKth(A, m, B, n, topK);
(3)边界条件:凡是出现了加、减的地方都要考虑数组的上下界;
(4)两个数组大小不一样,计算机只解决一次就行,不用分开考虑。在函数头部加上调整数组顺序即可。
class Solution {
public:
double findKth(int A[], int m, int B[], int n, int topK) {
// always assume that m is equal or less than n
if (m > n) {
return findKth(B, n, A, m, topK);
}
if(m == 0) {
return B[topK - 1];
}
if (topK == 1) {
return min(A[0], B[0]);
}
// divide topK into two parts
int pa = min(topK/2, m);
int pb = topK - pa;
if (A[pa-1] < B[pb-1]){
return findKth(A+pa, m-pa, B, n, topK - pa);
} else if (A[pa-1] > B[pb-1]) {
return findKth(A, m, B+pb, n-pb, topK - pb);
} else {
return A[pa-1];
}
}
double findMedianSortedArrays(int A[], int m, int B[], int n) {
if ((m+n)%2 == 1) {
return findKth(A, m, B, n, (m+n)/2 + 1) * 1.0;
} else {
return (findKth(A, m, B, n, (m+n)/2) +
findKth(A, m, B, n, (m+n)/2 + 1)) * 1.0 / 2;
}
}
};
分享到:
相关推荐
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)). Java AC版本
java java_leetcode题解之Median of Two Sorted Arrays.java
在JavaScript编程领域,"Median of Two Sorted Arrays"(两个排序数组的中位数)是一个经典的算法问题。这个问题的目的是在给定两个已排序的数组中找到它们合并后的中位数,但无需实际合并这两个数组。这是一个典型...
leetcode 无法登录两个有序数组的中位数 问题 有两个大小分别为 m 和 n 的排序数组 nums1 和 nums2。求两个排序数组的中位数。 整体运行时间复杂度应该是 O(log (m+n))。 您可以假设 nums1 和 nums2 ...
4. Median of Two Sorted Arrays 7. Reverse Integer 9. Palindrome Number 11. Container With Most Water 13. Roman to Integer 15. 3Sum 16. 3Sum Closest 17. Letter Combinations of a Phone Number 18. 4Sum ...
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...
7 Median of Two Sorted Arrays 33 8 Kth Largest Element in an Array 35 9 Wildcard Matching 37 10 Regular Expression Matching in Java 39 11 Merge Intervals 43 12 Insert Interval 45 13 Two Sum 47 14 Two ...
Two Sum 002. Add Two Numbers 003. Longest Substring Without Repeating Characters4. Median of Two Sorted Arrays 004. Median of Two Sorted Arrays 005. Longest Palindromic Substring 006. ZigZag ...
Two Sum.cpp │ │ ├── 002 - Add Two Numbers.cpp │ │ ├── 003 - Longest Substring Without Repeating Characters.cpp │ │ ├── 004 - Median of Two Sorted Arrays.cpp │ │ └──...
java入门 java_leetcode题解之004_Median_of_Two_Sorted_Arrays
c语言入门 c语言_leetcode题解04-median-of-two-sorted-arrays.c
js js_leetcode题解之4-median-of-two-sorted-arrays.js
Median of Two Sorted Arrays 5. Longest Palindromic Substring 7. Reverse Integer 9. Palindrome Number 11. Container With Most Water 12. Integer to Roman 13. Roman to Integer 14. Longest Common Prefix ...
Median of Two Sorted Arrays 困难 数学 Longest Palindromic Substring 中等 回文 ZigZag Conversion 中等 矩阵 重要 Reverse Integer 简单 字串 String to Integer (atoi) 中等 字串 麻烦 Palindrome Number 简单 ...
* 两个排序数组的中位数(Median of Two Sorted Arrays):计算两个排序数组的中位数。 * 整数到罗马(Integer to Roman):将整数转换为罗马数字。 * 罗马到整数(Roman to Integer):将罗马数字转换为整数。 4...
#4:Median of Two Sorted Arrays 地图 #1:Two Sum #3:Longest Substring Without Repeating Characters #5:Longest Palindromic Substring 链表 #2:Add Two Numbers 分而治之 #53:Maximum Subarray 队列/集 #3:...
4. **Median of Two Sorted Arrays** (Hard) 这是一道查找两个已排序数组中中位数的题目。要求整体时间复杂度为O(log (m+n))。可以采用二分查找的方法,不断缩小查找范围,找到两个数组合并后中间位置的元素,或者...
Median of Two Sorted Arrays 30.7% Hard 0005 Longest Palindromic Substring 30.1% Medium 0006 ZigZag Conversion 37.5% Medium 0007 Reverse Integer 25.8% Easy 0008 String to Integer (atoi) 15.5% Medium ...
* 经典问题:Evaluate Reverse Polish Notation, Longest Palindromic Substring, 单词分割, 字梯, Median of Two Sorted Arrays, 正则表达式匹配, 合并间隔, 插入间隔, Two Sum, 3Sum, 4Sum, 3Sum Closest, String ...