leetcode 4:Median of Two Sorted Arrays

public double FindMedianSortedArrays(int[] nums1, int[] nums2) {

int t=nums1.Length+nums2.Length;
int mid1=0;
int mid2=0;
int i=0;
int p1=0;
int p2=0;
while(p1<nums1.Length&&p2<nums2.Length&&i<=(t/2))
{
if(nums1[p1]<nums2[p2])
{
mid1=mid2;
mid2=nums1[p1];
p1++;
}
else
{
mid1=mid2;
mid2=nums2[p2];
p2++;
}
i++;
}
if(i<=(t/2))
{
while(p1<nums1.Length&&i<=(t/2))
{
mid1=mid2;
mid2=nums1[p1];
p1++;
i++;
}
while(p2<nums2.Length&&i<=(t/2))
{
mid1=mid2;
mid2=nums2[p2];
p2++;
i++; }
}
if(t%2==0)
return (mid1+mid2)/2.0;
else
return mid2; }

  合并两个有序数组,取中间的两个或者一个数

上一篇:分布式事务-03-XA, 2PC, 3PC 等理论知识


下一篇:python+selenium,打开浏览器时报selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH