Leetcode4:Median of Two Sorted Arrays@Python
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 这道题给定两个由小到大已排好序的列表,将它继续由从小到大的顺序排列,并返回新列表的中间值。(要求时间复杂度不能超过O(log (m+n))) 首先,给定的两个列表都是已经排好序的,所以我们不使用几种排序算法,他们的时间复杂度都超过了log(m+n)。我们可以使用数据结构中学到的归并(MergeList)两个已排好序的链表的方法。
#-*-coding:utf-8-*- class Node(object):
'''
定义链表的结点
'''
def __init__(self,val):
self.val = val
self.next = None class Solution(object):
def initlist(self,nums):
'''
链表转换函数,传入一个列表,返回列表中元素组成的有头结点的链表
'''
l = Node(0)
ll = l
for num in nums:
node = Node(num)
ll.next = node
ll = ll.next
return l def findMedianSortedArrays(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: float
"""
list1 = self.initlist(nums1)
list2 = self.initlist(nums2)
list3 = Node(0)
pa = list1.next
pb = list2.next
pc = list3 while(pa and pb):
if(pa.val<=pb.val):
pc.next = Node(pa.val)
pc = pc.next
pa = pa.next
else:
pc.next = Node(pb.val)
pc = pc.next
pb = pb.next # 插入剩余段
if pa:
pc.next = pa
else:
pc.next = pb # 将已排好序的链表list3转换为列表nums3
nums3 = []
ll = list3.next
while(ll):
nums3.append(ll.val)
ll = ll.next j = len(nums3)/2
if len(nums3)%2==0:
return (nums3[j]+nums3[j-1])/2.0
else:
return nums3[j]
Leetcode4:Median of Two Sorted Arrays@Python的更多相关文章
- LeetCode4 Median of Two Sorted Arrays
题目: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
- [leetcode]Median of Two Sorted Arrays @ Python
原题地址:https://oj.leetcode.com/problems/median-of-two-sorted-arrays/ 题意:There are two sorted arrays A ...
- 力扣 -- 寻找两个有序数组的中位数 Median of Two Sorted Arrays python实现
题目描述: 中文: 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n)). 你可以假设 nums ...
- leetcode4 Median of Two Sorted Arrays学习记录
学习了扁扁熊的题解:https://leetcode-cn.com/problems/median-of-two-sorted-arrays/solution/4-xun-zhao-liang-ge- ...
- Leetcode4.Median of Two Sorted Arrays两个排序数组的中位数
给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个有序数组的中位数.要求算法的时间复杂度为 O(log (m+n)) . 你可以假设 nums1 和 nums2 不同 ...
- (python)leetcode刷题笔记04 Median of Two Sorted Arrays
4. Median of Two Sorted Arrays There are two sorted arrays nums1 and nums2 of size m and n respectiv ...
- 周刷题第二期总结(Longest Substring Without Repeating Characters and Median of Two Sorted Arrays)
这周前面刷题倒是蛮开心,后面出了很多别的事情和问题就去忙其他的,结果又只完成了最低目标. Lonest Substring Without Repeating Characters: Given a ...
- 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays
一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...
- [LintCode] Median of Two Sorted Arrays 两个有序数组的中位数
There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...
随机推荐
- 1.13 linux笔记
free -m 看内存脚本 rpm -ivh 显示过程 rpm -U --upgraderpm -F --freshenrpm -e --eraserpm -e koren find / -name ...
- winform中固定界面大小的方法
Step1: MaximizeBox : False MinimizeBox : False Step2: FormBoarderStyle : FixedSingle
- IDEA Generating project in Batch mode
Idea 设置 -DarchetypeCatalog=internal 然后再执行 : Archetype repository not defined. Using the one from [or ...
- geohash基本原理
geohash基本原理是将地球理解为一个二维平面,将平面递归分解成更小的子块,每个子块在一定经纬度范围内拥有相同的编码,这种方式简单粗暴,可以满足对小规模的数据进行经纬度的检索 目录: 经纬度常识 认 ...
- IOS培训还值得么
文章结构 1培训机构 各方面的评价 培训安排 收获 2 市场 就业 是否饱和 3 姿势 做好的事情 IOS这几年在IT界一直是热门的讨论话题,之前看着拉钩出品的北上广高薪岗位的人员技术流动也主要指向这 ...
- 很不错的在线Office控件:IWebOffice与SOAOffice
http://blog.csdn.net/cjh200102/article/details/17220441 iWebOffice2003文档控件 iWebOffice2003网络文档中间件能够在I ...
- Eclipse调试时附加匹配版本的JAR包源码:Edit Source Loopup
- hdu 5382 GCD?LCM!
先考虑化简f函数 发现,f函数可以写成一个递归式,化简后可以先递推求出所有f函数的值, 所以可以先求出所有S函数的值,对于询问,O(1)回答 代码: //File Name: hdu5382.cpp ...
- 使用Grunt 插件打包Electron Windows应用
最近利用Electron来创建跨桌面应用的趋势似乎很火.看了几个用Electron开发的应用,这些应用在windows下面的安装方式,都是类似一个绿色软件的安装方法,下载.zip->解压到相应目 ...
- bootstrap-提示标签、提示框
提示标签: <body> <div class="container"> <div class="row"> <div ...