[LeedCode OJ]#88 Merge Sorted Array

【 声明:版权所有,转载请标明出处,请勿用于商业用途。  联系信箱:[email protected]】

题目链接:https://leetcode.com/problems/merge-sorted-array/

题意:

给定两个已经排好序的数组,然后要你把第二个数组合并到第一个去

思路:

先用两个标记指向两个数组的第一个,然后一边比较,取小的放入新数组,一直移动标记直到最后去玩

class Solution
{
public:
    void merge(vector<int>& a, int m, vector<int>& b, int n)
    {
        vector<int> ans;
        int x = 0,y = 0;
        while(x<m&&y<n)//直到其中一个取完
        {
            if(a[x]<b[y])
                ans.push_back(a[x++]);
            else
                ans.push_back(b[y++]);
        }
        while(x<m)//b取完a没取完
            ans.push_back(a[x++]);
        while(y<n)//a取完b没取完
            ans.push_back(b[y++]);
        swap(ans,a);
    }
};

版权声明:本文为博主原创文章,如果转载,请注明出处

时间: 2024-11-06 09:39:46

[LeedCode OJ]#88 Merge Sorted Array的相关文章

Leet Code OJ 88. Merge Sorted Array [Difficulty: Easy]

题目: 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

LeetCode --- 88. 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 (size that is greater or equal to m + n) to hold additional elements from B. The number of elements init

leetCode 88. Merge Sorted Array 有序数组

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 n

88. Merge Sorted Array【leetcode】算法,java将两个有序数组合并到一个数组中

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 n

No.88 Merge Sorted Array

No.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

[leedcode 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 initi

88. Merge Sorted Array (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 init

88 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 (size that is greater or equal to m + n) to hold additional elements from B. The number of elements initialized in A and B are ma

【LeetCode】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 initi