lintcode-64-合并排序数组 II
64-合并排序数组 II
合并两个排序的整数数组A和B变成一个新的数组。
注意事项
你可以假设A具有足够的空间(A数组的大小大于或等于m+n)去添加B中的元素。
样例
给出 A = [1, 2, 3, empty, empty], B = [4, 5]
合并之后 A 将变成 [1,2,3,4,5]标签
数组 排序数组 脸书
思路
从后向前遍历数组
code
class Solution {
public:
/**
* @param A: sorted integer array A which has m elements,
* but size of A is m+n
* @param B: sorted integer array B which has n elements
* @return: void
*/
void mergeSortedArray(int A[], int m, int B[], int n) {
// write your code here
int size = m + n, i = 0, a = m-1, b = n-1;
for(i=size-1; i>=0; i--){
if(A[a] <= B[b]) {
A[i] = B[b];
b--;
}
else {
A[i] = A[a];
a--;
}
}
}
};
lintcode-64-合并排序数组 II的更多相关文章
- LintCode之合并排序数组II
题目描述: 分析:题目的意思是把数组A和数组B合并到数组A中,且数组A有足够的空间容纳A和B的元素,合并后的数组依然是有序的. 我的代码: public class Solution { /* * @ ...
- 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=[1,2,3,4],B=[2,4,5,6],返回 [1,2,2,3,4,4,5,6] 1.Python:先将数组B加到数组A之后,然后 ...
- [LintCode笔记了解一下]64.合并排序数组
Given two sorted integer arrays A and B, merge B into A as one sorted array. 思路: 因为A的后面的部分都是空的留出来给我们 ...
- [LintCode] 合并排序数组II
class Solution { public: /** * @param A: sorted integer array A which has m elements, * but size of ...
- 64. 合并排序数组.md
描述 合并两个排序的整数数组A和B变成一个新的数组. 你可以假设A具有足够的空间(A数组的大小大于或等于m+n)去添加B中的元素. 您在真实的面试中是否遇到过这个题? 样例 给出 A = [1, 2, ...
- 6. 合并排序数组 II
6. Merge Two Sorted Arrays Description Merge two given sorted integer array A and B into a new sorte ...
- LintCode 6---合并排序数组 II
import java.util.Arrays; public class Lint6 { /* * 合并两个排序的整数数组A和B变成一个新的数组.新数组也要有序. */ public static ...
- [容易]合并排序数组 II
题目来源:http://www.lintcode.com/zh-cn/problem/merge-sorted-array/
随机推荐
- c# Hash一致算法负载均衡(KetamaHash)项目升级
其实就是我最近写的一个项目,采用Hash一致满足负载均衡.Hash一致环带虚拟节点. 在前面的博文中说明了我采用的方法,MurmurHash+红黑树(底层其实是sortedlist).经过多次测试结合 ...
- js替换字符串中的空格,换行符\r\n或\n替换成<br>
为了让回车换行符正确显示,需要将 \n 或 \r\n 替换成 <br>.同样地,将空格替换存 .这里我们通过正则表达式来替换. 一.替换所有的空格.回车换行符 //原始字符串 var s ...
- 【ospf-基础配置】
ospf开放最短路径优先基本配置{ ospf cost :配置ospf接口的优先级 ospf dr-priority :配置路径花费值 ospf router_id:创建ospf的进程号 area a ...
- zookeeper环境搭建(Linux)
安装zookeeper 安装jdk(此处省略) 解压tar包并配置变量环境 配置文件修改 将/usr/local/src/zookeeper-3.4.5/conf这个路径下的zoo_sample.cf ...
- springMVC3
复习: springmvc框架: DispatcherServlet前端控制器:接收request,进行response HandlerMapping处理器映射器:根据url查找Handler.(可以 ...
- R语言绘图:时间序列分析 ggplot2绘制ACF PACF
R语言真是博大精深 方法一 Acf(gold[,2], type = "correlation",lag.max = 100) Acf(gold[,2], type = " ...
- Using ARR to setup a proxy
Install IIS Install ARR 3.0 from WebPI Open IIS management console (inetmgr), select the server node ...
- java加密用到了BASE64Decoder时报错信息:Access restriction: The type BASE64Encoder is not accessible due to restrict
在Eclipse中编写Java代码时,用到了BASE64Decoder,import sun.misc.BASE64Decoder;可是Eclipse提示: Access restriction : ...
- C++11中default的使用
In C++11, defaulted and deleted functions give you explicit control over whether the special member ...
- bootstrap重新设计checkbox样式
文章采集于: https://www.cnblogs.com/GumpYan/p/7845445.html#undefined 在原文基础上修改了勾勾的内容,直接采用bootstrap字体库.修改了横 ...