LeetCode | Merge Sorted Array
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 nums1and nums2 are m and n respectively.
Have you met this question in a real interview?
Yes
No
思路:这题没看懂意思其实,WA了两次,后来猜测是只取数组里面前一部分的值,即nums1整个数组只取前m个值,nums2只取前n个值,试了一下居然AC了,happy。
class Solution { public: void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) { vector<int> temp; temp.insert(temp.end(),nums1.begin(),nums1.end()); nums1.clear(); vector<int>::iterator p=temp.begin(); for(int i=0;i<m;i++) { nums1.push_back(p[i]); } vector<int> tmp; tmp.insert(tmp.end(),nums2.begin(),nums2.end()); nums2.clear(); vector<int>::iterator q=tmp.begin(); for(int i=0;i<n;i++) { nums2.push_back(q[i]); } nums1.insert(nums1.end(),nums2.begin(),nums2.end()); sort(nums1.begin(),nums1.end()); } };
版权声明:本文为博主原创文章,未经博主允许不得转载。
posted on 2015-06-09 00:45 Tob__yuhong 阅读(121) 评论(0) 编辑 收藏 举报