lintcode--合并排序数组
来源:互联网 发布:如何配置子域名 编辑:程序博客网 时间:2024/06/05 08:44
合并两个排序的整数数组A和B变成一个新的数组。
注意事项
你可以假设A具有足够的空间(A数组的大小大于或等于m+n)去添加B中的元素。
给出 A = [1, 2, 3, empty, empty]
, B = [4, 5]
合并之后 A 将变成 [1,2,3,4,5]
class Solution {
/**
* 从数组A和B的尾部遍历比较元素,大的放在数组A的后面。最后如果B中还有元素,将其加入A中。
注意:不必在B中的元素遍历完后再次遍历A中的元素,因为他们已经在A中了。
*/
public void mergeSortedArray(int[] A, int m, int[] B, int n) {
// write your code here
int i= m-1;int j = n-1;int end = m+n-1;
while(i>=0&&j>=0){
if(A[i]>B[j]){
A[end] = A[i];
end--;
i--;
}else{
A[end] =B[j];
end--;
j--;
}
}
while(i>=0){
A[end] = A[i];
end--;
i--;
}
while(j>=0){
A[end] = B[j];
end--;
j--;
}
}
}
- LintCode 合并排序数组
- LintCode: 合并排序数组
- lintcode ----合并排序数组
- LintCode:合并排序数组
- lintcode--合并排序数组
- Lintcode合并排序数组
- Lintcode:合并排序数组
- LintCode 合并排序数组 II
- 合并排序数组(LintCode)
- LintCode(easy)合并排序数组
- LintCode-合并排序数组 II
- LintCode 6 合并排序数组
- 【lintcode笔记】合并排序数组
- Lintcode 合并排序数组 II
- LintCode-合并排序数组 II
- 【LintCode】合并排序数组 II
- Lintcode合并排序数组 II
- LintCode Python 合并排序数组
- Proxy动态代理
- 均值滤波、中值滤波的matlab实现
- 测试Boolean
- node权限控制模块node_acl的应用
- 修改Spring Boot启动端口
- lintcode--合并排序数组
- Ajax请求未等待应答直接执行了success
- (转载)Java异常之try,catch,finally,throw,throws
- Linux如何实现开机启动程序详解
- python中的函数
- mybatis中使用批量添加和删除
- Lintcode-K个最近的点(#612)
- 电脑键盘指法的正确练习步骤
- Doker源码地址