4_Median_of_Two_Sorted_Arrays.py
来源:互联网 发布:手机网页京东淘宝广告 编辑:程序博客网 时间:2024/04/27 22:04
题目是:
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
就是求两个数组所有数的的中间值
本来这个问题没什么难度的直接合并排序之后取中值,如下:
def time_complex_n(self, nums1, nums2): nums = nums1 + nums2 nums.sort() index, carry = divmod(len(nums), 2) if carry == 1: result = float(nums[index]) else: result = (nums[index] + nums[index-1])/2.0 return result
但是考虑到题目要求的时间复杂度是O(log (m+n))
同时由于O(log (n))是典型的二分查找算法的时间复杂度, 其核心思想是将一个数据分成2半然后分开的每一半再分成2半依次类推.
因此这里不能简单的使用time_complex_n这个算法.
0 0
- 4_Median_of_Two_Sorted_Arrays.py
- 4_Median_of_Two_Sorted_Arrays
- py
- py
- py
- py
- py
- py
- py
- Py
- Django--4--views.py详解
- py学习之路4---运算
- Notes of Py for informatics 4
- Faster-RCNN_TF代码解读4:config.py
- py-faster-rcnn详解(4)——anchor_target_layer.py接口说明
- bin2hex.py && hex2bin.py
- web.py (url.py)
- [py]py存放家具
- hashMap表面源码分析
- DQN 原理(二):理解 DQN 中的“Q”
- The Linux Programming Interface Appendix B Parsing Command-Line options 解析命令行中的选项
- Linux使用tcpdump抓取网络数据包示例
- HttpClient获取网络数据
- 4_Median_of_Two_Sorted_Arrays.py
- 关于Mongodb的全面总结
- Struts2学习笔记之入门
- Linux下抓包工具tcpdump应用详解
- android学习-grade编译不过解决办法
- 如何实现WiFi Display互联:我的一次WiFi Display(Miracast)功能发送端(source)和接收端(sink)的实现笔记
- redis 使用定位算法
- 什么是js闭包
- springmvc常用注解标签详解