[LeetCode] 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 nrespectively.
把两个有序数组合并成一个有序数组,这是merge sort的基础步骤,因为在algs4里面有训练过所以一下就写出来了,不过第一次遇到的时候我是没能写出来,原因是有tricky的corner case,就是扫描两个数组的时候有可能其中一个数组先结束了,这时候就要从另一个数组里面拿数据。 这里的办法是先设定总的迭代次数,就是m+n,反正不会超过这个量,然后在里面分别判断两个数组有无越界,有则从另一个数组拿数据,否则比较两数据大小,把小的取走。
void merge(int A[], int m, int B[], int n) {
int aux[m+n];
int i = , j = ;
for (int k = ; k < m+n; k++) {
if (i >= m) { aux[k] = B[j++]; }
else if (j >= n) { aux[k] = A[i++]; }
else if (A[i] < B[j]) {
aux[k] = A[i++];
} else {
aux[k] = B[j++];
}
}
for (int k = ; k < m+n; k++) {
A[k] = aux[k];
}
}
[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 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 ...
随机推荐
- 40 网络相关函数(八)——live555源码阅读(四)网络
40 网络相关函数(八)——live555源码阅读(四)网络 40 网络相关函数(八)——live555源码阅读(四)网络 简介 15)writeSocket向套接口写数据 TTL的概念 函数send ...
- replace、replaceAll、replaceFirst的区别详解
String s = "my.test.txt"; System.out.println(s.replace(".", "#")); Sys ...
- 插入备份数据 报 IDENTITY_INSERT 为 ON 时解决方法
SQL Server 2008 R2 x64 下 事情是这样的, 使用Navcate的数据导出备份工具导出表的记录 INSERT INTO [xxx_users] VALUES (1, 'tom ', ...
- SQL 大数据查询如何进行优化?
1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索引而 ...
- Kendo UI
http://www.cnblogs.com/libingql/category/585455.html http://www.scala-china.net/discuz/forum.php?mod ...
- CentOS 7部署flume
CentOS 7部署flume 准备工作: 安装java并设置java环境变量,在`/etc/profile`中加入 export JAVA_HOME=/usr/java/jdk1.8.0_65 ex ...
- CEF3开发者系列之类和接口
CEF3基本的框架包含C/C++程 序接口,通过本地库的接口来实现,而这个库则会隔离宿主程序和 Chromium&Webkit的操作细节.它在浏览器控件和宿主程序之间提供紧密的整合,它支持用户 ...
- 禁用visual studio实时调试器
最近每次开机时都会出来一个visual Studio实时调试器,报“发生了未处理的异常(‘System ComponentModel.Win32Exception’,发生位置是 BSSocketSms ...
- ExpressQuantumGrid4的cxGrid的一些使用方法和经验
使用cxGrid有一些时间了,在这里总结一下使用cxGrid的一些方法,希望给刚开始接触cxGrid的人一些帮助. 1.简单介绍:cxGrid右下方的cxGrid1Level1是表示Grid表的层,c ...
- opencv-3.x.0-x86-mingw32-staticlib-gcc5.3.0-20160712.7z
折腾了半天 用 cmake 3.5.0 + gcc5.3.0 编译 opencv3.x.0 静态库 opencv-3.0.0-x86-mingw32-staticlib-gcc5.3.0-201607 ...