1: class Solution {
2: public:
3: void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
4: int i = m-1;
5: int j = n-1;
6: int k = m+n-1;
7: while (i >= 0 && j >= 0) {
8: if (nums1[i] > nums2[j]) {
9: nums1[k--] = nums1[i--];
10: } else {
11: nums1[k--] = nums2[j--];
12: }
13: }
14: while (j >= 0) nums1[k--] = nums2[j--];
15: }
16: };
Tuesday, August 16, 2016
88. Merge Sorted Array
The tricky part is we need to start from the end of arrays such that the largest will be allocated in the end.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment