Leetcode--Median of Two Sorted Arrays

问题描述:

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)).

《法一》最原始的办法,归并,把两个数组序列合并为一个链表,在查找二者中间的结果。程序结果正确,但运行时间超出限制。

代码如下:

import java.util.Scanner;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Solution{
public static double findMedianSortedArrays(int[] nums1, int[] nums2) {

double medium=0;
double medium1=0;
double medium2=0;
int i=0,j=0;
int a=0,b=0,c=0;
ArrayList<Integer> list = new ArrayList<Integer>();
while(i<nums1.length&&j<nums2.length)
{
if(nums1[i]<=nums2[j])
{
list.add(new Integer(nums1[i]));
i++;

}
else
{
list.add(new Integer(nums2[j]));
j++;
}
}

if(i==nums1.length)
while(j<nums2.length)
{
list.add(new Integer(nums2[j]));
j++;
}
else
while(i<nums1.length)
{
list.add(new Integer(nums1[i]));
i++;
}
int count=list.size();
/*System.out.println("the list is");
for(int k=0;k<count;k++)
System.out.println(list.get(k));*/

if(count%2==0)
{
a=count/2;
b=a-1;
medium1=list.get(a);
medium2=list.get(b);
medium=(medium1+medium2)/2.0;

}
else
{
c=(count+1)/2-1;
medium=list.get(c);
}

return medium;


}
public static void main(String args[]) {
Scanner s=new Scanner(System.in);
System.out.println("Input a value to stand the size of the first array");
int n=s.nextInt();
int[] intArray1= new int[n];
for(int i=0;i<n;i++)
{
Scanner count=new Scanner(System.in);
intArray1[i]= count.nextInt();
}

Scanner s1=new Scanner(System.in);
System.out.println("Input a value to stand the size of the second array");
int m=s1.nextInt();
int[] intArray2= new int[m];
for(int j=0;j<m;j++)
{
Scanner count1=new Scanner(System.in);
intArray2[j]= count1.nextInt();
}
s1.close();
double result=findMedianSortedArrays(intArray1,intArray2);
System.out.println(result);

}
}

 

《法二》转化为寻找两个有序数组第K小的元素,具体实现代码后续补充。

import java.util.Scanner;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
class Four_second{
public static double findMedianSortedArrays(int[] nums1, int[] nums2){
int length1=nums1.length;
int length2=nums2.length;

if((length1+length2)/2==0)
return (FindK(nums1,nums2,0,0,length1,length2,(length1+length2)/2)+FindK(nums1,nums2,0,0,length1,length2,(length1+length2)/2+1))/2;
else
return FindK(nums1,nums2,0,0,length1,length2,(length1+length2)/2+1);

}
public static int FindK(int[] nums1, int[] nums2,int start1,int start2,int len1,int len2,int k){

int length1=nums1.length;
int length2=nums2.length;
if(length1>length2){
return FindK(nums2,nums1,start2,start1,length2,length1,k);
}
if(length1==0)
return nums2[start2+k-1];

if(k==1)
return Math.min(nums1[start1],nums2[start2]);
int p=Math.min(k/2,length1);
int q=k-p;
if(nums1[p-1]<nums2[q-1]){
return FindK(nums1,nums2,p,start2,length1-p,length2,k-p);
}else if(nums1[p-1]>nums2[q-1]){
return FindK(nums1,nums2,start1,q,length1,length2-q,k-q);
} else{
return nums1[start1+p-1];
}

}
public static void main(String args[]){

Scanner s=new Scanner(System.in);
System.out.println("Input a value to stand the size of the first array");
int n=s.nextInt();
int[] intArray1= new int[n];
for(int i=0;i<n;i++)
{
Scanner count=new Scanner(System.in);
intArray1[i]= count.nextInt();
}

Scanner s1=new Scanner(System.in);
System.out.println("Input a value to stand the size of the second array");
int m=s1.nextInt();
int[] intArray2= new int[m];
for(int j=0;j<m;j++)
{
Scanner count1=new Scanner(System.in);
intArray2[j]= count1.nextInt();
}
s1.close();
double result=findMedianSortedArrays(intArray1,intArray2);
System.out.println(result);

}


}

 http://www.07net01.com/2015/07/871155.html

posted on 2016-11-06 19:55  毛无语666  阅读(150)  评论(0编辑  收藏  举报

导航