leetcode-4. Median of Two Sorted Arrays
2017-09-25 09:07
316 查看
https://leetcode.com/problems/median-of-two-sorted-arrays/description/
题意理解:二分找中位数,凑《=(m+n+1)/2的在a中的数,然后找到在b中的数
急转弯:先定a,再定b
算法:二分
数据结构:
#
#
'binary'
__author__ = 'hjkruclion'
import sys
def read_int():
return list(map(int, sys.stdin.readline().split()))
class Solution:
def b_b(self, m, x, b):
l = 0
r = m
while(l < r):
mid = (l + r) // 2
if b[mid] <= x:
l = mid + 1
else:
r = mid
return r - 1 + 1
def b_a(self, n, m, key, a, b):
l = 0
r = n
while(l < r):
mid = (l + r) // 2
res = mid + 1 + self.b_b(m, a[mid], b)
if res <= key:
l = mid + 1
else:
r = mid
return r - 1
def findMedianSortedArrays(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: float
"""
a = nums1
b = nums2
n = len(a)
m = len(b)
key = (n + m + 1) // 2
x = self.b_a(n, m, key, a, b)
# print(x)
num_x = x - 0 + 1
num_y = key - num_x
if x < 0:
L = b[num_y - 1]
else:
L = a[x]
if num_y - 1 >= 0:
L = max(a[x], b[num_y - 1])
if (n + m) % 2 == 1:
return L
else:
Ra = x + 1
Rb = num_y - 1 + 1
if Ra >= n:
R = b[Rb]
elif Rb >= m:
R = a[Ra]
else:
R = min(a[Ra], b[Rb])
return (L + R) / 2
题意理解:二分找中位数,凑《=(m+n+1)/2的在a中的数,然后找到在b中的数
急转弯:先定a,再定b
算法:二分
数据结构:
#
#
'binary'
__author__ = 'hjkruclion'
import sys
def read_int():
return list(map(int, sys.stdin.readline().split()))
class Solution:
def b_b(self, m, x, b):
l = 0
r = m
while(l < r):
mid = (l + r) // 2
if b[mid] <= x:
l = mid + 1
else:
r = mid
return r - 1 + 1
def b_a(self, n, m, key, a, b):
l = 0
r = n
while(l < r):
mid = (l + r) // 2
res = mid + 1 + self.b_b(m, a[mid], b)
if res <= key:
l = mid + 1
else:
r = mid
return r - 1
def findMedianSortedArrays(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: float
"""
a = nums1
b = nums2
n = len(a)
m = len(b)
key = (n + m + 1) // 2
x = self.b_a(n, m, key, a, b)
# print(x)
num_x = x - 0 + 1
num_y = key - num_x
if x < 0:
L = b[num_y - 1]
else:
L = a[x]
if num_y - 1 >= 0:
L = max(a[x], b[num_y - 1])
if (n + m) % 2 == 1:
return L
else:
Ra = x + 1
Rb = num_y - 1 + 1
if Ra >= n:
R = b[Rb]
elif Rb >= m:
R = a[Ra]
else:
R = min(a[Ra], b[Rb])
return (L + R) / 2
相关文章推荐
- [LeetCode.Trick]Median of Two Sorted Arrays
- LeetCode Median of Two Sorted Arrays
- Leetcode: Median of Two Sorted Arrays
- Leetcode Median of Two Sorted Arrays
- LeetCode:Median of Two Sorted Arrays
- [LeetCode] Median of Two Sorted Arrays 解题报告
- LeetCode 4 - Median of Two Sorted Arrays
- leetcode-004 Median of Two Sorted Arrays
- leetcode——finding Median of Two Sorted Arrays题目分析
- LeetCode --- 4. Median of Two Sorted Arrays
- leetcode 刷题日记——Median of Two Sorted Arrays
- [Leetcode 4] Median of Two Sorted Arrays
- leetcode第一刷_Median of Two Sorted Arrays
- LeetCode-4.Median of Two Sorted Arrays
- [LeetCode]Median of Two Sorted Arrays
- [LeetCode]2 Median of Two Sorted Arrays (C++,Python实现)
- leetcode 4. Median of Two Sorted Arrays
- 【Leetcode】Median of Two Sorted Arrays
- 刷LeetCode(4)——Median of Two Sorted Arrays
- LeetCode(4)Median of Two Sorted Arrays