标签:spec rap miss target The color example row 思路
题目: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)).
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
思路:见我的另一篇博客:寻找第K小数
代码如下;
1 class Solution { 2 public double findMedianSortedArrays(int[] nums1, int[] nums2) { 3 if((nums1.length+nums2.length)%2==1){ 4 int k=findKthNum(nums1,nums2,(nums1.length+nums2.length+1)/2); 5 double r=k/1.0; 6 return r; 7 }else{ 8 int a=findKthNum(nums1,nums2,(nums1.length+nums2.length+1)/2); 9 int b=findKthNum(nums1,nums2,(nums1.length+nums2.length+1)/2+1); 10 double r=(a+b)/2.0; 11 return r; 12 } 13 14 } 15 public int getUpMedian(int[] arr1,int start1,int end1,int[] arr2,int start2,int end2){ 16 int mid1=0,mid2=0; 17 int offset=0;//用于判断过程中数组的长度的奇偶 18 while(start1<end1){ 19 mid1=(start1+end1)/2; 20 mid2=(start2+end2)/2; 21 offset=((end1-start1+1)&1)^1; 22 //元素个数为奇数,offset为0,元素个数为偶数,offset为1 23 if (arr1[mid1] > arr2[mid2]){ 24 end1=mid1; 25 start2=mid2+offset; 26 }else if(arr1[mid1]<arr2[mid2]){ 27 end2=mid2; 28 start1=mid1+offset; 29 }else{ 30 return arr1[mid1]; 31 } 32 } 33 return Math.min(arr1[start1],arr2[start2]); 34 } 35 public int findKthNum(int[]arr1,int[]arr2,int kth){ 36 if(arr1.length==0)return arr2[kth-1]; 37 if(arr2.length==0)return arr1[kth-1]; 38 if(kth<1||kth>arr1.length+arr2.length){ 39 throw new RuntimeException("k is invalid"); 40 } 41 int[]longs=arr1.length>=arr2.length?arr1:arr2; 42 int[]shorts=arr1.length<arr2.length?arr1:arr2; 43 int l=longs.length; 44 int s=shorts.length; 45 if(kth<=s){ 46 return getUpMedian(shorts,0,kth-1,longs,0,kth-1); 47 } 48 if(kth>l){ 49 if(shorts[kth-l-1]>=longs[l-1]) 50 return shorts[kth-l-1]; 51 if(longs[kth-s-1]>=shorts[s-1]) 52 return longs[kth-s-1]; 53 return getUpMedian(shorts,kth-l,s-1,longs,kth-s,l-1); 54 } 55 else if(longs[kth-s-1]>=shorts[s-1]){ 56 return longs[kth-s-1]; 57 } 58 return getUpMedian(shorts,0,s-1,longs,kth-s,kth-1); 59 } 60 }
Your runtime beats 18.77 % of java submissions
不知道为何还是比较慢,看了排第一的算法,感觉差别不大
1 class Solution { 2 public double findMedianSortedArrays(int[] nums1, int[] nums2) { 3 int len1 = nums1.length; 4 int len2 = nums2.length; 5 if((len1+len2)%2==0){ 6 return (double)(findKnum(nums1,nums2, 0,0,len1,len2,(len1+len2)/2)+findKnum(nums1,nums2, 0,0,len1,len2,(len1+len2)/2+1))/2; 7 }else{ 8 return findKnum(nums1,nums2, 0,0,len1,len2,(len1+len2)/2+1); 9 } 10 } 11 12 public int findKnum(int[] num1, int[] num2, int start, int start2, int len1, int len2, int k){ 13 if(len1>len2){ 14 return findKnum(num2,num1, start2,start,len2,len1,k); 15 } 16 if(len1 == 0) 17 return num2[start2+k-1]; 18 if(k==1) 19 return Math.min(num1[start], num2[start2]); 20 int pa = Math.min(len1, k/2); 21 int pb = k - pa; 22 if(num1[start+pa-1]<num2[start2+pb-1]){ 23 return findKnum(num1,num2,start+pa,start2,len1-pa,len2,k-pa); 24 }else if(num1[start+pa-1]>num2[start2+pb-1]){ 25 return findKnum(num1, num2, start, start2+pb,len1, len2-pb, k-pb); 26 }else{ 27 return num1[start+pa-1]; 28 } 29 30 } 31 }
leetcode-4-Median of Two Sorted Arrays
标签:spec rap miss target The color example row 思路
原文地址:https://www.cnblogs.com/pathjh/p/9097858.html