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 number of elements initialized in nums1 and nums2 are m and n respectively.

问题描述:将两个有序数组合并成一个有序数组。

思路:创建第三个数组,将其它两个数组有序的插入第三个数组中。然后根据需求变化。

代码如下:

class Solution {
public:
    void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
        vector<int> result;
        
        int i= 0;
        int j=0;
        while( (i < m) && (j < n))
        {
            if(nums1[i] <= nums2[j])
            {
                result.push_back(nums1[i]);
                i++;
            }
            else
            {
                result.push_back(nums2[j]);
                j++;
            }
        }
        if(i < m)
        {
            for(;i < m; i++ )
            {
                result.push_back(nums1[i]);
            }
        }
        
        if(j < n)
        {
            for(;j < n; j++)
            {
                result.push_back(nums2[j]);
            }
        }
        
        swap(result,nums1);
    }
};

2016-08-05 23:36:14

时间: 2024-10-10 21:51:28

leetCode 88. Merge Sorted Array 有序数组的相关文章

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(合并有序数组)

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

LeetCode 88 Merge Sorted Array(合并排序数组)(*)

翻译 给定两个排序的整型数组nums1和nums2,将nums2合并到nums1成一个排序数组. 批注: 你可以假设nums1中有足够的空间(空间大于或等于m+n)来存放来自nums2的额外元素. nums1和nums2的初始空间分别是m和n. 原文 Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: You may assume that nums1

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 init

[LeetCode] 88. Merge Sorted Array Java

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

[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

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

&lt;LeetCode OJ&gt; Merge Sorted Array【88】

88. Merge Sorted Array My Submissions Question Total Accepted: 80830 Total Submissions: 274120 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 sp

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