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 and n respectively.

解法一:从前往后,小的排最前。每次插入一个B,A需要整体后移。

class Solution {
public:
void merge(int A[], int m, int B[], int n) {
int indA = ;
int indB = ;
int shift = ;
while(indA < m+shift && indB < n)
{
if(A[indA] <= B[indB])
indA++;
else
{
for(int i = m-+shift; i >= indA; i --)
A[i+] = A[i];
shift ++;
A[indA++] = B[indB++];
}
}
if(indA >= m+shift)
{
while(indB < n)
A[indA++] = B[indB++];
}
}
};

解法二:从后往前,大的排最后。不需要多余的移动。

class Solution {
public:
void merge(int A[], int m, int B[], int n) {
int indA = m-;
int indB = n-;
for(int i = m+n-; i >= ; i --)
{
if(indA < )
{// A finished
A[i] = B[indB --];
}
else if(indB < )
{// B finished
A[i] = A[indA --];
}
else
{
if(A[indA] >= B[indB])
A[i] = A[indA --];
else
A[i] = B[indB --];
}
}
}
};

【LeetCode】88. Merge Sorted Array (2 solutions)的更多相关文章

  1. 【LeetCode】88. Merge Sorted Array 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 新建数组 日期 题目地址:https://leetc ...

  2. 【LeetCode】88 - Merge Sorted Array

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:Yo ...

  3. 【一天一道LeetCode】#88. Merge Sorted Array

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...

  4. 【easy】88. Merge Sorted Array 合并两个有序数组

    合并两个有序的list 把排序好的nums2插入nums1中,假设nums1这个vector的空间永远是够的 思路:倒序!! class Solution { public: void merge(v ...

  5. LeetCode OJ 88. Merge Sorted Array

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:Yo ...

  6. Leetcode No.88 Merge Sorted Array(c++实现)

    1. 题目 1.1 英文题目 You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and ...

  7. 【LeetCode】108. Convert Sorted Array to Binary Search Tree 解题报告 (Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...

  8. 【leetcode❤python】 88. Merge Sorted Array

    #-*- coding: UTF-8 -*-class Solution(object):    def merge(self, nums1, m, nums2, n):        "& ...

  9. 【LeetCode】108. Convert Sorted Array to Binary Search Tree

    Problem: Given an array where elements are sorted in ascending order, convert it to a height balance ...

随机推荐

  1. UCN(User-Centric Networks,用户中心网络)

    UCN(User-Centric Networks,以用户为中心的网络)是下一代移动通信网络(5G)的发展方向,目前尚处于初级发展阶段.2016年11月,IEEE SDN研究组(聚焦研发SDN.NFV ...

  2. Informatica 常用组件Filter之三 创建FIL

    在 Designer 中,切换到 Mapping Designer 并打开映射. 选择"转换-创建". 选择"过滤器转换",然后输入新的转换名称.过滤器转换的命 ...

  3. 算法导论第九章 第K顺序统计量

    1.第K顺序统计量概念 在一个由n个元素组成的集合中,第k个顺序统计量是该集合中第k小的元素.例如,最小值是第1顺序统计量,最大值是第n顺序统计量. 2.求Top K元素与求第K顺序统计量不同 Top ...

  4. c++学习之友元

    最近工作好累呀,晚上总是失眠,自学c++的步骤都放慢了,本来之前看c++ primer的,结果这本书讲的太细节了,初学者不是很好把握.所以我又重新找了个教程,比较适合初学者.今天学习到友元函数和友元类 ...

  5. go语言基础之结构体成员的使用普通变量

    1.结构体成员的使用普通变量 示例: package main //必须有个main包 import "fmt" //定义一个结构体类型 type Student struct { ...

  6. 初识EntityFramework6

    初识EntityFramework6 什么是EF? EF是一种ORM(Object-relational mapping)框架,它能把我们在编程时使用对象映射到底层的数据库结构.比如,你可以在数据库中 ...

  7. 根据百度API获得经纬度,然后根据经纬度在获得城市信息

    package com.pb.baiduapi; import java.io.BufferedReader; import java.io.IOException; import java.io.I ...

  8. [Backbone]3. More detail on View

    Change the AppointmentView to have a top-level li tag (instead of the default div tag). var Appointm ...

  9. 远程连接Ubuntu桌面配置

    1.打开终端:依次安装 sudo apt-get install xrdp sudo apt-get install vnc4server tightvncserver sudo apt-get in ...

  10. Node使用淘宝 NPM 镜像

    npm install -g cnpm --registry=https://registry.npm.taobao.org之后可以通过cnpm来安装node模块cnpm install [name]