验证中...
私信发送成功
合并两个排序数组
原始数据 复制代码
/*
问题:
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.
Note:
You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold
additional elements from nums2. The number of elements initialized in nums1 and nums2 are m
and n respectively.
*/
/*
解答:
题目简单,注意理解题意。
*/
#include<iostream>
#include<vector>
#include<set>
using namespace std;
class Solution{
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n)
{
int i;
multiset<int> help(nums1.begin(), nums1.begin()+m);
multiset<int>::iterator it;
for(i=0; i<n; i++)
help.insert(nums2[i]);
nums1.clear();
it = help.begin();
while(it != help.end())
{
nums1.push_back(*it);
it++;
}
}
};
int main()
{
int i,in;
vector<int> m;
vector<int> n;
for(i=0; i<=3; i++)
{
cin>>in;
m.push_back(in);
}
for(i=0; i<=3; i++)
{
cin>>in;
n.push_back(in);
}
Solution sol;
sol.merge(m, m.size(), n, n.size());
for(i=0; i<m.size(); i++)
cout<<m[i]<<' ';
cout<<endl;
return 0;
}

评论列表( 0 )

你可以在登录后,对此项目发表评论