Median of Two Sorted ArraysMar 28 '116215 / 31527
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)).
» Solve this problem很久前就见过这题,当时随便一想,以为自己想到了logN的方法。
前几天又看到,打算写一下。发现之前的想法不靠谱啊!!
之前是想:每次找m/2, n/2处。然后比来比去搞。思路错了。
后来参考了:http://blog.csdn.net/zxzxy1988/article/details/8587244
class Solution { public: double findMedianSortedArrays(int A[], int m, int B[], int n) { int total = n + m; if (total % 2 == 0) { return (findKth(A, m, B, n, total/2) + findKth(A, m, B, n, total / 2 + 1)) / 2; } else return findKth(A, m, B, n, total / 2 + 1); } double findKth(int a[], int m, int b[], int n, int k) { if (m > n) return findKth(b, n, a, m, k); if (m == 0) return b[k-1]; if (k == 1) return min(a[0], b[0]); int ia = min(k/2, m); int ib = k - ia; if (a[ia-1] < b[ib-1]) return findKth(a+ia, m - ia, b, n, k - ia); else if (a[ia-1] > b[ib-1]) return findKth(a, m, b+ib, n - ib, k - ib); else return a[ia-1]; } };
相关推荐
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
leetcode 无法登录两个有序数组的中位数 问题 有两个大小分别为 m 和 n 的排序数组 nums1 和 nums2。求两个排序数组的中位数。 整体运行时间复杂度应该是 O(log (m+n))。 您可以假设 nums1 和 nums2 不能都为空。 ...
c语言入门 c语言_leetcode题解04-median-of-two-sorted-arrays.c
java入门 java_leetcode题解之004_Median_of_Two_Sorted_Arrays
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 ...
js js_leetcode题解之4-median-of-two-sorted-arrays.js
leetcode Python 001 leetcode的算法问题 这是我的解决方案,用 cpp 、 java 和 python 编写 #LeetCode 解决的问题: 001. Two Sum 002. Add Two Numbers 003. Longest Substring Without Repeating Characters4. ...
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 ...
leetcode Python 001 力码 ├── Algorithms │ ├── cpp │ │ ├── 001 - Two Sum.cpp │ │ ├── 002 - Add Two Numbers.cpp │ │ ├── 003 - Longest Substring Without Repeating ...
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 ...
#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:...
leetcode双人赛LeetCode 编号 题目 难度 题型 备注 Two Sum 简单 Add Two Numbers 中等 链结串列 重要 Longest Substring Without Repeating Characters 中等 字串 重要 Median of Two Sorted Arrays 困难 数学 ...
leetcode分类LeetCodeSourceCodes###LeetCode1TowSum如果数组是有序的,O(n)的时间复杂度就可以解决了,现在是无序的,一开始要排一下序###LeetCode2MedianofTwoSortedArrays让用logn的时间复杂度,用了两个二分,...
Median of Two Sorted Arrays JavaScript O(log (m+n)) O(1) Hard 7 Reverse Integer JavaScript O(n) O(1) Easy 9 Palindrome Number JavaScript O(n) O(1) Easy 19 Remove Nth Node From End of List JavaScript O...
Median of Two Sorted Arrays 5 Longest Palindromic Substring 8 String to Integer 11 Container with Most Water 14 Longest Common Prefix 15 Three Sum 16 Three Sum Closest 20 Valid Parentheses 26 Remove ...
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 ...
LeetCode刷题总结 1.Two Sum 2.Add Two Numbers 3.Longest Substring Without Repeating Characters 4.Median of Two Sorted Arrays 5.Longest Palindromic Substring (Manacher算法待完成) 6.ZigZag Conversion 7....
* 两个排序数组的中位数(Median of Two Sorted Arrays):计算两个排序数组的中位数。 * 整数到罗马(Integer to Roman):将整数转换为罗马数字。 * 罗马到整数(Roman to Integer):将罗马数字转换为整数。 4...
Median of Two Sorted Arrays 36.3% 困难 5 Longest Palindromic Substring 27.6% 中等 6 ZigZag Conversion 45.6% 中等 7 Reverse Integer 33.2% 简单 8 String to Integer (atoi) 18.5% 中等 9 Palindrome Number ...