<LeetCode OJ> 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 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.

Subscribe to see which companies asked this question

Hide Tags

Array Two
Pointers

Show Similar Problems

//思路首先:题目说的很清楚nums1已经预留了足够的空间,但是只有前面m个数是实际要合并的数
//在处理的时候将nums1中前m个数取出到临时空间tmpnums1
//从tmpnums1和nums2中将较小者放进nums1
class Solution {
public:
    void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
        if(nums2.empty())
            return;
        vector<int> tmpnums1(nums1.begin(),nums1.begin()+m);
        int i=0,mm=0,nn=0;
        while(mm<m && nn<n)
        {
            if( tmpnums1[mm]<=nums2[nn])
            {
                nums1[i]=tmpnums1[mm];
                mm++;
            }else{
                nums1[i]=nums2[nn];
                nn++;
            }
            i++;
        }

        while(mm<m)
        {
            nums1[i]=tmpnums1[mm];
                mm++;
                i++;
        }
        while(nn<n)
        {
            nums1[i]=nums2[nn];
                nn++;
                i++;
        }
    }
};

别人家的解法:

非常好的思路:从两个数组的最后开始比较,直接放在了最后,因为已经知道了两个数组的长度,所以最后的坐标是 m+n-1 而每次比较后 n或m自减1

class Solution {
public:
    void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
        while(n)//数组2先放完即可终止,如果数组1都是较小者,则不动即可
        {//好巧妙
            if(!m || nums1[m-1]<=nums2[n-1])//m如果已经到0,说明nums1已经放完则只需nums2全部放入nums1中即可,如果m不为0,则根据大小判断即可
                nums1[m+n-1]=nums2[n-- -1];
            else
                nums1[m+n-1]=nums1[m-- -1];
        }
    }
};
时间: 2024-12-31 06:56:48

<LeetCode OJ> Merge Sorted Array【88】的相关文章

LeetCode OJ - Merge Sorted Array

原地归并. 下面是AC代码: 1 public void merge(int A[], int m, int B[], int n) { 2 3 int len = A.length; 4 //first copy m elements of A to the end of A 5 for(int i=m-1,j = len-1;i>=0;i--){ 6 A[j--] = A[i]; 7 } 8 //merge the A and B 9 int startA = len - m; 10 int

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

62. Search in Rotated Sorted Array【medium】

62. Search in Rotated Sorted Array[medium] 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, ot

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

LeetCode (1) 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 to hold additional elements from B. The number of elements initialized in A and B are m and n respectively. 刚开始根据归并排序写的多了一个中

&lt;LeetCode OJ&gt; Find Minimum in Rotated Sorted Array【153】

153. Find Minimum in Rotated Sorted Array My Submissions Question Total Accepted: 73048 Total Submissions: 209952 Difficulty: Medium 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

LeetCode: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 m

LeetCode 之 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 elem