4.Median of Two Sorted Arrays
来源:互联网 发布:高通unity3d 编辑:程序博客网 时间:2024/05/16 18:17
题目: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
超时的实现:
double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size) { int num=nums1Size+nums2Size; double ret=0; int i=0,j=0; quicksort(nums1,0,nums1Size-1); quicksort(nums2,0,nums2Size-1); for(i=0;i<nums1Size;i++) { printf("%d ",nums1[i]); } if(num%2==0) { num=num>>1; if(num<nums1Size) { ret=(nums1[num]+nums1[num-1])/2.0; } else if(nums1[num-1] == nums1[nums1Size-1]) { ret=(nums1[num-1]+nums2[0])/2.0; } else if((num-1)>=nums1Size) { ret=(nums2[num-nums1Size-1]+nums2[num-nums1Size])/2.0; } } else { num=num>>1; if(num<nums1Size) { ret=nums1[num]; } else { ret=nums2[num-nums1Size]; } } return ret;}void quicksort(int* v, int left, int right){ if(left < right){ int key = v[left]; int low = left; int high = right; while(low < high){ while(low < high && v[high] > key){ high--; } v[low] = v[high]; while(low < high && v[low] < key){ low++; } v[high] = v[low]; } v[low] = key; quicksort(v,left,low-1); quicksort(v,low+1,right); }}
别人的实现:
double searchKth(int* a,int m,int* b,int n,int k){ printf("The k is %d %d %d\n",k,m,n); if(m > n) // m is always the smmaller { return searchKth(b,n,a,m,k); } if(m==0) { return b[k-1]; } if(k==1) { printf(" a[0] %d, b[0] %d\n",a[0],b[0]); if(a[0]<=b[0]) { return a[0]; } else return b[0]; } int p=0,q=0; if((k/2)<=m) p=k/2; else p=m; q=k-p; printf("%d %d\n",p,q); if(a[p-1] < b[q-1]) return searchKth(a+p,m-p,b,n,k-p); else if(a[p-1] > b[q-1]) return searchKth(a,m,b+q,n-q,k-q); else return a[p-1];}double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size) { int sum=nums1Size+nums2Size; if(sum & 0x1)//奇数 { return searchKth(nums1,nums1Size,nums2,nums2Size,sum/2+1); } else { double t=searchKth(nums1,nums1Size,nums2,nums2Size,sum/2)+searchKth(nums1,nums1Size,nums2,nums2Size,sum/2+1); return t/2; }}
0 0
- [LeetCode]4.Median of Two Sorted Arrays
- LeetCode 4. Median of Two Sorted Arrays
- LeetCode --- 4. Median of Two Sorted Arrays
- [Leetcode] 4. Median of Two Sorted Arrays
- 4.Median of Two Sorted Arrays
- [LeetCode]4.Median of Two Sorted Arrays
- 4. Median of Two Sorted Arrays
- 4.Median of Two Sorted Arrays
- 4. Median of Two Sorted Arrays
- 4. Median of Two Sorted Arrays
- 【leetcode】4. Median of Two Sorted Arrays
- Leetcode-4.Median of Two Sorted Arrays
- 4. Median of Two Sorted Arrays
- 4. Median of Two Sorted Arrays
- LeetCode-4.Median of Two Sorted Arrays
- leetcoode 4. Median of Two Sorted Arrays
- Leetcode 4. Median of Two Sorted Arrays
- leetcode 4. Median of Two Sorted Arrays
- Svchosts病毒分析报告
- JAVA关键字
- Android 在win8的cmd中输入adb shell提示不是内部或外部命令
- 编写程序模拟堆栈的存储方式
- Servlet中文乱码
- 4.Median of Two Sorted Arrays
- 从A-za-z0-9任选6个组成一字符串
- POJ 1611 The Suspects 并查集
- 笔记-CCNA与网络安全 第1章 计算机网络
- POJ 2337 Catenyms 欧拉回路
- Numeber类——包装类
- 模块加载过程代码分析1
- 网易秋招编程题——最大的奇约数
- hibernate中的二级缓存的问题