LeetCode_Merge Sorted Array

一.题目

Merge Sorted Array

Total Accepted: 52295 Total Submissions: 173663My
Submissions

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.

Show Tags

Have you met this question in a real interview?

Yes

No

Discuss

二.解题技巧

这道题是一道比较水的题,没有很复杂的算法,只是一个技巧问题。如果单纯得考虑从小到大地将两个数组进行合并的话,每次在num1中插入一个数的话,需要将后面的元素都向后移动一位,这样,整个处理过程的时间复杂度为O(m*n)。

由于两个数组的元素的个数是知道的,同时,合并后的数组也是递增排序的,也就是说,排序之后的数组的最大值是放在最后面的,因此,我们可以从后往前遍历,也就是将最大值放在第一个数组的m+n-1位置,然后将次最大值放在m+n-2位置,依次类推,这样在将元素放置到合适位置的时候,就不需要移动元素,这个方法的时间复杂度为O(m+n)。

三.实现代码

#include <iostream>
#include <vector>

using std::vector;

class Solution
{
public:
    void merge(vector<int>& nums1, int m, vector<int>& nums2, int n)
    {
        int ResultIndex = m + n - 1;
        m--;
        n--;
        while (m >= 0 || n >= 0)
        {
            if (m < 0)
            {
                nums1[ResultIndex--] = nums2[n--];
                continue;
            }

            if (n < 0)
            {
                nums1[ResultIndex--] = nums1[m--];
                continue;
            }

            if (m >= 0 && n >= 0)
            {
                if (nums1[m] > nums2[n])
                {
                    nums1[ResultIndex--] = nums1[m--];
                    continue;
                }
                else
                {
                    nums1[ResultIndex--] = nums2[n--];
                    continue;
                }
            }
        }

    }
};

四.体会

这道题只是一个技巧问题,有时从前往后进行处理的时候不太好操作的话,可以考虑从后往前处理。

版权所有,欢迎转载,转载请注明出处,谢谢

时间: 2024-11-02 08:14:07

LeetCode_Merge Sorted Array的相关文章

Remove Duplicates from Sorted Array II

Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorted array nums = [1,1,1,2,2,3], Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn'

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]Find Minimum in Rotated Sorted Array

Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). Find the minimum element. You may assume no duplicate exists in the array. 这道题是要求找出一个数组中的最小值,但是这个数组是在有序数组的基础上循环右移了K次. 提示可以用二分

[leetcode]Search in Rotated Sorted Array II

问题描述: Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. "Search in Rotated Sorted Array&q

LeetCode: Search in Rotated Sorted Array

LeetCode: Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, othe

Leetcode | Remove Duplicates from Sorted Array I &amp;&amp; II

Remove Duplicates from Sorted Array I Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memor

LeetCode OJ - Convert Sorted Array/List to Binary Search Tree

虚函数使用的时机 为什么虚函数不总是适用? 1. 虚函数有事会带来很大的消耗: 2. 虚函数不总是提供所需的行为: 3. 当我们不考虑继承当前类时,不必使用虚函数. 必须使用虚函数的情况: 1. 当你想删除一个表面上指向基类对象,实际却是指向派生类对象的指针,就需要虚析构函数. LeetCode OJ - Convert Sorted Array/List to Binary Search Tree,布布扣,bubuko.com LeetCode OJ - Convert Sorted Arra

26. Remove Duplicates from Sorted Array【easy】

26. Remove Duplicates from Sorted Array[easy] Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with consta

26. Remove Duplicates from Sorted Array【leetcode】,数组,array,java,算法

26. Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant mem