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

Note:

  • The number of elements initialized in nums1 and nums2 are m and n respectively.
  • You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2.

Example:

Input:
nums1 = [1,2,3,0,0,0], m = 3
nums2 = [2,5,6], n = 3 Output: [1,2,2,3,5,6]

给两个有序的数组 A 和 B,把 B 合并到 A,变成一个数组,假定 A 有足够的空间。

可以考虑新建立一个m + n的数组,然后从两个数组的开头取各取一个元素进行比较,把小的放进新数组,然后在循环这个过程,直到结束。

好的方法是不用新建立数组,而是直接在A 数组上写入,因为 A 足够大,可从两个数组的最大数也就是最后一个数开始比较,大的写入A[m + n -1],然后循环这个过程。如果 B 的元素写完了,A 剩下的元素正好在正取的位置,不用写了。如果 A 的元素都取完了,那剩下的 B 的元素可一次全部写进 A。

Java:

public class Solution {
public void merge(int A[], int m, int B[], int n) { while(m > 0 && n > 0){
if(A[m-1] > B[n-1]){
A[m+n-1] = A[m-1];
m--;
}else{
A[m+n-1] = B[n-1];
n--;
}
} while(n > 0){
A[m+n-1] = B[n-1];
n--;
}
}
}

Java:

public void merge(int A[], int m, int B[], int n) {
int i = m - 1;
int j = n - 1;
int k = m + n - 1; while (k >= 0) {
if (j < 0 || (i >= 0 && A[i] > B[j]))
A[k--] = A[i--];
else
A[k--] = B[j--];
}
}

Python:

class Solution:
# @param A a list of integers
# @param m an integer, length of A
# @param B a list of integers
# @param n an integer, length of B
# @return nothing
def merge(self, A, m, B, n):
last, i, j = m + n - 1, m - 1, n - 1 while i >= 0 and j >= 0:
if A[i] > B[j]:
A[last] = A[i]
last, i = last - 1, i - 1
else:
A[last] = B[j]
last, j = last - 1, j - 1 while j >= 0:
A[last] = B[j]
last, j = last - 1, j - 1 if __name__ == "__main__":
A = [1, 3, 5, 0, 0, 0, 0]
B = [2, 4, 6, 7]
Solution().merge(A, 3, B, 4)
print(A)

  

Python:

class Solution:
def merge(self, nums1, m, nums2, n):
"""
:type nums1: List[int]
:type m: int
:type nums2: List[int]
:type n: int
:rtype: void Do not return anything, modify nums1 in-place instead.
"""
while m > 0 and n > 0:
if nums1[m-1] > nums2[n-1]:
nums1[m+n-1] = nums1[m-1]
m -= 1
else:
nums1[m+n-1] = nums2[n-1]
n -= 1
if n > 0:
nums1[:n] = nums2[:n]

C++:

class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
int i = m + n;
while (m > 0 && n > 0) {
if (nums1[m - 1] > nums2[n - 1]) {
nums1[i - 1] = nums1[m - 1];
--m;
} else {
nums1[i - 1] = nums2[n - 1];
--n;
}
--i;
} while (n > 0) {
nums1[i - 1] = nums2[n - 1];
--n;
--i;
}
}
};

 

类似题目:

[LeetCode] 21. Merge Two Sorted Lists 合并有序链表

[LeetCode] 23. Merge k Sorted Lists 合并k个有序链表

All LeetCode Questions List 题目汇总

[LeetCode] 88. Merge Sorted Array 合并有序数组的更多相关文章

  1. [leetcode]88. Merge Sorted Array归并有序数组

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

  2. Leetcode#88. Merge Sorted Array(合并两个有序数组)

    题目描述 给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组. 说明: 初始化 nums1 和 nums2 的元素数量分别为 m ...

  3. [LeetCode] 88. Merge Sorted Array 混合插入有序数组

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

  4. Leetcode 88 Merge Sorted Array STL

    合并有序数组 时间复杂度O(m+n) 该算法来自各种算法与数据结构书,写得已经烂得不能再烂了,这个应该是最短的代码了吧,不知如何归类 class Solution { public: void mer ...

  5. LeetCode 88. Merge Sorted Array(合并有序数组)

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

  6. 【LeetCode】Merge Sorted Array(合并两个有序数组)

    这道题是LeetCode里的第88道题. 题目描述: 给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组. 说明: 初始化 nu ...

  7. LeetCode 088 Merge Sorted Array 合并两个有序数组

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

  8. leetCode 88.Merge Sorted Array (合并排序数组) 解题思路和方法

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

  9. LeetCode 88 Merge Sorted Array

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

随机推荐

  1. robot framework 笔记(四),使用时遇到的问题

    背景: 使用rf遇到的一些问题汇总 一:跑WEBUI的时候报错: [ WARN ] Keyword 'Capture Page Screenshot' could not be run on fail ...

  2. 【Selenium-WebDriver实战篇】Eclipse上的maven使用JsonObject时,添加依赖后无法自动下载相应的jar包

    ==================================================================================================== ...

  3. Browsersync 省时浏览器同步测试工具,浏览器自动刷新,多终端同步

    官网地址 http://www.browsersync.cn/ 1.安装 BrowserSync npm install -g browser-sync 2.启动 BrowserSync // --f ...

  4. (尚018-第二章2.1)Vue使用vue-cli创建模板项目

    2.1.1 1)vue-cli是官方提供的脚手架工具(注意:脚手架本身是个库) 2)github:https://github.com/vuejs/vue-cli 3)作用:从https://gith ...

  5. [RN] React Native 使用 FlatList 实现九宫格布局 GridList

    React Native 使用 FlatList 实现九宫格布局 先看图片演示实例: 本文以图片列表为例,实现九宫格布局! 主要有两种方法: 1)方法一: 利用FlatList的 numColumns ...

  6. 计蒜客 39270.Angel's Journey-简单的计算几何 ((The 2019 ACM-ICPC China Shannxi Provincial Programming Contest C.) 2019ICPC西安邀请赛现场赛重现赛

    Angel's Journey “Miyane!” This day Hana asks Miyako for help again. Hana plays the part of angel on ...

  7. avalon用background-image不起作用,怎么来选取前几个的图片进行渲染

    <span ms-css="{backgroundImage: 'url('+item.image + ')'}" ms-for="($index,item) in ...

  8. hdoj - 1248 寒冰王座

    Problem Description 不死族的巫妖王发工资拉,死亡骑士拿到一张N元的钞票(记住,只有一张钞票),为了防止自己在战斗中频繁的死掉,他决定给自己买一些道具,于是他来到了地精商店前.死亡骑 ...

  9. Intellij IDEA 智能补全的 10 个姿势,太牛逼了。。

    一年多前,栈长那时候刚从 Eclipse 转型 IDEA 成功,前面转了好多次,都是失败史,都是泪..后面我就在微信公众号 "Java技术栈" 写了这篇文章:Intellij ID ...

  10. hdu1702 ACboy needs your help again![简单STL 栈 队列]

    目录 题目地址 题干 代码和解释 参考 题目地址 hdu1702 题干 代码和解释 本题很简单,只要掌握STL stack和STL vector的语法即可作答.记录本题是为了记录STL vector的 ...