[leet code 4] Median of Two Sorted Arrays
2015-02-09 21:55
204 查看
1 题目
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)).
2 思路
这题比较简单,实现的其实就是归并排序merge那个部分。
3 代码
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)).
2 思路
这题比较简单,实现的其实就是归并排序merge那个部分。
3 代码
public class MedianOfTwoSortedArrays { public double findMedianSortedArrays(int A[], int B[]) { double median = 0; int sum = A.length + B.length; int[] C = new int[sum]; int c = 0; int a = 0; int b = 0; while(a < A.length && b < B.length){ if (A[a] > B[b]) { C[c] = B[b]; b++; c++; }else { C[c] = A[a]; a++; c++; } } while (a < A.length) { C[c] = A[a]; c++; a++; } while(b < B.length){ C[c] = B[b]; c++; b++; } if (sum % 2 == 0) { median = (double) (C[sum/2] + C[sum/2 - 1])/2; }else { median = C[sum/2]; } System.out.println(median); return median; }
相关文章推荐
- Leet Code 4 Median of Two Sorted Arrays
- 【Leet Code】Median of Two Sorted Arrays
- leet code 004:Median of Two Sorted Arrays
- leecode algo4: Median of Two Sorted Arrays (Java)
- CODE 128: Median of Two Sorted Arrays
- leetcode 日经贴,python code -Median of Two Sorted Arrays
- LeetCode题解——Median of Two Sorted Arrays
- 二分查找——Median of two Sorted Arrays
- Median of Two Sorted Arrays -- LeetCode
- (算法分析Week4)Median of Two Sorted Arrays[Hard]
- LeetCoder_____Median of Two Sorted Arrays
- 【leetcode】Median of Two Sorted Arrays 如果觉得网上其他解释太难,那么请看这篇
- LeetCode Median of Two Sorted Arrays
- 【leetcode 002】Longest Substring Without Repeating Characters || Median of Two Sorted Arrays
- median-of-two-sorted-arrays
- [LeetCode] Median of Two Sorted Arrays 两个排序数组的中位数
- LeetCode 4. Median of Two Sorted Arrays
- LeetCode——Median of Two Sorted Arrays
- 寻找两个不等长数组的中位数 Median of Two Sorted Arrays
- LeetCode Median of Two Sorted Arrays