1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | public class Solution { public void merge(int[] nums1, int m, int[] nums2, int n) { int available = m+n-1; int i = m-1; int j = n-1; while (i != -1 && j != -1){ if (nums1[i] > nums2[j]){ nums1[available--] = nums1[i--]; }else{ nums1[available--] = nums2[j--]; } } for (int k = i; k >= 0; k--){ nums1[available--] = nums1[k]; } for (int k = j; k >= 0; k--){ nums1[available--] = nums2[k]; } } } |
No comments:
Post a Comment