Merge Two Sorted Arrays
Merge two given sorted integer array A and B into a new sorted integer array.
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的更多相关文章
- Merge k Sorted Arrays【合并k个有序数组】【优先队列】
Given k sorted integer arrays, merge them into one sorted array. Example Given 3 sorted arrays: [ [1 ...
- 怎样合并排序数组(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 ...
- [Algorithm] 6. Merge Two Sorted Arrays
Description Merge two given sorted integer array A and B into a new sorted integer array. Example A= ...
- 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 ...
- 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays
一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...
- 【转载】两个排序数组的中位数 / 第K大元素(Median of Two Sorted Arrays)
转自 http://blog.csdn.net/zxzxy1988/article/details/8587244 给定两个已经排序好的数组(可能为空),找到两者所有元素中第k大的元素.另外一种更加具 ...
- LeetCode—— Median of Two Sorted Arrays
Description: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the medi ...
- LeetCode——Merge k Sorted Lists
Discription: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its ...
- LeetCode 4 Median of Two Sorted Arrays (两个数组的mid值)
题目来源:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 an ...
随机推荐
- 在VS2012中编译WinXP兼容的程序
VS2012默认是不兼容Windows XP的,编译链接出来的程序只能在Windows Vista及以上版本的操作系统上运行.可是有时需要在Windows XP上运行,又不得不用VS2012(例如用了 ...
- Oracle常用sql
Oracle不像Sqlserver,并没有提供l默认约束,但提供了默认值,效果一样.--------------------------- 在建表时设置默认约束-------------------- ...
- Oracle自定义函数实例
1. 传入一个值, 如果该值为0,则返回空. CREATE OR REPLACE FUNCTION Fun_Test(p IN NUMBER) RETURN VARCHAR2 IS v_Result ...
- MySQL tips (日期时间操作/concat 等)
1. Query结尾要加一个分号: 2. 数据库和表 SHOW DATABASES; USE YOUR_DB; SHOW TABLES; SHOW COLUMNS FROM study或者D ...
- 全排列(java版)
适用于不同数字的全排列,其实也适用于有重复数字的全排列,只不过的出来的结果有重复,需手动删减掉重复的组合. package testFullPermutation; import java.util. ...
- hihocoder #1058 Combination Lock
传送门 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Finally, you come to the interview room. You know that a ...
- intent属性
private String mAction;private Uri mData;private String mType;private String mPackage;private Compon ...
- PHP防止重复提交表单(helloweba网站经典实例)
<?php session_start(); header("Content-Type:text/html;charset:utf8"); function set_toke ...
- OOA/OOD/OOP(了解)
Object-Oriented Analysis:面向对象分析方法 是在一个系统的开发过程中进行了系统业务调查以后,按照面向对象的思想来分析问题.OOA与结构化分析有较大的区别.OOA所强调的是在系统 ...
- JavaScript入门培训材料(Copy至此以作备份)
JavaScript简明学习教程 2014年5月31日 目录 一.说明... 2 二.准备知识... 2 (一)HTML. 2 (二)DOM.. 3 三.JavaScript简介... 3 四.Jav ...