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需要整体后移。

  1. class Solution {
  2. public:
  3. void merge(int A[], int m, int B[], int n) {
  4. int indA = ;
  5. int indB = ;
  6. int shift = ;
  7. while(indA < m+shift && indB < n)
  8. {
  9. if(A[indA] <= B[indB])
  10. indA++;
  11. else
  12. {
  13. for(int i = m-+shift; i >= indA; i --)
  14. A[i+] = A[i];
  15. shift ++;
  16. A[indA++] = B[indB++];
  17. }
  18. }
  19. if(indA >= m+shift)
  20. {
  21. while(indB < n)
  22. A[indA++] = B[indB++];
  23. }
  24. }
  25. };

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

  1. class Solution {
  2. public:
  3. void merge(int A[], int m, int B[], int n) {
  4. int indA = m-;
  5. int indB = n-;
  6. for(int i = m+n-; i >= ; i --)
  7. {
  8. if(indA < )
  9. {// A finished
  10. A[i] = B[indB --];
  11. }
  12. else if(indB < )
  13. {// B finished
  14. A[i] = A[indA --];
  15. }
  16. else
  17. {
  18. if(A[indA] >= B[indB])
  19. A[i] = A[indA --];
  20. else
  21. A[i] = B[indB --];
  22. }
  23. }
  24. }
  25. };

【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. css3 实现圆角边框的border-radius属性和实现阴影效果的box-shadow属性

    首先我要介绍的是border-radius属性,它的作用是实现圆角边框,其中border-radius:20px;表示,一个’体‘四个角都圆滑20px,其值如果为100px那么圆角度则为最高,如果是正 ...

  2. Ceph rgws客户端验证

    修改/etc/ceph/ceph.conf文件,加入rados gw监听的端口 [client.rgw.rgws] rgw_frontends = "civetweb port=80&quo ...

  3. 《Small Memory Software:Patterns For System With Limited Memory》读书笔记

    原文地址:http://blog.csdn.net/jinzhuojun/article/details/13297447 虽然摩尔定律让我们的计算机硬件得以以指数速度升级,但反摩尔定律又不断消减这些 ...

  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. 反射 Reflect Modifier 修饰符工具类

    在查看反射相关的Class.Field .Constructor 等类时,看到他们都有这样一个方法:getModifiers():返回此类或接口以整数编码的 Java 语言修饰符.如需要知道返回的值所 ...

  6. 在asp.net中使用jQuery实现类似QQ网站的图片切割效果

    今天要给大家介绍一个asp.net结合jQuery来切割图片的小程序,原理很简单,大致流程是: 加载原图 --> 用矩形框在原图上选取区域并将选取的顶点坐标和矩形尺寸发送至服务器 --> ...

  7. Android -- 程序启动画面 Splash

    很多应用都会有一个启动界面.欢迎画面慢慢隐现,然后慢慢消隐. 我的方式是使用两个Activity,程序启动时候load第一张Activity,然后由tick触发N秒钟后startActivity另外一 ...

  8. [Algorithm] Universal Value Tree Problem

    A unival tree (which stands for "universal value") is a tree where all nodes under it have ...

  9. 浅谈JavaScript框架设计

    在这个js框架随处乱跑的时代,你是否考虑过写一个自己的框架?下面的内容也许会有点帮助. 一个框架应该包含哪些内容? 1.语言扩展 大部分现有的框架都提供了这部分内容,语言扩展应当是以ECMAScrip ...

  10. Hibernate(七)一对一映射

    一.创建数据库表 --班级表 create table grade ( gid number primary key, --班级ID gname ), --班级名称 gdesc ) --班级介绍 ); ...