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. Codeforces E. Weakness and Poorness(三分最大子列和)

    题目描述: E. Weakness and Poorness time limit per test 2 seconds memory limit per test 256 megabytes inp ...

  2. shell脚本中向hive动态分区插入数据

    在hive上建表与普通分区表创建方法一样: CREATE TABLE `dwa_m_user_association_circle`( `device_number` string, `oppo_nu ...

  3. pyinstaller打包多个py文件和去除cmd黑框

    1.打包多个py文件并且去除cmd黑框 格式:pyinstaller.exe -F 路径\文件名.py空格路径\文件名.py空格--noconsole pyinstaller.exe -F ui.py ...

  4. 长为N的数组,元素范围是0-N-1,其中只有一个数是重复的,找出这个重复元素

    思路:如果限制空间复杂度为O(1),我们就无法采用哈希表的方法去求解.题目中数组中所以数字都在范围[0, N-1],因此哈希表的大小为N即可.因此我们实际要做的就是对N个范围为0到N-1的数进行哈希, ...

  5. GSM/GPRS模块 AT指令集C语言编程——基于有方M660+和MSP430单片机

    GSM/GPRS芯片是手机中负责收发短信.拨打电话以及访问GPRS网络的核心器件.有方M660+为深圳有方公司生产的一款超小封装的GSM/GPRS工业无线模块,可以提供高品质的语音.短信.数据业务等功 ...

  6. 按值传递与按值引用详解(java版)

    http://blog.csdn.net/zzp_403184692/article/details/8184751

  7. 洛谷 P2341 [HAOI2006]受欢迎的牛 题解

    今天学了强连通分量的Tarjan算法,做了这道类似于板子题的题(尽管我调了1.5h).主要的思路是用Tarjan缩点之后,求每个点的入度(实际上是出度,因为我是反着连边的).如果 有且只有一个点的入度 ...

  8. windbg命令行选项

    我们不仅可以通过GUI的方式使用Windbg,还可以通过命令行的方式使用它,且在有些需求和使用场景下,只能使用命令行模式  windbg命令行使用以下语法: windbg [ -server Serv ...

  9. 【luoguP5091】【模板】欧拉定理

    题目链接 欧拉定理: 当\(a\),\(m\)互质时,\(a^{\phi(m)}\equiv 1 (mod ~ m)\) 扩展欧拉定理: 当\(B>\phi(m)\)时,\(a^B\equiv ...

  10. mysql 引擎类型

    innodb: 可靠的事物处理引擎,不支持全文搜索 memeory: 数据存储在内存,速度很快 myisam: 性能极高的引擎,支持全文本搜索,但不支持事物