(leetcode)4.两个有序数列中间的第n个数 Median of Two Sorted Arrays--Java
来源:互联网 发布:邢帅表白app源码下载 编辑:程序博客网 时间:2024/05/21 16:57
There are two sorted arrays nums1 and nums2 of size m and n respectively.
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
nums2 = [3, 4]
The median is (2 + 3)/2 = 2.5
public class Solution {
public double findMedianSortedArrays(int A[], int B[]) {
int len = A.length + B.length;
if (len % 2 == 1) {
return findKth(A, 0, B, 0, len / 2 + 1);
}
return (
findKth(A, 0, B, 0, len / 2) + findKth(A, 0, B, 0, len / 2 + 1)
) / 2.0;
}
// find kth number of two sorted array
public static int findKth(int[] A, int A_start,
int[] B, int B_start,
int k){
if (A_start >= A.length) {
return B[B_start + k - 1];
}
if (B_start >= B.length) {
return A[A_start + k - 1];
}
if (k == 1) {
return Math.min(A[A_start], B[B_start]);
}
int A_key = A_start + k / 2 - 1 < A.length
? A[A_start + k / 2 - 1]
: Integer.MAX_VALUE;
int B_key = B_start + k / 2 - 1 < B.length
? B[B_start + k / 2 - 1]
: Integer.MAX_VALUE;
if (A_key < B_key) {
return findKth(A, A_start + k / 2, B, B_start, k - k / 2);
} else {
return findKth(A, A_start, B, B_start + k / 2, k - k / 2);
}
public double findMedianSortedArrays(int A[], int B[]) {
int len = A.length + B.length;
if (len % 2 == 1) {
return findKth(A, 0, B, 0, len / 2 + 1);
}
return (
findKth(A, 0, B, 0, len / 2) + findKth(A, 0, B, 0, len / 2 + 1)
) / 2.0;
}
// find kth number of two sorted array
public static int findKth(int[] A, int A_start,
int[] B, int B_start,
int k){
if (A_start >= A.length) {
return B[B_start + k - 1];
}
if (B_start >= B.length) {
return A[A_start + k - 1];
}
if (k == 1) {
return Math.min(A[A_start], B[B_start]);
}
int A_key = A_start + k / 2 - 1 < A.length
? A[A_start + k / 2 - 1]
: Integer.MAX_VALUE;
int B_key = B_start + k / 2 - 1 < B.length
? B[B_start + k / 2 - 1]
: Integer.MAX_VALUE;
if (A_key < B_key) {
return findKth(A, A_start + k / 2, B, B_start, k - k / 2);
} else {
return findKth(A, A_start, B, B_start + k / 2, k - k / 2);
}
}}
0 0
- (leetcode)4.两个有序数列中间的第n个数 Median of Two Sorted Arrays--Java
- LeetCode 4. Median of Two Sorted Arrays(两个有序数组的中位数)
- 【LeetCode 4. Median of Two Sorted Arrays】两个有序数组的中位数求解
- 【LeetCode】4. Median of Two Sorted Arrays两个有序数组的中位数
- Median of Two Sorted Arrays(获取两个有序数列的中值)
- LeetCode Median of Two Sorted Arrays两数列的中间数
- [LeetCode]Median of Two Sorted Arrays 二分查找两个有序数组的第k数(中位数)
- 两个有序数组的中位数 Median of Two Sorted Arrays
- [leetcode] Median of Two Sorted Arrays 两个排序数组找出第k个数,或者中位数
- Median of Two Sorted Arrays 两个有序数组的中位数@LeetCode
- Median of Two Sorted Arrays 两个有序数组的中位数@LeetCode
- LeetCode OJ 之 Median of Two Sorted Arrays(两个有序数组的中位数)
- [leetcode] Median of Two Sorted Arrays 寻找两个有序数组的中位数
- [Leetcode #4]Median of Two Sorted Arrays 计算两个有序数组的中位数
- [LeetCode] Median of Two Sorted Arrays 两个有序数组的中位数
- LeetCode OJ:Median of Two Sorted Arrays(两个有序数组的中位数)
- leetcode解题之4. Median of Two Sorted Arrays java (两个排序数组的中位数)
- 4. Median of Two Sorted Arrays 两个有序数组的中位数
- Oracle与MySQl对比
- eclipse下tomcat中文乱码解决
- 2017浙江省赛:Yet Another Game of Stones(尼姆变形)
- Android面试
- 解决 nginx server (ubuntu)上安全漏洞 OpenSSL AES-NI Padding Oracle MitM Information Disclosure
- (leetcode)4.两个有序数列中间的第n个数 Median of Two Sorted Arrays--Java
- 20. Valid Parentheses
- servlet中list转换成一个歌的对象
- New study shakes the roots of the dinosaur family tree
- 自己写的UiUtils工具类
- Java编程思想重点笔记(Java开发必看)
- 堆(heap)和栈(stack)、内存泄漏(memory leak)和内存溢出
- HTTP协议详解(转载)
- 修改hosts--目标文件夹访问拒绝解决办法