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 ...
随机推荐
- java连接sql server2000/2005
接触Java或者JSP,难免会使用到数据库SQL Server 2000/2005(我使用2005标准版[9.0.3054]测试),经过自己的搜索和研究,使用JDBC连接SQL Server成功,特此 ...
- eclipse快捷键的使用及概述
<eclipse快捷键的使用及概述> <Eclipse概述> Eclipse 是一个开放源代码的.基于Java的可扩展开发平台.就其本身而言,它只是一个框架和一组服 ...
- web前端开发常用的10个高端CSS UI开源框架
web前端开发常用的10个高端CSS UI开源框架 随着人们对体验的极致追求,web页面设计也面临着新的挑战,不仅需要更人性化的设计理念,还需要设计出更酷炫的页面.作为web前端开发人员,运用开源 ...
- CVE-2014-0160 Heartbleed Vul Analysis && OpenSSL Cryptographic Software Library Bug
目录 . Heartbleed漏洞简介 . 漏洞造成的风险和影响 . 漏洞的测试.POC . OpenSSL漏洞源代码分析 . 防御.修复方案 . 从漏洞中得到的攻防思考 1. Heartbleed漏 ...
- cowboy-高性能简洁的erlang版web框架
那么Cowboy是什么呢? Cowboy is a small, fast and modular HTTP server written in Erlang. 其定位非常明确: Cowboy aim ...
- 在excel批量更改单元格类型后的批量刷新显示
把E的东西变成完整显示 解决办法: 选中所需要更改的整列数据------>菜单栏的数据选项------>分列
- 依赖管理工具漫谈--从Maven,Gradle到Go
http://jolestar.com/dependency-management-tools-maven-gradle/
- Jquery——思维导图
- Jquery的初识
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- java链接到mysql
原文出自 http://qq163230530.blog.163.com/blog/static/4289250620081186262719/ eclipse 下载安装后 新建web项目 首先配置服 ...