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 ...
随机推荐
- PHP里面增加写日志功能
配置项中:
- java,sort函数的深刻理解
先来看看题目吧: 链接:https://www.nowcoder.com/questionTerminal/97b6a49a85944650b2e3d0660b91c324来源:牛客网 [编程题]德才 ...
- ubuntu下安装CAJ阅读器
目录 1.ubuntu下wine的基本介绍 (1)wine的介绍 (2)wine的安装 (3)exe文件的安装 (4)exe程序的卸载 (6)wine的基本使用 2.CAJ阅读器的安装 (1)首先放上 ...
- python基础之函数参数,名称空间,以及函数嵌套
函数进阶内容梗概: 1. 函数参数--动态传参 2. 名称空间, 局部名称空间, 全局名称空间, 作⽤用域, 加载顺序. 3. 函数的嵌套 4. gloabal , nonlocal 关键字 1. 函 ...
- Two Melodies CodeForces - 813D (DP,技巧)
https://codeforces.com/problemset/problem/813/D dp[i][j] = 一条链以i结尾, 另一条链以j结尾的最大值 关键要保证转移时两条链不能相交 #in ...
- Ubuntu中php.ini修改运行内存
首先,我们要知道我们要修改的是什么文件?那就是php.ini文件,一般php有关运行的文件都跟这个文件有关系,路径是 /etc/php5/apache2 .在这里我们就可以用命令进入, cd /etc ...
- PAT 1011 World Cup Betting
1011 World Cup Betting (20 分) With the 2010 FIFA World Cup running, football fans the world over w ...
- git 连接github的配置
这段时间要先在git上开发,上传代码到github上,所以首先需配置本地的git和github. 这几篇文章都不错,可以参考一下,大体的配置都很清楚. 1:https://blog.csdn.net/ ...
- 【Linux】bash shell学习
Bash Shell Linux系统的合法shell都写入/etc/shells这个文件,默认使用的shell版本称为“Bourne Again Shell(简称bash)” 用户登录时系统会分配一个 ...
- Object对象的浅拷贝与深拷贝方法详解
/* ===================== 直接看代码 ===================== */ <!DOCTYPE html> <html> <head& ...