class Solution: # @param A a list of integers # @param m an integer, length of A # @param B a list of integers # @param n an integer, length of B # @return nothing def merge(self, A, m, B, n): i,j,k=m-1,n-1,m+n-1 while i>=0 and j>=0: if A[i]>B[j]: A[k]=A[i] i-=1 else: A[k]=B[j] j-=1 k-=1 while j>=0: A[k]=B[j] j-=1 k-=1
Sunday, September 7, 2014
Leetcode: Merge Sorted Array @Python
Merge from the end so that the index of the elements won't change before merging.
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment