64. 合并排序数组.md
描述
合并两个排序的整数数组A和B变成一个新的数组。
你可以假设A具有足够的空间(A数组的大小大于或等于m+n)去添加B中的元素。
您在真实的面试中是否遇到过这个题?
样例
给出 A = [1, 2, 3, empty, empty], B = [4, 5]
合并之后 A 将变成 [1,2,3,4,5]
public class Solution {
/*
* @param A: sorted integer array A which has m elements, but size of A is m+n
* @param m: An integer
* @param B: sorted integer array B which has n elements
* @param n: An integer
* @return: nothing
*/
public void mergeSortedArray(int[] A, int m, int[] B, int n) {
// write your code here
int totalLength = m+n;
while (n != 0 && m != 0) {
int curA = A[m - 1];
int curB = B[n - 1];
if (curA > curB) {
A[totalLength - 1] = curA;
m --;
} else {
A[totalLength - 1] = curB;
n --;
}
totalLength --;
}
//如果数组A的长度为0;前面的直接不执行
while (n != 0) {
A[totalLength - 1] = B[n - 1];
n--;
totalLength--;
}
// while (aSize != 0) {
// }
}
}
public class Solution {
/*
* @param A: sorted integer array A which has m elements, but size of A is m+n
* @param m: An integer
* @param B: sorted integer array B which has n elements
* @param n: An integer
* @return: nothing
*/
public void mergeSortedArray(int[] A, int m, int[] B, int n) {
if(n == 0){
return;
}
if(m == 0){
for(int l = 0 ; l < n ; l++){
A[l] = B[l];
}
return;
}
int[] C = new int[m + n];
int i = 0;
int j = 0;
int num = 0;
boolean flag = false;
for(; i < m ; ){
// write your code here
for(; j < n ; ){
if(A[i] < B[j]){
C[num] = A[i];
num++;
i++;
if(i == m){
flag = true;
break;
}
}else{
C[num] = B[j];
j++;
num++;
if(j == n){
flag = true;
break;
}
}
}
if(flag){
break;
}
}
if(i != m){
for(; i < m ; i++){
C[num] = A[i];
num++;
}
}
if(j != n){
for(; j < n ; j++){
C[num] = B[j];
num++;
}
}
if(num == m + n){
for(int l = 0 ; l < m+n ; l++){
A[l] = C[l];
}
return;
}
}
}
64. 合并排序数组.md的更多相关文章
- [LintCode笔记了解一下]64.合并排序数组
Given two sorted integer arrays A and B, merge B into A as one sorted array. 思路: 因为A的后面的部分都是空的留出来给我们 ...
- lintcode:合并排序数组 II
题目: 合并排序数组 II 合并两个排序的整数数组A和B变成一个新的数组. 样例 给出A = [1, 2, 3, empty, empty] B = [4,5] 合并之后A将变成[1,2,3,4,5] ...
- lintcode:合并排序数组
题目: 合并排序数组 合并两个排序的整数数组A和B变成一个新的数组. 样例 给出A=[1,2,3,4],B=[2,4,5,6],返回 [1,2,2,3,4,4,5,6] 挑战 你能否优化你的算法,如果 ...
- LintCode之合并排序数组II
题目描述: 分析:题目的意思是把数组A和数组B合并到数组A中,且数组A有足够的空间容纳A和B的元素,合并后的数组依然是有序的. 我的代码: public class Solution { /* * @ ...
- 6. 合并排序数组 II
6. Merge Two Sorted Arrays Description Merge two given sorted integer array A and B into a new sorte ...
- LintCode——合并排序数组II
描述:合并两个排序的整数数组A和B变成一个新的数组 样例:给出A=[1,2,3,4],B=[2,4,5,6],返回 [1,2,2,3,4,4,5,6] 1.Python:先将数组B加到数组A之后,然后 ...
- 怎样合并排序数组(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 ...
- leetCode 88.Merge Sorted Array (合并排序数组) 解题思路和方法
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: Y ...
- LintCode之合并排序数组
题目描述: 我的代码: public class Solution { /* * @param A: sorted integer array A * @param B: sorted integer ...
随机推荐
- 通过$broadcast或$emit在子级和父级controller之间进行值传递
通过$broadcast或$emit在controller之间进行值传递,不过这些controller必须是子级或者父级关系, $emit只能向父级parent controller传递事件event ...
- Python元组与列表的区别
列表类似于我们用铅笔在纸上写字,写错了还可以擦掉:而元组则类似于用钢笔写字,写错了就擦不掉了,除非换张纸重写. 列表和元组的区别主要体现在一下几个方面: 列表属于可变序列,他的元素可以随时修改或删除: ...
- Unnamed namespaces
Unnamed namespaces The unnamed-namespace-definition is a namespace definition of the form inline(o ...
- ubuntu安装界面 会出现不完整情况
解决方法: alt+鼠标左键或者win+鼠标左键拖动
- python列表1
List (列表)List(列表) 是 Python 中使用最 频繁的数据类 型.列表 可以 完成大 多数集 合类 的数据 结构 实现. 列表中 元素 的类型 可以 不相同 ,它支 持数 字,字 符串 ...
- Redis都有哪些数据类型
string 这是最基本的类型了,就是普通的set和get,做简单的kv缓存 hash 这个是类似map的一种结构,这个一般就是可以将结构化的数据,比如一个对象(前提是这个对象没嵌套其他的对象)给缓存 ...
- C#学习-const和readonly
const是表示为常量的关键字,一旦赋值就不能改变了.是程序编译时候CLR就将const的值编译到IL代码中了. readonly也是常量的关键的字: 所以,有了这两个关键字的比较.readonly肯 ...
- [转] Webpack的devtool和source maps
source maps Webpack打包生成的.map后缀文件,使得我们的开发调试更加方便,它能帮助我们链接到断点对应的源代码的位置进行调试(//# souceURL),而devtool就是用来指定 ...
- centos7 查看ip地址
命令: ip address 简写ip a 过滤出来某个网卡的ip: ip a show ens33 |awk -F ' ' 'NR==3{print$2}'|cut -d / -f1
- fanuc 机床,加工中心通信总结,机床联网监控系统
有需求要与fanuc机床及加工中心通讯,读取状态信息. 1.通过了解,与fanuc通讯需要具备的硬件条件如下: a.串口通讯:可以进行程序的上传下载,绝大部分机床状态也是以文件的形式保存,所以理论上都 ...