LintCode之合并排序数组

题目描述:

我的代码:

 1 public class Solution {
 2     /*
 3      * @param A: sorted integer array A
 4      * @param B: sorted integer array B
 5      * @return: A new sorted integer array
 6      */
 7     public int[] mergeSortedArray(int[] A, int[] B) {
 8         // write your code here
 9         int[] a = new int[A.length+B.length];
10         int p=0,q=0,count=0;
11         //当有一个数组全都重新排好序之后就退出循环
12         while(p<A.length && q<B.length) {
13             if(A[p] <= B[q]) {
14                 a[count++] = A[p];
15                 p++;
16             }else {
17                 a[count++] = B[q];
18                 q++;
19             }
20         }
21         if(p < A.length) {
22             for(int i=p; i<A.length; i++) {
23                 a[count++] = A[i];
24             }
25         }
26         if(q < B.length) {
27             for(int i=q; i<B.length; i++) {
28                 a[count++] = B[i];
29             }
30         }
31         return a;
32     }
33 }

 

posted @ 2017-10-27 20:50  zwt3  阅读(190)  评论(0编辑  收藏  举报