(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 respectively.
Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
Example 1:
nums1 = [, ]
nums2 = [] The median is 2.0
Example 2:
nums1 = [, ]
nums2 = [, ] The median is ( + )/ = 2.5
class Solution:
def findMedianSortedArrays(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: float
"""
nums1.extend(nums2)
nums1.sort()
len1=len(nums1)
if len1:
if len1%==:
return (nums1[len1//2]+nums1[len1//2-1])/2
else:
return nums1[len1//2]
Code
def main(nums1,nums2):
nums1.extend(nums2)
nums1.sort()
len1=len(nums1)
if len1:
if len1%==:
return (nums1[len1//2]+nums1[len1//2-1])/2
else:
return nums1[len1//2]
# print(l1)
if __name__ == '__main__':
l1=[,]
l2=[]
print(main(l1,l2))
调试代码
(python)leetcode刷题笔记04 Median of Two Sorted Arrays的更多相关文章
- 【leetcode刷题笔记】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 ...
- (python)leetcode刷题笔记03 Longest Substring Without Repeating Characters
3. Longest Substring Without Repeating Characters Given a string, find the length of the longest sub ...
- (python)leetcode刷题笔记05 Longest Palindromic Substring
5. Longest Palindromic Substring Given a string s, find the longest palindromic substring in s. You ...
- 【leetcode刷题笔记】Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- 【leetcode刷题笔记】Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- 【leetcode刷题笔记】Search in Rotated Sorted Array II
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- 【leetcode刷题笔记】Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- 【leetcode刷题笔记】Search in Rotated Sorted Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- 18.9.10 LeetCode刷题笔记
本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...
随机推荐
- Notes 20180306 : 变量与常量
1.1 变量与常量 我们在开发中会经常听到常量和变量,那么常量和变量指的又是什么呢?顾名思义,在程序执行过程中,其值不能被改变的量称为常量,其值能被改变的量称为变量.变量与常量的命名都必须使用合法的标 ...
- LeetCodee 105. Construct Binary Tree from Preorder and Inorder Traversal
问题重述: 问题求解: 我们换一组比较有代表性的样例, 对于上图的树来说, index: 0 1 2 3 4 5 6 先序遍历为: 中序遍历为: 5同理,右子树也是如此.这样不难看出本题应该用递归方法 ...
- ubuntu包管理机制
1 ubuntu包管理机制 跟大家分享一下ubuntu的软件管理机制.如果你们有过: apt-get install 或者 apt-get update 失败的经历. 在众多的apt命令中迷失. 疑惑 ...
- C# 对象的深复制和浅复制
2019年第一篇博客,好吧,又大了一岁了,继续加油吧. 正文: C# 中的对象,众所周知是引用类型,那么如何复制对象Object呢,我们来看看下面这段代码: public class User { p ...
- 多用户OFDM系统资源分配研究
首先,OFDMA 是什么? OFDM 技术的基本原理是将无线信道划分为若干互相正交的子信道,把高速串行数据流转化为低速并行子数据流,低速并行子数据流在子信道上独立传输. OFDMA 是LTE的下行多址 ...
- 「PHP」简单工厂模式
引言 所属:创建型模式,常用设计模式之一 工厂模式分为:简单工厂模式.工厂方法模式.静态工厂模式.抽象工厂模式. 下面为简单工厂模式. 参考资料: <大话设计模式>程杰 模式概 ...
- 大数据&人工智能&云计算
仅从技术上讲大数据.人工智能都包含工程.算法两方面内容: 一.大数据: 工程: 1)云计算,核心是怎么管理大量的计算机.存储.网络. 2)核心是如何管理数据:代表是分布式存储,HDFS 3)核心是如何 ...
- 在树莓派上用 python 做一个炫酷的天气预报
教大家如何在树莓派上自己动手做一个天气预报.此次教程需要大家有一定的python 基础,没有也没关系,文末我会放出我已写好的代码供大家下载. 首先在开始之前 需要申请高德地图API,去高德地图官网注册 ...
- MAVLink功能开发,移植教程。
MAVLink功能开发 -----------------本文由"智御电子"提供,同时提供视频移植教程,以便电子爱好者交流学习.---------------- 1.MAVLink ...
- Python学习 :函数
函数 函数(Functions) 是指可重复使用的程序片段.它们允许你为某个代码块赋予名字,允许你通过这一特殊的名字在你的程序任何地方来运行代码块,并可重复任何次数.这就是调用(Calling)函数. ...