Leetcode题解(Python): 4.Median of Two Sorted Arrays (非递归)
来源:互联网 发布:papi 知乎 编辑:程序博客网 时间:2024/06/04 18:43
【题目】
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)).
【题解】
刚开始,因为写博客的原因看了眼前几道题的难度系数,这道题是5,当时以为很难,但是上手后感觉并不是很难。主要是借鉴了本科数据结构里学习的“将两个有序链表合并为一个有序链表”的思路。
代码:
class Solution(object): def findMedianSortedArrays(self, nums1, nums2): """ :type nums1: List[int] :type nums2: List[int] :rtype: float """ m = len(nums1); n = len(nums2); isEven = True;//用于记录列表总长是不是偶数 mid = 0;//用于记录列表中位数的下标 if (m + n) % 2 == 0: isEven = True; mid = (m + n) / 2; if mid > 0: mid -= 1; else: isEven = False; mid = (m + n - 1) / 2; i = j = 0; result = 0; needContinue = True; tmp1 = tmp2 = 0; //两个列表都有未遍历的元素 while i < m and j < n: if nums1[i] <= nums2[j]: tmp1 = nums1[i]; if i + j == mid: if isEven: i += 1; if i < m and nums1[i] <= nums2[j]: tmp2 = nums1[i]; else: tmp2 = nums2[j]; result = 1.0 * (tmp1 + tmp2) / 2; else: result = tmp1; needContinue = False; break; i += 1; else: tmp1 = nums2[j]; if i + j == mid: if isEven: j += 1; if j < n and nums2[j] <= nums1[i]: tmp2 = nums2[j]; else: tmp2 = nums1[i]; result = 1.0 * (tmp1 + tmp2) / 2; else: result = tmp1; needContinue = False; break; j += 1; if needContinue: //nums2中元素已经遍历完,但仍没有找到中位数,继续遍历nums1 while i < m: if i + j == mid: if isEven: if i + 1 < m: result = 1.0 * (nums1[i] + nums1[i + 1]) / 2; else: result = nums1[i]; else: result = nums1[i]; needContinue = False; break; i += 1; if needContinue: //nums1遍历完,仍未找到中位数,继续遍历nums2 while j < n: if i + j == mid : if isEven: if j + 1 < n: result = 1.0 * (nums2[j] + nums2[j + 1]) / 2; else: result = nums2[j]; else: result = nums2[j]; break; j += 1; return result;
这种方法在leetcode上的执行时间是120ms,击败82%+。 最坏时间复杂度为O(m+n)。就代码的简洁来说,感觉应该有更好的方法,在网上找了找,貌似都是用递归去写的。在这里就不再赘述,有兴趣的朋友可以自己百度~
水平有限,欢迎指正~
如需转发,请注明出处,thx~
0 0
- Leetcode题解(Python): 4.Median of Two Sorted Arrays (非递归)
- 【leetcode】4. Median of Two Sorted Arrays(Python & C++)
- LeetCode 题解(66): 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题解Java | 4. Median of Two Sorted Arrays
- 4. Median of Two Sorted Arrays LeetCode题解
- 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(Hard)
- LeetCode | 4. Median of Two Sorted Arrays(中位数)
- leetcode:4. Median of Two Sorted Arrays(Java实现)
- Median of Two Sorted Arrays -- leetcode -- Python
- 学习WCF之路3:在IIS中寄存服务
- 面试中常见链表问题5:删除链表中重复元素2
- 51nod 1082 与7无关的数
- Mysql 开启数据的远程访问权限
- Object.keys(obj)返回参数obj可被枚举的属性
- Leetcode题解(Python): 4.Median of Two Sorted Arrays (非递归)
- android库项目设置
- 盘点:2016中国百强地产CIO高峰论坛的8大看点
- ORA-12518: TNS: 监听程序无法分发客户机连接
- 使用ajax提交form表单
- ASP.NET MVC Razor 输出没有编码的HTML字符串
- PHP的foreach中无法替换数组的值
- iOS Xcode编辑器 光标消失问题
- [hihoCoder] 区域周长 解题报告