88. Merge Sorted Array

这道题的trick在于如果从小到大排列的话,涉及到数组从左向右移动的问题。 所以这道题从大到小进行排序。

另外,这道题中该学习到的coding style,当需要处理2个数组/list时候 用

      while (a != null && b != null) {}

      while (a != null) {}

      while (b != null) {}

这样3个while循环

 1 public class Solution {
 2     public void merge(int[] nums1, int m, int[] nums2, int n) {
 3         if (nums1 == null || nums2 == null) {
 4             return;
 5         }
 6         if (n == 0) {
 7             return;
 8         }
 9         int i = m - 1;
10         int j = n - 1;
11         int index = m + n -1;
12         while (i != -1 && j != -1) {
13             if (nums1[i] > nums2[j]) {
14                 nums1[index--] = nums1[i--];//注意 这里可以直接在数字角标内部进行自减!
16             } else {
17                 nums1[index--] = nums2[j--];
19             }
21         }
22         while (j != -1) {
23             nums1[index--] = nums2[j--];
25         }
26     }
27 }
原文地址:https://www.cnblogs.com/jiangchen/p/5779247.html