Skip to content

Commit e6eece7

Browse files
authored
Create median_of_two_sorted_array.md
1 parent 51f8211 commit e6eece7

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

median_of_two_sorted_array.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Median of Two Sorted Arrays
2+
希望没人打开这篇,因为这道题我还没A就想睡觉了!!!!最近又有点神经衰弱,emmmm,老是想东想西,不在状态,要好好调整啊
3+
## Problem
4+
There are two sorted arrays nums1 and nums2 of size m and n respectively.
5+
6+
Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
7+
8+
Example 1:
9+
nums1 = [1, 3]
10+
nums2 = [2]
11+
12+
The median is 2.0
13+
Example 2:
14+
nums1 = [1, 2]
15+
nums2 = [3, 4]
16+
17+
The median is (2 + 3)/2 = 2.5
18+
## 解题思路
19+
主要考查如何减少时间复杂度
20+
## 未A代码
21+
```
22+
void quickSort(int* nums,int first,int end){
23+
int temp,l,r;
24+
if(first>=end)return;
25+
temp=nums[first];
26+
l=first;r=end;
27+
while(l<r){
28+
while(l<r && nums[r]>=temp)
29+
r--;
30+
if(l<r)
31+
nums[l]=nums[r];
32+
while(l<r && nums[l]<=temp)
33+
l++;
34+
if(l<r)
35+
nums[r]=nums[l];
36+
}
37+
nums[l]=temp;
38+
quickSort(nums,first,l-1);
39+
quickSort(nums,l+1,end);
40+
}
41+
double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size) {
42+
if(nums1Size==0&&nums2Size==0)
43+
return 0;
44+
else{
45+
int new[nums1Size+nums2Size];
46+
double result;
47+
for(int i=0;i<nums1Size;i++)
48+
new[i]=nums1[i];
49+
for(int j=0;j<nums2Size;j++)
50+
{
51+
int count=nums1Size;
52+
new[count]=nums2[j];
53+
count++;
54+
}
55+
quickSort(new,0,nums1Size+nums2Size-2);
56+
int size=nums1Size+nums2Size;
57+
if((nums1Size+nums2Size)%2!=0){
58+
int a=size/2-1;
59+
int b=size/2;
60+
result=((new[a]+new[b])/2);
61+
}
62+
else
63+
result=(double)new[size/2];
64+
return result;
65+
}
66+
}
67+
```

0 commit comments

Comments
 (0)