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)).
思路:这里进一步寻找第k小的元素。(参考http://leetcode.com/2011/01/find-k-th-smallest-element-in-union-of.html)
每一步中取i+j=k-1,如果,Bj_1 <= Ai && Ai <= Bj或者Ai_1 <= Bj && Bj <= Ai,那么,Ai或者Bj为所求。如果A和B不满足这个条件,那么,可以去掉不可能的一般的元素,然后再剩下的元素中找。
int findKthSmallest(int A[], int m, int B[], int n, int k) { assert(m >= 0); assert(n >= 0); assert(k > 0); assert(k <= m+n); int i = (int)((double)m / (m+n) * (k-1)); int j = (k-1) - i; assert(i >= 0); assert(j >= 0); assert(i <= m); assert(j <= n); int Ai_1 = ((i == 0) ? INT_MIN : A[i-1]); int Bj_1 = ((j == 0) ? INT_MIN : B[j-1]); int Ai = ((i == m) ? INT_MAX : A[i]); int Bj = ((j == n) ? INT_MAX : B[j]); if (Bj_1 <= Ai && Ai <= Bj) // 反面是Ai > Bj或者Ai<Bj_1 return Ai; else if (Ai_1 <= Bj && Bj <= Ai)// 剩下的反面是Ai > Bj且Ai_1 > Bj 或者Ai<Bj_1且Bj > Ai return Bj; assert((Ai >=Bj && Ai_1 >= Bj) || (Ai <=Bj && Ai <=Bj_1)); if (Ai <=Bj) //Ai<Bj_1和Bj,那么满足条件的i应该右移(Ai增大),j左移(Bj_1减小) return findKthSmallest(A+i+1, m-i-1, B, j, k-i-1); else return findKthSmallest(A, i, B+j+1, n-j-1, k-j-1); }
double findMid(int A[], int m, int B[], int n){ int a=m+n; if(a%2!=0) return findKthSmallest(A, m, B ,n,a/2+1); else return (findKthSmallest(A, m, B ,n,a/2)+ findKthSmallest(A, m, B ,n,a/2+1))/2.0; }
相关推荐
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 ...