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. 语义后承(semantic consequence),句法后承(syntactic consequence),实质蕴含(material implication / material conditional)

    作者:罗心澄链接:https://www.zhihu.com/question/21191299/answer/17469774来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...

  2. java静态代码分析工具infer

    infer是一个静态代码分析工具,探测bugs. 主要支持Java.C/C++ 安装:brew install infer 在线展示:https://codeboard.io/projects/115 ...

  3. 浅析Linux线程调度

    在Linux中,线程是由进程来实现,线程就是轻量级进程( lightweight process ),因此在Linux中,线程的调度是按照进程的调度方式来进行调度的,也就是说线程是调度单元.Linux ...

  4. Remove Duplicates from Sorted List leetcode java

    题目: Given a sorted linked list, delete all duplicates such that each element appear only once. For e ...

  5. 判断 iframe 是否加载完毕

    我能想到的有以下几种方式: 方法一.jQuery load() var frm = document.getElementById('myiframe'); $(frm).load(function( ...

  6. java.lang.ClassNotFoundException: org.apache.commons.beanutils.DynaBean

    项目报错:java.lang.ClassNotFoundException: org.apache.commons.beanutils.DynaBean 解决方法: 在pom.xml中加入如下依赖: ...

  7. Spark Streaming事务处理彻底掌握

    本篇文章主要从二个方面展开: 一.Exactly Once 二.输出不重复 事务: 银行转帐为例,A用户转账给B用户,B用户可能收到多笔钱,如何保证事务的一致性,也就是说事务输出,能够输出且只会输出一 ...

  8. 微博推荐算法学习(Weibo Recommend Algolrithm)

    原文:http://hijiangtao.github.io/2014/10/06/WeiboRecommendAlgorithm/ 基础及关联算法 作用:为微博推荐挖掘必要的基础资源.解决推荐时的通 ...

  9. php各版本下载

    Index of /php5/ File name File size Date Parent directory/ - - pecl-5.0.0-Win32.zip 954306 13-Jul-20 ...

  10. java基础-类变量,类方法