(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 天的价格. ...
随机推荐
- Flex控件之repeater和radioButton应用
代码:http://www.cnblogs.com/modou/articles/2108346.html <?xml version="1.0" encoding=&quo ...
- 火狐中jq的attr出现的bug问题用prop代替
再工作的时候遇到一个很奇怪的问题 ,就是attr属性不好使!就问度娘去了...... 结果如下: .prop() 1..prop( propertyName ) 获取匹配集合中第一个元素的Prop ...
- Unity经验之谈-DoTween动画结束匿名委托之巨坑
产生问题: 成百上千个物体放在List列表里面循环,每个物体都要使用移动和移动结束事件. BUG: 动画结束之后我想隐藏该物体,结果却没有正常的隐藏,代码如下 foreach (var item in ...
- Java使用正则解决问题
分析以下需求,并用代码实现 (根据描述写匹配手机号和邮箱的正则表达式) 1.根据描述写出正则表达式 (1)手机号正则: 第一位为1 第二位为3或4或5或7或8 第三~十一位为0~9的其中一个数字 (2 ...
- 解决ssh连接linux系统特别慢的问题
新安装的centos系统,发现ssh连接很慢,因为是测试环境,对安全的要求不高,所以完全可以更快的连接,下面一起来解决这个问题. 一.分析主要原因: 1.SSH的反向DNS解析会消耗大量时间 2.GS ...
- oracle编程300例-性能优化(一)
1.在SELECT语句中避免使用“*” 2.尽可能减小记录行数 3.使用rowid高效删除重复记录 实例: delete from stu s where s.rowid>(select min ...
- MVC action过滤器验证登录
方法一 : 1.创建一个全局action过滤器 (在appstart 的filterconfig中注册 filters.Add(new LoginAttribute());) 2.不需要登 ...
- thinkphp5访问sql2000数据库
大家都知道php跟mysql是绝配,但是因为有时候工作需要,要求php访问操作sql2000,怎么办呢? 一般来说有两种方式: 1. sqlsrv驱动方式 2. odbc方式 sqlsrv驱动方式,因 ...
- php的基础知识(二)
7.系统常量: 常量的定义:常量是程序运行的时候是不可以改变的量 定义格式:define(‘常量名字’,‘常量的值’): 注意: ·不能重复定义 ·常量的名字最好用大写字母. ·常量的值只能是标量. ...
- STM32JTAG口用作普通IO的配置
使用Jlink向STM32烧录程序时,需要使用6个芯片的引脚(以STM32F103C8T6为例),分别是PB4/JNTRST.PB3/JTDO.PA13/JTMS.PA14/JTCK.PA15/JTD ...