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 ...
随机推荐
- django 学习-13 Django文件上传
1..vim blog/views.py from django import formsfrom django.http import HttpResponse 1 2 from dja ...
- sql将表中的某个字段进行排序
. update tempTable set field1 = rownum from( select field1, ROW_NUMBER() over(order by fieldId) rown ...
- 数据字典系统,要的进来下载吧,MVC+Easyui写的
数据字典系统,要的进来下载吧,以后做开发不用单独去生成一个数据字典了,直接打开此系统就OK啦 使用VS2012写的 语法是SQL 2005以上版本,包含2005的哦,2000的不适用此系统 字数不够啦 ...
- 【转载】存储过程实现FTP上传下载
CREATE OR REPLACE PACKAGE ftp AS -- ---------------------------------------------------------------- ...
- CoreAnimation 核心动画一 (一些常用属性 和 方法)
1.常用属性: frame bounds center alpha Transition 过渡 transform 动画效果 2.常用方法: +(void)setAnimati ...
- php面向对象的多态
多态是指使用类的上下文来重新定义或改变类的性质或行为,或者说接口的多种不同的实现方式即为多态.把不同的子类对象都当成父类来看,可以屏蔽不同子类对象之间的差异,写出通用的代码,做出通用的编程,以适应需要 ...
- 《NFS文件共享服务的搭建》RHEL
首先要清楚一点:NFS服务的共享是建立在linux和linux之间的. 配置任何服务之前我们要做的2件事: iptables -F setenforce 0 NFS服务内核防火墙影响不大,主要 ...
- Atan2
在三角函数中,两个参数的函数atan2是正切函数的 一个变种.对于任意不同时等于0的实参数x和y,atan2(y,x)所表达的意思是坐标原点为起点,指向(x,y)的射线在坐标平面上与x轴正方向之间 的 ...
- WPF 类型“System.ComponentModel.ISupportInitialize”在未被引用的程序集中定义。
问题:类型“System.ComponentModel.ISupportInitialize”在未被引用的程序集中定义.必须添加对程序集“System, Version=4.0.0.0, Cultur ...
- JS定时器实例解析
在javascritp中,有两个关于定时器的专用函数. 分别为:1.倒计定时器:timename=setTimeout("function();",delaytime);2.循环定 ...