mirror of https://github.com/TheAlgorithms/C
added median of two sorted arrays
This commit is contained in:
parent
78d815c795
commit
5f87bcd5d4
|
@ -7,6 +7,7 @@ LeetCode
|
|||
| # | Title | Solution | Difficulty |
|
||||
|---| ----- | -------- | ---------- |
|
||||
|3|[Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/) | [C](./src/3.c)|Medium|
|
||||
|4|[Median of Two Sorted Arrays](https://leetcode.com/problems/median-of-two-sorted-arrays/) | [C](./src/4/c)|Hard|
|
||||
|20|[Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) | [C](./src/20.c)|Easy|
|
||||
|27|[Remove Element](https://leetcode.com/problems/remove-element/) | [C](./src/27.c)|Easy|
|
||||
|24|[Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/) | [C](./src/24.c)|Medium|
|
||||
|
|
|
@ -0,0 +1,40 @@
|
|||
|
||||
|
||||
double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size){
|
||||
int index1=0;
|
||||
int index2=0;
|
||||
int v[nums1Size+nums2Size];
|
||||
int v_index=0;
|
||||
|
||||
while(index1<nums1Size && index2<nums2Size){
|
||||
if(nums1[index1]<=nums2[index2]){
|
||||
v[v_index++]=nums1[index1++];
|
||||
}
|
||||
else{
|
||||
v[v_index++]=nums2[index2++];
|
||||
}
|
||||
}
|
||||
if(index1<nums1Size){
|
||||
while(index1<nums1Size){
|
||||
v[v_index++]=nums1[index1++];
|
||||
}
|
||||
}
|
||||
if(index2<nums2Size){
|
||||
while(index2<nums2Size){
|
||||
v[v_index++]=nums2[index2++];
|
||||
}
|
||||
}
|
||||
if(v_index==1){
|
||||
return v[0];
|
||||
}
|
||||
if(v_index%2==0){
|
||||
double n1,n2;
|
||||
n1=v[v_index/2];
|
||||
n2=v[(v_index/2)-1];
|
||||
return (n1+n2)/2;
|
||||
}
|
||||
int new_index=(int)v_index/2;
|
||||
int i=0;
|
||||
return v[new_index];
|
||||
}
|
||||
|
Loading…
Reference in New Issue