Leetcode in Java && Oracle
422 subscribers
8 photos
397 files
400 links
Second channel: @codeforces_java

Let's Develop Together!
Download Telegram
image_2022-06-27_16-25-33.png
53.6 KB
#HARD
#N4. Median of Two Sorted Arrays
problem link
class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int l1=nums1.length, l2=nums2.length, l=l1+l2;
int num[] = new int[l], i=0, j=0;
for(; i<l1 || j<l2; ){
if(i<l1 && j<l2){
if(nums1[i]>nums2[j])
num[i+j]=nums2[j++];
else
num[i+j]=nums1[i++];
}else if(i<l1)
num[i+j]=nums1[i++];
else
num[i+j]=nums2[j++];
}
return l%2==0 ? (num[l/2-1]+num[l/2])/2.0 : (double)num[l/2];
}
}
🔥3