LeetCode#4 Median of Two Sorted Arrays
2015-07-04 07:21
337 查看
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)).
class Solution:
# @param {integer[]} nums1
# @param {integer[]} nums2
# @return {float}
def findMedianSortedArrays(self, nums1, nums2):
i = 0
j = 0
k = 0
newArr = []
while i < len(nums1) and j < len(nums2):
if nums1[i]<=nums2[j]:
newArr.append(nums1[i])
i+=1
k+=1
else:
newArr.append(nums2[j])
j+=1
k+=1
while i < len(nums1):
newArr.append(nums1[i])
i+=1
k+=1
while j < len(nums2):
newArr.append(nums2[j])
j+=1
k+=1
num = len(nums1) + len(nums2)
if num % 2 == 1:
return newArr[num/2]
else:
return float(newArr[num/2-1]+newArr[num/2])/2
size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
class Solution:
# @param {integer[]} nums1
# @param {integer[]} nums2
# @return {float}
def findMedianSortedArrays(self, nums1, nums2):
i = 0
j = 0
k = 0
newArr = []
while i < len(nums1) and j < len(nums2):
if nums1[i]<=nums2[j]:
newArr.append(nums1[i])
i+=1
k+=1
else:
newArr.append(nums2[j])
j+=1
k+=1
while i < len(nums1):
newArr.append(nums1[i])
i+=1
k+=1
while j < len(nums2):
newArr.append(nums2[j])
j+=1
k+=1
num = len(nums1) + len(nums2)
if num % 2 == 1:
return newArr[num/2]
else:
return float(newArr[num/2-1]+newArr[num/2])/2
相关文章推荐
- OSChina 周六乱弹 —— 性别不同,怎能恋爱?
- STM32W108无线射频模块通用IO接口
- OSSIM 4.1安装
- STM32W108无线ZigBee射频模块
- 专访4秒源码商城CTO陈杰:扎根互联网的“不安的心”
- 让餐厅放心的云服务-雅座CRM技术解密
- 非常好的Demo网站
- 解决ansible官网访问特别慢的问题
- LA 3902 Network 网络
- 【Leetcode】Contains Duplicate II #219
- MySQL中的datetime与timestamp比较-------转载
- spring aop实现mysql读写分离(可用于SAE的mysql数据库)
- IOS 判断当前UIViewController 是否正在显示
- 获取iOS应用中当前处于Activity状态的ViewController
- 大型网站的优化思路
- UVa 11520 Fill the Square 填充正方形
- 《Java面试试题》写一个两个线程之间的死锁
- LA 3635 Pie 派
- 【Leetcode】Course Schedule #207
- version (or platform)dependent Code