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 ... 
随机推荐
- BarTender是怎么做出雪花状文字
			一些小伙伴在做标签时,发现有的人做的标签上的文字颜色不是纯色的,问我是怎么做的.这种雪花状文字要设置出来其实很简单,只要用到字体颜色填充工具就可以了.下面,小编就来给大家简单介绍一下BarTender ... 
- DBA-mysql-字符集
			查看支持的字符集:show charset; 查看现使用字符集:status; 1.在[mysqld]下添加 default-character-set=utf8 (mysql 5.1版本) char ... 
- python print 进度条的例子
			def progress(width, percent): print "%s %d%%\r" % (('%%-%ds' % width) % (width * percent / ... 
- CentOS常见问题
			1.图形界面无法启动 检查messagebus服务是否为开机启动:chkconfig --list messagebus 如果状态为不启动,则修改之:chkconfig messagebus on 启 ... 
- HttpClientUtil [使用apache httpclient模拟http请求]
			基于httpclient-4.5.2 模拟http请求 以get/post方式发送json请求,并获取服务器返回的json -------------------------------------- ... 
- python之 Redis
			Redis redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorte ... 
- ios打包出来为pkg的处理方法
			Add LSRequiresIPhoneOS YES to your Info.plist The key can be found as Application requires iPhone en ... 
- Hibernate5.2之QBC查询
			Hibernate5.2值QBC查询 一.简介 Hibenate的QBC查询个人认为是Hib ... 
- 虚拟机拷贝后网卡eth0变成了eth1的解决办法
			一.修改/etc/udev/rules.d/70-persistent-net.rules文件 将之前的eth0那行删了,将eth1改为eth0 二.配置ifcfg-eth0脚本,注意HWADDR那行 ... 
- mir [20161220]
			最近玩backmir,查询了一些资料,突然领悟到原来各个地方的boss攻击和防御都有一定的上限,而相对应的,玩家也有攻击和防御,只要玩家的攻防能对付boss的攻防,就可以无伤打boss. 小时候玩热血 ... 
