Shawngbk / Leecode

Questions of Leecode
0 stars 0 forks source link

88. Merge Sorted Array #37

Open Shawngbk opened 7 years ago

Shawngbk commented 7 years ago

从后往前放元素 public class Solution { public void merge(int[] nums1, int m, int[] nums2, int n) { int sum = m + n - 1; int p1 = m - 1; int p2 = n - 1; while(p1 >=0 && p2 >= 0) { if(nums1[p1] > nums2[p2]) { nums1[sum] = nums1[p1]; sum--; p1--; } else { nums1[sum] = nums2[p2]; sum--; p2--; } } // if nums2 still has sth left for(int i = 0; i <= p2; i++) { nums1[i] = nums2[i]; } } }

Shawngbk commented 7 years ago

FB Bloomberg