LeetCode:4.寻找两个正序数组中的中位数(median-of-two-sorted-arrays)
思路与题解:LeetCode:4.寻找两个正序数组中的中位数(median-of-two-sorted-arrays)
方法一
import java.util.Scanner;
/**
* 在这里给出对类 LC4MedianOfTwoSortedArrays 的描述。
*
* @作者(yequan17)
* @版本(2021.12.8)
*/
public class LC4MedianOfTwoSortedArrays
{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String[] str1=sc.nextLine().split(",");
int[] nums1=new int[str1.length];
for(int i=0;i<nums1.length;i++){
nums1[i]=Integer.parseInt(str1[i]);
}
String[] str2=sc.nextLine().split(",");
int[] nums2=new int[str2.length];
for(int i=0;i<nums2.length;i++){
nums2[i]=Integer.parseInt(str2[i]);
}
System.out.print(findMedianSortedArrays(nums1,nums2));
}
public static double findMedianSortedArrays(int[] nums1, int[] nums2) {
int[] nums;
int m = nums1.length;
int n = nums2.length;
nums = new int[m + n];
if (m == 0) {
if (n % 2 == 0) {
return (nums2[n / 2 - 1] + nums2[n / 2]) / 2.0;
} else {
return nums2[n / 2];
}
}
if (n == 0) {
if (m % 2 == 0) {
return (nums1[m / 2 - 1] + nums1[m / 2]) / 2.0;
} else {
return nums1[m / 2];
}
}
int count = 0;
int i = 0, j = 0;
while (count != (m + n)) {
if (i == m) {
while (j != n) {
nums[count++] = nums2[j++];
}
break;
}
if (j == n) {
while (i != m) {
nums[count++] = nums1[i++];
}
break;
}
if (nums1[i] < nums2[j]) {
nums[count++] = nums1[i++];
} else {
nums[count++] = nums2[j++];
}
}
if (count % 2 == 0) {
return (nums[count / 2 - 1] + nums[count / 2])/ 2.0;
} else {
return nums[count / 2];
}
}
}
方法二
import java.util.Scanner;
/**
* 在这里给出对类 LC4MedianOfTwoSortedArrays2 的描述。
*
* @作者(yequan17)
* @版本(2021.12.8)
*/
public class LC4MedianOfTwoSortedArrays2
{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String[] str1=sc.nextLine().split(",");
int[] nums1=new int[str1.length];
for(int i=0;i<nums1.length;i++){
nums1[i]=Integer.parseInt(str1[i]);
}
String[] str2=sc.nextLine().split(",");
int[] nums2=new int[str2.length];
for(int i=0;i<nums2.length;i++){
nums2[i]=Integer.parseInt(str2[i]);
}
System.out.print(findMedianSortedArrays(nums1,nums2));
}
public static double findMedianSortedArrays(int[] A, int[] B) {
int m = A.length;
int n = B.length;
int len = m + n;
int left = -1, right = -1;
int aStart = 0, bStart = 0;
for (int i = 0; i <= len / 2; i++) {
left = right;
if (aStart < m && (bStart >= n || A[aStart] < B[bStart])) {
right = A[aStart++];
} else {
right = B[bStart++];
}
}
if ((len & 1) == 0)
return (left + right) / 2.0;
else
return right;
}
}
方法三
import java.util.Scanner;
/**
* 在这里给出对类 LC4MedianOfTwoSortedArrays2 的描述。
*
* @作者(yequan17)
* @版本(2021.12.8)
*/
public class LC4MedianOfTwoSortedArrays2
{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String[] str1=sc.nextLine().split(",");
int[] nums1=new int[str1.length];
for(int i=0;i<nums1.length;i++){
nums1[i]=Integer.parseInt(str1[i]);
}
String[] str2=sc.nextLine().split(",");
int[] nums2=new int[str2.length];
for(int i=0;i<nums2.length;i++){
nums2[i]=Integer.parseInt(str2[i]);
}
System.out.print(findMedianSortedArrays(nums1,nums2));
}
public static double findMedianSortedArrays(int[] A, int[] B) {
int m = A.length;
int n = B.length;
int len = m + n;
int left = -1, right = -1;
int aStart = 0, bStart = 0;
for (int i = 0; i <= len / 2; i++) {
left = right;
if (aStart < m && (bStart >= n || A[aStart] < B[bStart])) {
right = A[aStart++];
} else {
right = B[bStart++];
}
}
if ((len & 1) == 0)
return (left + right) / 2.0;
else
return right;
}
}
方法四
import java.util.Scanner;
/**
* 在这里给出对类 LC4MedianOfTwoSortedArray4 的描述。
*
* @作者(yequan17)
* @版本(2021.12.8)
*/
public class LC4MedianOfTwoSortedArray4
{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String[] str1=sc.nextLine().split(",");
int[] nums1=new int[str1.length];
for(int i=0;i<nums1.length;i++){
nums1[i]=Integer.parseInt(str1[i]);
}
String[] str2=sc.nextLine().split(",");
int[] nums2=new int[str2.length];
for(int i=0;i<nums2.length;i++){
nums2[i]=Integer.parseInt(str2[i]);
}
System.out.print(findMedianSortedArrays(nums1,nums2));
}
public static double findMedianSortedArrays(int[] nums1, int[] nums2) {
int m = nums1.length;
int n = nums2.length;
if (m > n) {
return findMedianSortedArrays(nums2,nums1); // 保证 m <= n
}
int iMin = 0, iMax = m;
while (iMin <= iMax) {
int i = (iMin + iMax) / 2;
int j = (m + n + 1) / 2 - i;
if (j != 0 && i != m && nums2[j-1] > nums1[i]){ // i 需要增大
iMin = i + 1;
}
else if (i != 0 && j != n && nums1[i-1] > nums2[j]) { // i 需要减小
iMax = i - 1;
}
else { // 达到要求,并且将边界条件列出来单独考虑
int maxLeft = 0;
if (i == 0) { maxLeft = nums2[j-1]; }
else if (j == 0) { maxLeft = nums1[i-1]; }
else { maxLeft = Math.max(nums1[i-1],nums2[j-1]); }
if ( (m + n) % 2 == 1 ) { return maxLeft; } // 奇数的话不需要考虑右半部分
int minRight = 0;
if (i == m) { minRight = nums2[j]; }
else if (j == n) { minRight =nums1[i]; }
else { minRight = Math.min(nums2[j], nums1[i]); }
return (maxLeft + minRight) / 2.0; //如果是偶数的话返回结果
}
}
return 0.0;
}
}