[LeetCode] 4. Median of Two Sorted Arrays
来源:互联网 发布:淘宝模特拍摄价格 编辑:程序博客网 时间:2024/06/07 11:35
思路:
将问题转换为求两个有序数组的第k项问题, 因为是中位数, 那么就是第(m + n) / 2项. 第k项问题在处理的时候用到二分法的思路, 每次用两个数组的中项相比, 小的一方左边所有元素都可以直接弃掉, 所以每次可以排除k / 2个元素. 时间复杂度为O(logk), 此题为O(log(m + n)).
// 查询两个排序数组第k个元素的函数double findKth(vector<int>::iterator it1, int n1, vector<int>::it2, int n2, int k) { if (n1 > n2) // 保证n1比n2短或等于 return findKth(it2, n2, it1, n1, int k); if (! n1) // 如果第一个数组为空, 则直接返回第二个数组中的元素 return *(it2 + k - 1); if (k == 1) // 如果k为1, 则直接返回两个数组中较小的首元素 return min(*it1, *it2); // 将k尽量分为两份, 但因为n1有可能小到不够k的一半, 所以为了避免越界访问数组, 取min int offset1 = min(k / 2, n1), offset2 = k - offset1; if (*(it1 + offset1 - 1) < *(it2 + offset2 - 1)) return findKth(it1 + offset1, n1 - offset1, it2, n2, k - offset1); else if (*(it1 + offset1 - 1) > *(it2 + offset2 - 1)) return findKth(it1, n1, it2 + offset2, n2 - offset2, k - offset2); else return *(it1 + offset1 - 1);}double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) { int total = nums1.size() + nums2.size(); if (total & 1) return findKth(nums1.begin(), nums1.size(), nums2.begin(), nums2.size(), total / 2 + 1); else return (findKth(nums1.begin(), nums1.size(), nums2.begin(), nums2.size(), total / 2) + findKth(nums1.begin(), nums1.size(), nums2.begin(), nums2.size(), total / 2 + 1)) / 2.0;}
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
- [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
- 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
- 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
- leetCode 4. Median of Two Sorted Arrays
- Leetcode 4. Median of Two Sorted Arrays
- SDKManager配置
- android学习之fragment的简单使用
- POJ1753 状态压缩(+BFS) + 棋盘问题
- 怎么在leangoo里绑定账号,更改邮箱?
- 数据库的一些论文的个人整理
- [LeetCode] 4. Median of Two Sorted Arrays
- javacard软件架构
- 面向对象
- PAT 表达式转换
- 21款最佳Linux命令行终端工具
- POJ 3468 线段树 区间增减 区间求和
- java导入https证书
- OC __attribute__
- 什么是PMU(PMIC)