leetcode 88. Merge Sorted Array

来源:互联网 发布:淘宝禁止开店 编辑:程序博客网 时间:2024/06/05 03:02
88. 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 nums1 and nums2 are m and n respectively.

来源: https://leetcode.com/problems/merge-sorted-array/description/

class Solution {public:   void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {        vector<int> nums0;        for (int i = 0; i <m ; ++i) {            //复制一份到nums0            nums0.push_back(nums1[i]);        }        //在nums1上操作        for (int i=0,j=0,k=0;(j<m)||(k<n );) {            if ( (j < m) && (!(k < n)|| (nums0[j]<= nums2[k] ) ) )  nums1[i++]=nums0[j++];            if ( (k < n) && (!(j < m)|| (nums0[j] > nums2[k] ) ) )  nums1[i++]=nums2[k++];        }    }};