Careercup - Facebook面试题 - 5435439490007040
2014-05-02 07:37
原题:
// merge sorted arrays 'a' and 'b', each with 'length' elements,
// in-place into 'b' to form a sorted result. assume that 'b'
// has 2*length allocated space.
// e.g. a = [1, 3, 5], b = [2, 4, 6] => b = [1, 2, 3, 4, 5, 6] //how to do it without rearanging the b array
题目:有两个排好序的数组a[]和b[],把a有序归并到b中去,保证b的空间充足。如何就地完成这个算法?
解法:从后往前归并就可以不用额外空间了。
代码:
// http://www.careercup.com/question?id=5435439490007040
#include <iostream>
#include <vector>
using namespace std; class Solution {
public:
void mergeTwoArray (vector<int> &a, vector<int> &b) {
// merge a[] into b[].
int na = (int)a.size();
int nb = (int)b.size(); b.resize(na + nb); int i, j, k; i = na - ;
j = nb - ;
k = na + nb - ;
while (i >= && j >= ) {
b[k--] = a[i] > b[j] ? a[i--] : b[j--];
}
while (i >= ) {
b[k--] = a[i--];
}
};
}; int main()
{
vector<int> a, b;
int na, nb;
int i;
Solution sol; while (cin >> na >> nb && (na > && nb > )) {
a.resize(na);
b.resize(nb);
for (i = ; i < na; ++i) {
cin >> a[i];
}
for (i = ; i < nb; ++i) {
cin >> b[i];
}
sol.mergeTwoArray(a, b);
nb = (int)b.size();
for (i = ; i < nb; ++i) {
i ? (cout << ' ', ) : ;
cout << b[i];
}
cout << endl; a.clear();
b.clear();
} return ;
}
Careercup - Facebook面试题 - 5435439490007040的更多相关文章
- Careercup - Facebook面试题 - 6026101998485504
2014-05-02 10:47 题目链接 原题: Given an unordered array of positive integers, create an algorithm that ma ...
- Careercup - Facebook面试题 - 5344154741637120
2014-05-02 10:40 题目链接 原题: Sink Zero in Binary Tree. Swap zero value of a node with non-zero value of ...
- Careercup - Facebook面试题 - 5765850736885760
2014-05-02 10:07 题目链接 原题: Mapping ' = 'A','B','C' ' = 'D','E','F' ... ' = input: output :ouput = [AA ...
- Careercup - Facebook面试题 - 5733320654585856
2014-05-02 09:59 题目链接 原题: Group Anagrams input = ["star, astr, car, rac, st"] output = [[& ...
- Careercup - Facebook面试题 - 4892713614835712
2014-05-02 09:54 题目链接 原题: You have two numbers decomposed in binary representation, write a function ...
- Careercup - Facebook面试题 - 6321181669982208
2014-05-02 09:40 题目链接 原题: Given a number N, write a program that returns all possible combinations o ...
- Careercup - Facebook面试题 - 5177378863054848
2014-05-02 08:29 题目链接 原题: Write a function for retrieving the total number of substring palindromes. ...
- Careercup - Facebook面试题 - 4907555595747328
2014-05-02 07:49 题目链接 原题: Given a set of n points (coordinate in 2d plane) within a rectangular spac ...
- Careercup - Facebook面试题 - 5188884744896512
2014-05-02 07:18 题目链接 原题: boolean isBST(const Node* node) { // return true iff the tree with root 'n ...
随机推荐
- 深入理解ThreadLocal(转)(2015年06月11日)
注明:转自:http://my.oschina.net/clopopo/blog/149368 学习一个东西首先要知道为什么要引入它,就是我们能用它来干什么.所以我们先来看看ThreadLocal对我 ...
- linux 用户打开进程数和文件数调整
1 查看nproc(max user processes)命令 [root@vm-cdh4 ~]# ulimit -u 14866 2 修改nproc 临时修改, 重登录或重启后失效: [root@v ...
- mac下apache的多站点配置
以下操作均建立在 已经配置好了php环境 从网上搜索了下,后来自己配置了下还是比较简单的! 我的环境目录是在/Library/webServer/Documents 测试的时候可以直接在这里建立 ...
- Excel VBA 快捷键 代码
一. 在ThisWorkbook中 Private Sub Workbook_Open() '%对应alt键 宏不能加() Application.OnKey "%q", &quo ...
- iOS数据持久化(三)
#pragma mark - Core Data stack /** * @synthesize 关联成员变量和属性 */ @synthesize managedObjectContext = _ma ...
- c#中sqlhelper类的编写(二)
上一篇文章讲了简易版的SqlHelper类的编写,我们在这里就上一篇文章末尾提出的问题写出解决方案. sql语句注入攻击已经是众所周知的了.我们如何在C#中保护自己的数据库不被这样的方式攻击呢? 不用 ...
- jQuery 简单漂亮的 Nav 导航菜单
自己写的一个简单的导航菜单,先看效果: 鼠标悬浮时菜单项向上移动成蓝底白字,点击之后底部会有蓝条表示当前选中项. 页面代码,菜单的每一项都是一个 div ,其中包括一个 ul 用来放置显示文字等,另一 ...
- Window Phone 8开发问题反思
项目开发有段时间了,进入了阶段测试.然而在测试过程中bug连连不断,在抱怨产品需求的坑爹.不合理之外,我也一直在反思为什么会出现这么多Bug. 首先,由于项目开发的两个人都是新手,在刚刚认识MVVM架 ...
- Css background缩写
例子: background:url(../images20130624/bg.png) no-repeat -1424px -5px; 官方API Value: ['background-color ...
- Ubuntu搜狗输入法的使用
下载搜狗拼音输入法安装包 删除ibus,确认没有以ibus开头的包存在 sudo dpkg -P ibus ibus-gtk ibus-gtk3 ibus-pinyin ibus-pinyin-db- ...