leetcode python 004
## 已知l1,l2均为升序数组,
## 在两数组l1,l2中寻找第n位数,
## 两数组中位数中,前者大于后者,说明后者中位数以下的成员必定在真正中位数之下
## 可以将其剔除,剔除a个元素后的两数组中寻找第n-a位数,等价于
def findmid(l1,l2):
m,n=len(l1),len(l2)
if (m+n)%2==0:
return (listrec(l1,l2,(m+n)/2)+listrec(l1,l2,(m+n)/2))/2
else:
return listrec(l1,l2,(m+n+1)/2)
## la长度大于等于lb
def listrec(la,lb,i):
m,n=len(la),len(lb)
if n==0:
return la[i-1]
if m<n:
return listrec(lb,la,i)
if i==1:
return min(la[0],lb[0])
p=min(int(i/2),n)
print(m,n,i,p)
if la[p-1]>lb[-1]:
return listrec(la,lb[p:],i-p)
else:
return listrec(la[p:],lb,i-p)
lm=[x for x in range(13,200)if x%2==0]
ln=[x for x in range(15,99)if x%2==1]
print(findmid(lm,ln))
leetcode python 004的更多相关文章
- Leetcode Python Solution(continue update)
leetcode python solution 1. two sum (easy) Given an array of integers, return indices of the two num ...
- LeetCode python实现题解(持续更新)
目录 LeetCode Python实现算法简介 0001 两数之和 0002 两数相加 0003 无重复字符的最长子串 0004 寻找两个有序数组的中位数 0005 最长回文子串 0006 Z字型变 ...
- [LeetCode][Python]Container With Most Water
# -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/container-with-most-water/ Given n non-neg ...
- LeetCode Python 位操作 1
Python 位操作: 按位与 &, 按位或 | 体会不到 按位异或 ^ num ^ num = 0 左移 << num << 1 == num * 2**1 右移 & ...
- 【leetcode❤python】Sum Of Two Number
#-*- coding: UTF-8 -*- #既然不能使用加法和减法,那么就用位操作.下面以计算5+4的例子说明如何用位操作实现加法:#1. 用二进制表示两个加数,a=5=0101,b=4=0100 ...
- [Leetcode][Python]56: Merge Intervals
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 56: Merge Intervalshttps://oj.leetcode. ...
- [Leetcode][Python]55: Jump Game
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 55: Jump Gamehttps://leetcode.com/probl ...
- [Leetcode][Python]54: Spiral Matrix
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 54: Spiral Matrixhttps://leetcode.com/p ...
- [Leetcode][Python]53: Maximum Subarray
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 53: Maximum Subarrayhttps://leetcode.co ...
随机推荐
- python requests库与json数据处理详解
1. http://docs.python-requests.org/zh_CN/latest/user/quickstart.html get方法将参数放在url里面,安全性不高,但是效率高:pos ...
- python 读写TXT,安装pandas模块。
今天需要用python读TXT 文件,发现pandas库好用,所以就去下载,没想pythoncharm中的setting中下载失败,所以去下源文件,安装pandas 是提示得先装numpy库,于是又去 ...
- spring boot ----> jpa连接和操作mysql数据库
环境: centos6.8,jdk1.8.0_172,maven3.5.4,vim,spring boot 1.5.13,mysql-5.7.23 1.引入jpa起步依赖和mysql驱动jar包 &l ...
- android-------- socket 实现客户端与服务器端通信
前面介绍了Socket的简介和原理,今天简单的来实现一下客服端与服务器通信的功能 客服端 建立连接 try { socket = new Socket("192.168.1.100" ...
- 配置samba 服务器 共享Linux目录
配置samba 服务器 共享Linux目录 1.安装: yum install -y samba* 2.修改配置文件 vim /etc/samba/smb.conf [web] path = /usr ...
- python记录_day27 tcp/ip五层模型
## 网络协议按照不同的功能分为多层,目前存在的模型有osi七层模型.tcp/ip五层和tcp/ip四层模型 我们主要用的是tcp/ip五层模型 那么每层的作用是什么呢,现在就从设计者的角度自下到上逐 ...
- 先天性肾上腺增生症(ACH)
先天性肾上腺增生症 类型 症状 并发症 治疗 情感支持 产前筛查 预防 什么是先天性肾上腺皮质增生症? 先天性肾上腺增生症(CAH)是一组影响肾上腺的遗传性疾病.肾上腺产生激素皮质醇和醛固酮.CAH是 ...
- jps报process information unavailable的解决办法
现象 启动Hadoop的时候使用jps检查进程 ,出现Process information unavailable的问题,如下 [root@vm8033 local]# jps -- process ...
- laravel响应的发送和程序终止
响应的发送是通过index.php中的$response->send();实现的 vendor\symfony\http-foundation\Response.php public funct ...
- 手机验证码JQUERY实现
<!DOCTYPE html> <html> <head> <script src="http://libs.baidu.com/jquery/1. ...