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. [Android Pro] Android中IntentService的原理及使用

    转载自:http://blog.csdn.net/ryantang03/article/details/8146154 在Android开发中,我 们或许会碰到这么一种业务需求,一项任务分成几个子任务 ...

  2. 为apache安装mod_wsgi的时候出现-fpic的问题

    1.为了在apache里跑python项目,需要安装模块mod_wsgi 2.但是由于yum只支持python2.6,所以通过yum install mod_wsgi方式安装的mod_wsgi是pyt ...

  3. Miscellaneos:版本控制、SVN、VSS

    ylbtech-Miscellaneos:版本控制.SVN.VSS 版本控制.SVN.VSS 1.A,版本控制返回顶部 1, 版本控制(Revision control)是一种软体工程技巧,籍以在开发 ...

  4. C/C++/Java 程序计时功能函数

    编写程序肯定要使用计时功能,来判断程序的执行时间.今天Google了一下,自己就梳理总结一下: (1)C/C++程序计时 C/C++中使用的计时函数是clock(). C语言中的头文件对应是#incl ...

  5. go语言基础之append扩容特点

    1.append扩容特点 示例: package main //必须有个main包 import "fmt" func main() { //如果超过原来的容量,通常以2倍容量扩容 ...

  6. 网站日志访问记录组件UserVisitLogsHelp开源了!

    之前在<一种基于自定义代码记录用户访问日志在Sharepoint网站的应用方法!>一文利用本人几年前的开发的UserVisitLogsHelp组件进行了网站用户访问日志记录,可用于网站分析 ...

  7. 导出DLLRegisterServer接口遇到的问题

    I'm trying to add DLLRegisterServer and DLLUnregisterServer entry points to an existing DLL that is ...

  8. 如何解决SPD的缓存问题

      SPD有时候文件被缓存住了,表现为文件的最后更改时间不对,或者本来文件已经被check in了,但是显示check out状态,而此时如果选择check in, 就会提示文件没有被check ou ...

  9. IDA Pro基本简介

    IDA Pro基本简介 IDA加载完程序后,3个立即可见的窗口分别为IDA-View,Named,和消息输出窗口(output Window). IDA图形视图会有执行流,Yes箭头默认为绿色,No箭 ...

  10. Http具体解释

    一.Http是位于网络层的超文本传输协议. 我们用浏览器浏览网页就是用的http协议实现的. 二.Http请求详细分析 我们能够用httpwatch工具对http进行抓包分析 下面就是请求www.si ...