import java.util.Arrays;

/**
* Source : https://oj.leetcode.com/problems/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.
*/
public class MergeSortedArray { /**
* 将数组B合并到数组A,数组A空间足够大能放得下B
* 合并两个排序的数组,使用两个指针分别指向两个数组,然后比较大小将较小的放到数组A中,
* 如果把小的放在前面可能回把A中的元素覆盖(数组排序是升序的)
*
* 所以可以考虑从两个数组的末尾开始合并,将较大的元素放在A数组的最后
*
* 如果任意一个数组遍历完了,把B中剩下的元素依次插入A中
* @param a
* @param b
* @return
*/
public int[] merge (int[] a, int m, int[] b, int n) {
int pa = m - 1;
int pb = n - 1;
int pc = m + n - 1;
while (pa > -1 && pb > -1) {
if (a[pa] > b[pb]) {
a[pc--] = a[pa--];
} else {
a[pc--] = b[pb--];
}
}
while (pb > -1) {
a[pc--] = b[pb--];
}
return a;
} public static void main(String[] args) {
MergeSortedArray mergeSortedArray = new MergeSortedArray();
int[] a = new int[]{1,5,9,0,0,0,0};
int[] b = new int[]{2,3,8}; System.out.println(Arrays.toString(mergeSortedArray.merge(a, 3, b, 3)));
}
}

leetcode — merge-sorted-array的更多相关文章

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

    Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume tha ...

  2. [Leetcode] Merge Sorted Array (C++)

    我在Github上新建了一个解答Leetcode问题的Project, 大家可以参考, 目前是Java 为主,里面有leetcode上的题目,解答,还有一些基本的单元测试,方便大家起步. 题目: Gi ...

  3. [leetcode]Merge Sorted Array @ Python

    原题地址:https://oj.leetcode.com/problems/merge-sorted-array/ 题意:Given two sorted integer arrays A and B ...

  4. [LeetCode] Merge Sorted Array

    Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume tha ...

  5. [Leetcode] merge sorted array 合并数组

    Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assume th ...

  6. LeetCode Merge Sorted Array 合并已排序的数组

    void merge(int A[], int m, int B[], int n) { int *a=A,*b=B; ,j=; ||m==){ //针对特殊情况,比如A或B中无元素的情况 & ...

  7. leetcode - Merge Sorted Array (run time beats 100.00% of cpp submissions.)

    /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...

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

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

  9. 【LeetCode练习题】Merge Sorted Array

    Merge Sorted Array Given two sorted integer arrays A and B, merge B into A as one sorted array. Note ...

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

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

随机推荐

  1. C++入门笔记(一)零碎基础知识

    零碎基础知识 一.创建和运行程序 1.使用文本编辑器编写程序,保存为文件,该文件就叫源代码. 2.编译源代码:运行一个程序,将源代码翻译为主机使用的内部语言----机器语言.包含了 编译后程序的文件就 ...

  2. rhel 7安装Mysql

    rhel7安装mysql服务 环境: 1)rhel 7虚拟机 2)配置完163网络yum源,并且保证网络通畅 安装过程: 1) 安装Mysql和Mysql-devel 命令:yum install m ...

  3. 记一次非常规方法对接硬件设备(Grason Stadler GSI 61)

    Grason Stadler GSI 61 电测听设备 (写下设备的名字, 希望别人遇坑可以搜到) 对接说明 设备厂家提供自带的软件,但是没有找到接口说明.我们需要获取设备发送过来的数据. 厂家提供的 ...

  4. Do-Now—团队 冲刺博客六

    Do-Now-团队 冲刺博客六 作者:仇夏 前言 终于从四级的考试中解脱了(不过我觉得可能凉凉,呵呵),我们的APP制作也迎来了最后的两天. 自己觉得自己其实没有干成什么事情,代码什么的大都是队友们写 ...

  5. vue 学习小记

    vue 中有 data() .computed.methods.beforeRouteLeave.created等 具体的: data():定义一些文件中需要用到的变量,data中带return,是使 ...

  6. 数组的初始化&缩窄转换

    1.初始化: 初始化就是在声明变量的同时给变量赋值,而不是声明后再赋值. 先声明,后赋值: int a; //先声明,由于没有初始化,所以当前a的值是变量a创建前,相应的内存单元中保留的值,是未知的 ...

  7. python从入门到实践-8章函数

    #!/user/bin/env python# -*- coding:utf-8 -*- # 给形参指定默认值时,等号两边不要有空格 def function_name("parameter ...

  8. jstl使用中的错误----基于idea

    第一:首先正确将jstl.jar和standard.jar导入项目的lib目录下,注意两者的版本信息 第二: <%@ taglib prefix="c" uri=" ...

  9. linux端安装Anaconda,方便远端访问jupyter

    ipython notebook是一个基于浏览器的python数据分析工具,使用起来非常方便,具有极强的交互方式和富文本的展示效果.jupyter是它的升级版,它的安装也非常方便,一般Anaconda ...

  10. 最新版Navicat Premium12 中文破解版 安装激活

    对于PHPer 来说 Navicat Premium  简直就是神器有木有,反正我是这样觉得的,昨天刚更新了最新版本 Navicat Premium 12 ,官网是免费试用14 天的,肿么能行呢,我们 ...