【LeetCode算法练习(C语言)】Median of Two Sorted Arrays
来源:互联网 发布:iphone软件下载 编辑:程序博客网 时间:2024/06/05 13:48
题目:
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
链接:Median of Two Sorted Arrays
解法:非递归二分归并,时间 O(log (m+n))
double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size) { int cnt = (nums1Size + nums2Size) / 2 + 1; int pos1 = -1, pos2 = -1, tot1 = nums1Size, tot2 = nums2Size; double ans; while (pos1 + pos2 + 2 < cnt) { if (pos1 == tot1 - 1) { if (pos1 + (tot2 + pos2) / 2 + 2 <= cnt) pos2 = (tot2 + pos2) / 2; else tot2 = (tot2 + pos2) / 2; } else if (pos2 == tot2 - 1) { if (pos2 + (tot1 + pos1) / 2 + 2 <= cnt) pos1 = (tot1 + pos1) / 2; else tot1 = (tot1 + pos1) / 2; } else if (nums1[(tot1 + pos1) / 2] < nums2[(tot2 + pos2) / 2]) { if ((tot1 + pos1) / 2 + (tot2 + pos2) / 2 < cnt) pos1 = (tot1 + pos1) / 2; else tot2 = (tot2 + pos2) / 2; } else { if ((tot1 + pos1) / 2 + (tot2 + pos2) / 2 < cnt) pos2 = (tot2 + pos2) / 2; else tot1 = (tot1 + pos1) / 2; } } if ((nums1Size + nums2Size) % 2) { if (nums1[pos1] > nums2[pos2]) ans = (double)nums1[pos1]; else ans = (double)nums2[pos2]; } else { if (nums1[pos1] > nums2[pos2]) { if (pos1 >= 1 && nums1[pos1 - 1] > nums2[pos2]) ans = ((double)nums1[pos1] + (double)nums1[pos1 - 1]) / 2; else ans = ((double)nums1[pos1] + (double)nums2[pos2]) / 2; } else { if (pos2 >= 1 && nums2[pos2 - 1] > nums1[pos1]) ans = ((double)nums2[pos2] + (double)nums2[pos2 - 1]) / 2; else ans = ((double)nums1[pos1] + (double)nums2[pos2]) / 2; } } return ans;}
Runtime: 45 ms
阅读全文
0 0
- 【LeetCode算法练习(C语言)】Median of Two Sorted Arrays
- leetcode 4 Median of Two Sorted Arrays c语言实现
- LeetCode-4-Median of Two Sorted Arrays(C语言实现)
- leetcode Median of Two Sorted Arrays (C)
- 算法练习(3):Median of Two Sorted Arrays
- 【leetcode】4. Median of Two Sorted Arrays(Python & C++)
- [编程练习][Median of Two Sorted Arrays]<LeetCode-4>
- Leetcode练习 #4Median of Two Sorted Arrays
- 算法分析课每周练习 Median of Two Sorted Arrays
- LeetCode算法题4:Median of Two Sorted Arrays
- LeetCode算法题目:Median of Two Sorted Arrays
- [LeetCode]2 Median of Two Sorted Arrays (C++,Python实现)
- [leetcode-4]Median of Two Sorted Arrays(c)
- [c++]LeetCode Median of Two Sorted Arrays问题
- [LeetCode#4][C]Median of Two Sorted Arrays
- LeetCode 4. Median of Two Sorted Arrays(c++)
- Leetcode Median of two sorted arrays 的非递归O(m+n) C语言解法
- LeetCode: Median of Two Sorted Arrays
- 微信扫码关注公众号,公众号发放红包。一
- 经典Servlet+JSP+JavaBean开发模式(MVC)原理与创建工程
- VMware虚拟机文件夹中各文件作用详解
- Spring3+Spring-data-mongodb1.5.6示例
- 数据库存储过程
- 【LeetCode算法练习(C语言)】Median of Two Sorted Arrays
- GAN论文整理
- ubuntu设置开机启动程序
- 论文记录-Deep Compression:Compressing DeepNeural Networks With Pruning, Trained Quantization And Huffman
- Slf4j (一) HelloWorld示例
- 抓取数据为json文件,读入csv
- 基于Redis的简单分布式锁的原理
- HTML+CSS+JS面试题(附带答案)
- Java开发者必读的10篇精选优秀技术文章