leetcode — merge-sorted-array
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的更多相关文章
- [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 ...
- [Leetcode] Merge Sorted Array (C++)
我在Github上新建了一个解答Leetcode问题的Project, 大家可以参考, 目前是Java 为主,里面有leetcode上的题目,解答,还有一些基本的单元测试,方便大家起步. 题目: Gi ...
- [leetcode]Merge Sorted Array @ Python
原题地址:https://oj.leetcode.com/problems/merge-sorted-array/ 题意:Given two sorted integer arrays A and B ...
- [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 ...
- [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 ...
- LeetCode Merge Sorted Array 合并已排序的数组
void merge(int A[], int m, int B[], int n) { int *a=A,*b=B; ,j=; ||m==){ //针对特殊情况,比如A或B中无元素的情况 & ...
- leetcode - Merge Sorted Array (run time beats 100.00% of cpp submissions.)
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...
- [LeetCode] 88. Merge Sorted Array 混合插入有序数组
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: T ...
- 【LeetCode练习题】Merge Sorted Array
Merge Sorted Array Given two sorted integer arrays A and B, merge B into A as one sorted array. Note ...
- Leetcode#88. Merge Sorted Array(合并两个有序数组)
题目描述 给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组. 说明: 初始化 nums1 和 nums2 的元素数量分别为 m ...
随机推荐
- MyBatis3系列__06查询的几点补充
关于查询的一点补充: 当查询部门信息时,希望查询该部门下的所有员工,下面会采取两种方式实现: 1.联合查询 public Department getDeptWithEmpById(Integer i ...
- MQTT之Mosquitto
https://mosquitto.org/ Eclipse Mosquitto是一个开源(EPL / EDL许可)消息代理,它实现了MQTT协议版本3.1和3.1.1.Mosquitto重量轻,适用 ...
- AGC001F - Wide Swap
Description 给你一个长度为$n$的排列,每次可以交换$|i-j|\geq K$并且$|a_i-a_j|=1$的数对,问你经过若干次变换后最小字典序的排列是啥 Solution 对$a$做一 ...
- window 安装redis、memcache的php扩展和 reidis 、memcache 及 reids管理软件
redis 1.安装redis的php扩展 http://windows.php.net/downloads/pecl/releases/redis/ http://windows.php.net/d ...
- sql server 2012 减少日志
USE [master] GO ALTER DATABASE 数据库名 SET RECOVERY SIMPLE WITH NO_WAIT GO ALTER DATABASE 数据库名 SET RECO ...
- nodejs编译遇到的问题
src\node_contextify.cc:631: Assertion 'args[1]->IsString()' failed. node 10 版本会出现这个问题, 安装natives后 ...
- 关于外网无法访问阿里云主机CentOs
前两天阿里云ECS搞活动,所有买了个三年的Ecs,然后照着之前在虚拟机同样的搭建服务器,一切都很正常,可是 当我配置好防火墙和nginx之后,发现个问题,外网无法访问. 思考: 1.我的nginx没配 ...
- Web版记账本开发记录(三)开发过程遇到的问题小结2
问题1,获得当前时间 Date d = new Date(); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ...
- JAVA---MYSQL 基本知识点 第二部分
增删改查 (CRUD): 数据库 , 表 , 记录 ; 约束 ; 主键约束 :primary key 如果是int类型 可以使用 自动增长型 auto_increment; 唯一约束 ...
- 使用bind提供域名解析服务搭建
正向解析实验 1.安装bind服务 2.在/etc目录中找到该服务程序的主配置文件,然后把第11行和第17行的地址均修改为any 3.正向解析参数如下: 4.编辑数据配置文件,从/var/named目 ...