[容易]合并排序数组

题目来源:http://www.lintcode.com/zh-cn/problem/merge-sorted-array-ii/

先贴一个错误的代码,通过了示例,但是没有通过全部案例:

 1 #include <iostream>
 2 #include <vector>
 3 using namespace std;
 4 
 5 class Solution {
 6 public:
 7     /**
 8      * @param A and B: sorted integer array A and B.
 9      * @return: A new sorted integer array
10      */
11     vector<int> mergeSortedArray(vector<int> &A, vector<int> &B) {
12         // write your code here
13         vector<int> C(A.size()+B.size());
14         int i,j,k;
15         for(i=0,j=0,k=0;k<A.size()+B.size();k++)
16         {
17             if(i<A.size()&&A.at(i)<=B.at(j))
18             {
19                 C.at(k)=A.at(i);
20                 i++;
21             }
22             else
23                 if(j<B.size())
24                 {
25                     C.at(k)=B.at(j);
26                     j++;
27                 }                
28         }
29         return C;
30     }
31 };
32 
33 int main()
34 {
35     Solution s;
36     int a[4]={1,2,3,4};
37     int b[4]={2,4,5,6};
38     vector<int> A(a,a+4);
39     vector<int> B(b,b+4);
40     vector<int> C(8);
41     C=s.mergeSortedArray(A,B);
42     for(int i=0;i<C.size();i++)
43         cout<<C.at(i)<<" ";
44 }

这道题类似归并排序中的:将有序的temp[s..m]和temp[m+1..t]归并为有序的TR[s..t]。

注意边界情况。比如1,5,6和2,3,4合并。合并好1,2,3,4,之后还要把5,6复制进来。

 1 //将有序的temp[s..m]和temp[m+1..t]归并为有序的TR[s..t]
 2 void Merge(int temp[],int TR[],int s,int m,int n)
 3 {
 4     int j,k,l;
 5     for(j=m+1,k=s;s<=m && j<=n;k++)//将temp中记录由小到大地并入TR
 6     {
 7         if (temp[s]<temp[j])
 8             TR[k]=temp[s++];
 9         else
10             TR[k]=temp[j++];
11     }
12     if(s<=m)
13     {
14         for(l=0;l<=m-s;l++)
15             TR[k+l]=temp[s+l];//将剩余的temp[s..m]复制到TR
16     }
17     if(j<=n)
18     {
19         for(l=0;l<=n-j;l++)
20             TR[k+l]=temp[j+l];//将剩余的temp[j..n]复制到TR
21     }
22 }

参考上面的代码,可以accept的程序如下:

 1 class Solution {
 2 public:
 3     /**
 4      * @param A and B: sorted integer array A and B.
 5      * @return: A new sorted integer array
 6      */
 7     vector<int> mergeSortedArray(vector<int> &A, vector<int> &B) {
 8         // write your code here
 9         vector<int> C(A.size()+B.size());
10         int i,j,k;
11         for(i=0,j=0,k=0;i<A.size()&&j<B.size();k++)
12             if(A.at(i)<=B.at(j))
13                 C.at(k)=A.at(i++);
14             else
15                 C.at(k)=B.at(j++);
16         if(i<A.size())
17         {
18             for(int t=0;t<A.size()-i;t++)
19                 C.at(k+t)=A.at(i+t);
20         }
21         if(j<B.size())
22         {
23             for(int t=0;t<B.size()-j;t++)
24                 C.at(k+t)=B.at(j+t);
25         }
26         return C;
27     }
28 };

可以Accept的正确程序2:

 1 class Solution {
 2 public:
 3     /**
 4      * @param A and B: sorted integer array A and B.
 5      * @return: A new sorted integer array
 6      */
 7     vector<int> mergeSortedArray(vector<int> &A, vector<int> &B) {
 8         vector<int> C;
 9         int i = 0, j = 0;
10         while (i < A.size() && j < B.size()) {
11             if (A[i] < B[j]) {
12                 C.push_back(A[i++]);
13             } else {
14                 C.push_back(B[j++]);
15             }
16         }
17         while (i < A.size()) {
18             C.push_back(A[i++]);
19         }
20         while (j < B.size()) {
21             C.push_back(B[j++]);
22         }
23         return C;
24     }
25 };
posted @ 2016-04-29 17:10  Pearl_zju  阅读(210)  评论(0编辑  收藏  举报