LyWang

mergedSortedArray

Nov 25th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.18 KB | None | 0 0
  1. class Solution:
  2.         """
  3.        :type nums1: List[int]
  4.        :type m: int
  5.        :type nums2: List[int]
  6.        :type n: int
  7.        :rtype: void Do not return anything, modify nums1 in-place instead.
  8.        """
  9.         def mergeSortedArray(self, A, m, B, n):
  10.             # write your code here
  11.             i = m+n-1 #A size
  12.             j = m-1 #A
  13.             u = n-1 #B
  14.             if m == 0:
  15.                 for k in range(n):
  16.                     A[k] = B[k]
  17.             elif n == 0:
  18.                 pass
  19.             else:
  20.                 while i >=0:
  21.                     if B[u]>=A[j]:
  22.                         A[i] = B[u]
  23.                         i-=1
  24.                         if u==0:
  25.                             break
  26.                         else:
  27.                             u-=1
  28.                     else:
  29.                         A[i]=A[j]
  30.                         i-=1
  31.                         if j==0:
  32.                             while u>=0:
  33.                                 A[i]=B[u]
  34.                                 i-=1
  35.                                 u-=1
  36.                             break
  37.                         else:
  38.                             j-=1
Add Comment
Please, Sign In to add comment