Merge two given sorted integer array A and B into a new sorted integer array.

Example

A=[1,2,3,4]

B=[2,4,5,6]

return [1,2,2,3,4,4,5,6]

 class Solution {
/**
* @param A and B: sorted integer array A and B.
* @return: A new sorted integer array
* cnblogs.com/beiyeqingteng/
*/
public int[] mergeSortedArray(int[] A, int[] B) {
int[] newArray = new int[A.length + B.length]; int pointer = ; int pointerA = ;
int pointerB = ; while (pointerA < A.length || pointerB < B.length) {
if (pointerB == B.length || pointerA < A.length && A[pointerA] <= B[pointerB]) {
newArray[pointer] = A[pointerA];
pointerA++;
} else {
newArray[pointer] = B[pointerB];
pointerB++;
}
pointer++;
} return newArray;
}
}

转载请注明出处:cnblogs.com/beiyeqingteng/

Merge Two Sorted Arrays的更多相关文章

  1. Merge k Sorted Arrays【合并k个有序数组】【优先队列】

    Given k sorted integer arrays, merge them into one sorted array. Example Given 3 sorted arrays: [ [1 ...

  2. 怎样合并排序数组(How to merge 2 sorted arrays?)

    Question: We have 2 sorted arrays and we want to combine them into a single sorted array. Input: arr ...

  3. [Algorithm] 6. Merge Two Sorted Arrays

    Description Merge two given sorted integer array A and B into a new sorted integer array. Example A= ...

  4. Merge K Sorted Arrays

    This problem can be solved by using a heap. The time is O(nlog(n)). Given m arrays, the minimum elem ...

  5. 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays

    一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...

  6. 【转载】两个排序数组的中位数 / 第K大元素(Median of Two Sorted Arrays)

    转自 http://blog.csdn.net/zxzxy1988/article/details/8587244 给定两个已经排序好的数组(可能为空),找到两者所有元素中第k大的元素.另外一种更加具 ...

  7. LeetCode—— Median of Two Sorted Arrays

    Description: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the medi ...

  8. LeetCode——Merge k Sorted Lists

    Discription: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its ...

  9. LeetCode 4 Median of Two Sorted Arrays (两个数组的mid值)

    题目来源:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 an ...

随机推荐

  1. 在cmd中运行带包名的java程序

    例: 在 d 盘中的 zh.java 文件,zh.java文件中有package com.fanShe.....; 则命令是  javac -d . zh.java 要在中间加入 -d . 后面运行的 ...

  2. JS实现Ajax,Josn数据的序列化和反序列化---例: 省市区联动(包含get,post)

    服务器端相应JOSN数据   用到序列化和反序列化----命名空间using System.Web.Script.Serialization; public void ProcessRequest(H ...

  3. poj 3669 线段树成段更新+区间合并

    添加 lsum[ ] , rsum[ ] , msum[ ] 来记录从左到右的区间,从右到左的区间和最大的区间: #include<stdio.h> #define lson l,m,rt ...

  4. jQuery插件开发模式

    jQuery插件开发模式 软件开发过程中是需要一定的设计模式来指导开发的,有了模式,我们就能更好地组织我们的代码,并且从这些前人总结出来的模式中学到很多好的实践. 根据<jQuery高级编程&g ...

  5. The prefix "mvc" for element "mvc:annotation-driven" is not bound 的解决方法

    添加 xmlns:mvc="http://www.springframework.org/schema/mvc" http://www.springframework.org/sc ...

  6. Modular Query

    Solution F(L, R) 就是在A[L]在[L+1, R]内从左模到右. 首先应当注意到: 对$a, b > 0$ \[a \mod b \begin{cases} = a, & ...

  7. tomcat7禁用catalina.out输出

    tomcat7中禁用catalina.out的输出,又可能很大. 直接修改catalina.sh文件的输出语句. 在文件中找到以下内容. if [ -z "$CATALINA_OUT&quo ...

  8. 利用a标签自动解析URL

    parseURL // This function creates a new anchor element and uses location // properties (inherent) to ...

  9. 快速tab应用

    ZCTabNav-master https://github.com/zcsoft/ZCTabNav 层次构架清楚,很适合快速,导入

  10. Dedecms v5.7 最新注入分析

    该漏洞是cyg07在乌云提交的, 漏洞文件: plus\feedback.php.存在问题的代码: view source 01 ... 02 if($comtype == 'comments') 0 ...