首页 > 代码库 > merge-sorted-array——合并两个有序数组
merge-sorted-array——合并两个有序数组
Given two sorted integer arrays A and B, merge B into A as one sorted array.
Note:
You may assume that A has enough space to hold additional elements from B. The number of elements initialized in A and B are m and n respectively.
归并排序
1 class Solution { 2 public: 3 void merge(int A[], int m, int B[], int n) { 4 vector<int> v(m+n,0); 5 int i=0,j=0,k=0; 6 while(i<m&&j<n){ 7 if(A[i]<B[j]) 8 v[k++]=A[i++]; 9 else 10 v[k++]=B[j++]; 11 12 } 13 while(i<m){ 14 v[k++]=A[i++]; 15 16 } 17 while(j<n){ 18 v[k++]=B[j++]; 19 } 20 for(i=0;i<m+n;i++) 21 A[i]=v[i]; 22 } 23 };
merge-sorted-array——合并两个有序数组
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。