LeetCode Median of Two Sorted Arrays
来源:互联网 发布:81端口是什么服务 编辑:程序博客网 时间:2024/06/15 11:05
鄙人 刘天昊 csdn新用户 用博客来记录自己的学习过程也希望和网络上志同道合的朋友做一些交流
leetcode篇因为研究生本科都不是cs出生,所以自学数据结构,所有的leetcode都是C语言实现
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
题目告诉你拿到两个排好序的数字大小分别为m,n
找到他们新排序的数组
的中值那么要求算法的时间复杂度是O(log(m+n));
先说下自己的思路,拿到题目两个排好序的数组,很容易让大家想到合并排序。
然后在合并排序好的数组找中位数
double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size){ int *sum; const int len=nums1Size+nums2Size; sum=(int *)malloc((len)*sizeof(int)); int i=0,j=0,k=0; while(i<nums1Size&&j<nums2Size) { if(*(nums1+i)<=*(nums2+j)) { *(sum+k)=*(nums1+i); i++; } else { *(sum+k)=*(nums2+j); j++; } k++; } if(i>=nums1Size) { for(;j<nums2Size;j++,k++) { *(sum+k)=*(nums2+j); } } else { for(i;i<nums1Size;i++,k++) { *(sum+k)=*(nums1+i); } } if((len)%2==0) { return (double)(*(sum+len/2-1)+*(sum+len/2))/2.0; } else { return (double)*(sum+(len-1)/2) ; } }
然而这么做,并不满足题目所说的O(log(m+n))
这里应该对两个数组进行二分查找然而我并没有做出来,把自己绕进去了
这里po的后一个代码不是自己的是discuss里的值得学习的地方很多
#define min(x, y) (x < y ? x : y)int odd(int n) { return n & 0x1; }void swap(int *x, int *y) { int tmp = *x; *x = *y; *y = tmp;}/* meidan of an array */double medianof(int A[], int n) { return odd(n) ? (double) A[n / 2] : (double)(A[ n / 2] + A[n / 2 - 1]) / 2.0;}int find(int A[], int m, int B[], int n) { int l = 0, u = m; int i, j, half = (m + n + 1) / 2; if (!A || m == 0) return medianof(B, n); if (!B || n == 0) return medianof(A, m); while (l < u) { i = (l + u) / 2; j = half - i - 2; if (j < 0 || j >= n) { if (j == -1 && A[i] <= B[0]) return i; /* found */ if (j >= n ) l = i + 1; /* too small */ else u = i; /* too big */ } else { if (B[j]<= A[i] && (j == n - 1 || A[i] <= B[j+1])) return i; /* found */ else if (A[i] < B[j]) l = i + 1; /* too small */ else u = i; /* too big */ } } return -1;}double findMedianSortedArrays(int A[], int m, int B[], int n) { int i, j, k, *C; if (!A || m == 0) return medianof(B, n); if (!B || n == 0) return medianof(A, m); if ((i = find(A, m, B, n)) == -1) { i = find(B, n, A, m); C = A; A = B; B = C; swap(&m, &n); } if (odd(m + n)) return (double)A[i]; j = (m + n) / 2 - i - 2; if (i == m - 1) k = B[j+1]; else if (j == n - 1) k = A[i+1]; else k = min(A[i+1], B[j+1]); return (double)(A[i] + k) / 2.0;}
那么这次就到这里
欢迎交流讨论
0 0
- LeetCode: Median of Two Sorted Arrays
- LeetCode Median of Two Sorted Arrays
- leetcode 26: Median of Two Sorted Arrays
- Leetcode 4 Median of Two Sorted Arrays
- [Leetcode] Median of Two Sorted Arrays
- LeetCode 4 - Median of Two Sorted Arrays
- [LeetCode]Median of Two Sorted Arrays
- leetcode Median of Two Sorted Arrays
- LeetCode-Median of Two Sorted Arrays
- [LeetCode] Median of Two Sorted Arrays
- [LeetCode] Median of Two Sorted Arrays
- [leetcode] Median of Two Sorted Arrays
- leetcode-004:Median of Two Sorted Arrays
- Leetcode 4 Median of Two Sorted Arrays
- leetcode之 median of two sorted arrays
- 【转载】【leetcode】Median of Two Sorted Arrays
- LeetCode - Median of Two Sorted Arrays
- [LeetCode]Median of Two Sorted Arrays
- 你仍未知道那些有关 Activity典型、异常情况下的生命周期解析
- 【linux】vim学习总结(一)
- Nginx+Tomcat搭建高性能负载均衡集群
- Android:指定某浏览器访问指定页面
- java 十二十三天学习笔记总结
- LeetCode Median of Two Sorted Arrays
- cordova 提交iOS APP 需要注意的点
- Spring Boot 热部署与日志配置
- java学习笔记(二十五)模拟斗地主发牌与洗牌
- perl 十六进制转二进制
- Mybatis 的分页插件PageHelper-4.1.1的使用
- scikit-learn中离散特征二值化
- 单纯形求解线性规划
- TPCH测试脚本