[LeetCode]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 m andn respectively.

【分析】

【代码】

/*********************************
*   日期:2015-01-05
*   作者:SJF0115
*   题目: 88.Merge Sorted Array
*   来源:https://oj.leetcode.com/problems/merge-sorted-array/
*   结果:AC
*   来源:LeetCode
*   博客:
**********************************/
#include <iostream>
using namespace std;

class Solution {
public:
    void merge(int A[], int m, int B[], int n) {
        if(n <= 0){
            return;
        }//if
        int indexA = 0,indexB = 0;
        while(indexA < m && indexB < n){
            // B数组元素插入A数组中
            if(A[indexA] >= B[indexB]){
                // 后移一位
                for(int i = m-1;i >= indexA;i--){
                    A[i+1] = A[i];
                }//for
                A[indexA] = B[indexB];
                m++;
                indexB++;
            }//if
            indexA++;
        }//while
        // 如果B数组还没有遍历完
        while(indexB < n){
            A[indexA++] = B[indexB++];
        }//while
    }
};

int main() {
    Solution solution;
    int A[] = {1,2,4,7,9};
    int B[] = {3,5,8,10,11,12};
    int m = 5;
    int n = 6;
    solution.merge(A,m,B,n);
    // 输出
    for(int i = 0;i < 11;i++){
        cout<<A[i]<<" ";
    }
    cout<<endl;
}
【代码二】

The idea is to go from the last indexes of both arrays, compare and put elements from either A or B to the final position, which
can easily get since we know that A have enough space to store them all and we know size of A and B. Please refer to the comments for details.

/*********************************
*   日期:2015-01-05
*   作者:SJF0115
*   题目: 88.Merge Sorted Array
*   来源:https://oj.leetcode.com/problems/merge-sorted-array/
*   结果:AC
*   来源:LeetCode
*   博客:
**********************************/
#include <iostream>
using namespace std;

class Solution {
public:
    void merge(int A[], int m, int B[], int n) {
        if(n <= 0){
            return;
        }//if
        int indexA = m-1,indexB = n-1,cur = m + n -1;
        // AB数组从后往前扫描
        while(indexA >= 0 && indexB >= 0){
            if(A[indexA] < B[indexB]){
                A[cur--] = B[indexB--];
            }//if
            else{
                A[cur--] = A[indexA--];
            }//else
        }//while
        // 如果B数组没有遍历完
        while(indexB >= 0){
            A[cur--] = B[indexB--];
        }//while
    }
};

int main() {
    Solution solution;
    int A[] = {1,2,4,7,9};
    int B[] = {3,5,8,10,11,12};
    int m = 5;
    int n = 6;
    solution.merge(A,m,B,n);
    // 输出
    for(int i = 0;i < 11;i++){
        cout<<A[i]<<" ";
    }
    cout<<endl;
}

时间: 2024-09-21 21:10:02

[LeetCode]88.Merge Sorted Array的相关文章

[LeetCode]108.Convert Sorted Array to Binary Search Tree

[题目] Given an array where elements are sorted in ascending order, convert it to a height balanced BST. [分析] 二分法,以中间元素i为根节点[start,i-1]递归构建左子树,[i+1,end]递归构建右子树 [代码] /********************************* * 日期:2014-12-28 * 作者:SJF0115 * 题目: 108.Convert Sorte

[LeetCode]109.Convert Sorted List to Binary Search Tree

[题目] Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. [分析] 无 [代码] 在下面超时的代码上进行改进:把链表先转换为vector再进行操作. [LeetCode]108.Convert Sorted Array to Binary Search Tree /*******************************

LeetCode 23 Merge k Sorted Lists(合并K个已排序链表)

翻译 合并K个已排序的链表,并且将其排序并返回. 分析和描述其复杂性. 原文 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 代码 我们采用分治的方法来解决这个问题,其有K个链表,不断将其划分(partition),再将其归并(merge). 划分的部分并不难,将其不断分成两部分,但是需要注意的是可能出现start和end相等的情况,这时候就直接r

[LeetCode]81.Search in Rotated Sorted Array II

[题目] Search in Rotated Sorted Array II  Total Accepted: 3749 Total Submissions: 12937My Submissions 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 f

[LeetCode]154.Find Minimum in Rotated Sorted Array II

[题目] Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 mi

[LeetCode]153.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. [分析] 剑指Offer中有这道题目的分析.这是一道二分查找的变形的题目. 旋转之后的数组

[LeetCode]33.Search in Rotated Sorted Array

[题目] Search in Rotated Sorted Array  Total Accepted: 5827 Total Submissions: 20925My Submissions 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

[LeetCode]80.Remove Duplicates from Sorted Array II

[题目] Remove Duplicates from Sorted Array II  Total Accepted: 4460 Total Submissions: 15040My Submissions Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorted array A = [1,1,1,2,2,3], Your fun

LeetCode 26 Remove Duplicates from Sorted Array(从已排序数组中移除重复元素)

翻译 给定一个已排序的数组,删除重复的元素,这样每个元素只出现一次,并且返回新的数组长度. 不允许为另一个数组使用额外的空间,你必须就地以常量空间执行这个操作. 例如, 给定输入数组为 [1,1,2] 你的函数应该返回length = 2, 其前两个元素分别是1和2.它不关心你离开后的新长度. 原文 Given a sorted array, remove the duplicates in place such that each element appear only once and re