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. Python3从零开始爬取今日头条的新闻【一、开发环境搭建】

    Python3从零开始爬取今日头条的新闻[一.开发环境搭建] Python3从零开始爬取今日头条的新闻[二.首页热点新闻抓取] Python3从零开始爬取今日头条的新闻[三.滚动到底自动加载] Pyt ...

  2. python中的矩阵、多维数组

    2. 创建一般的多维数组 import numpy as np a = np.array([1,2,3], dtype=int)  # 创建1*3维数组   array([1,2,3]) type(a ...

  3. Web版记账本开发记录(三)开发过程遇到的问题小结2

    问题1,获得当前时间 Date d = new Date(); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ...

  4. unittest中的测试固件

    运行下面的两段代码,看看有什么不同? 第一段: import unittest from selenium import webdriver class F2(unittest.TestCase): ...

  5. 使用AOP实现方法执行时间和自定义注解

    环境:IDEA2018+JDK1.8+SpringBoot 第一步:在pom文件中引入依赖(度娘有很多(*^▽^*)): <!--引入AOP的依赖--><dependency> ...

  6. url中文参数乱码问题

    1.参数乱码: js: var url = $$pageContextPath + "iecp/ads/heilanAnalogCurve.do?pointCode=" + get ...

  7. Mesos源码分析(1): Mesos的启动过程总论

  8. 实战深度学习OpenCV(一):canny边缘检测

    利用canny边缘检测,我们可以很好地得到哦一个图像的轮廓,下面是基于C++的,这是我们通过这段代码得到的结果: #include "pch.h" #include <ios ...

  9. [Swift]LeetCode244.最短单词距离 II $ Shortest Word Distance II

    This is a follow up of Shortest Word Distance. The only difference is now you are given the list of ...

  10. [Swift]LeetCode791. 自定义字符串排序 | Custom Sort String

    S and T are strings composed of lowercase letters. In S, no letter occurs more than once. S was sort ...