题目:Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.
思路:二分查找+插入
class Solution(object):
def merge(self, nums1, m, nums2, n):
"""
:type nums1: List[int]
:type m: int
:type nums2: List[int]
:type n: int
:rtype: void Do not return anything, modify nums1 in-place instead.
"""
for i in range(len(nums1)-m):
nums1.pop()
pos=0
for i in range(n):
pos = self.binarySearch(nums1,pos,m+i,nums2[i])
nums1.insert(pos,nums2[i])
def binarySearch(self,num1,n,m,a):
l=0;r=m-1
while l<=r:
mid=(r-l)/2+l
if num1[mid]<a:
l=mid+1
elif num1[mid]>a:
r=mid-1
else: return mid
return l