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的更多相关文章

  1. Careercup - Facebook面试题 - 6026101998485504

    2014-05-02 10:47 题目链接 原题: Given an unordered array of positive integers, create an algorithm that ma ...

  2. Careercup - Facebook面试题 - 5344154741637120

    2014-05-02 10:40 题目链接 原题: Sink Zero in Binary Tree. Swap zero value of a node with non-zero value of ...

  3. Careercup - Facebook面试题 - 5765850736885760

    2014-05-02 10:07 题目链接 原题: Mapping ' = 'A','B','C' ' = 'D','E','F' ... ' = input: output :ouput = [AA ...

  4. Careercup - Facebook面试题 - 5733320654585856

    2014-05-02 09:59 题目链接 原题: Group Anagrams input = ["star, astr, car, rac, st"] output = [[& ...

  5. Careercup - Facebook面试题 - 4892713614835712

    2014-05-02 09:54 题目链接 原题: You have two numbers decomposed in binary representation, write a function ...

  6. Careercup - Facebook面试题 - 6321181669982208

    2014-05-02 09:40 题目链接 原题: Given a number N, write a program that returns all possible combinations o ...

  7. Careercup - Facebook面试题 - 5177378863054848

    2014-05-02 08:29 题目链接 原题: Write a function for retrieving the total number of substring palindromes. ...

  8. Careercup - Facebook面试题 - 4907555595747328

    2014-05-02 07:49 题目链接 原题: Given a set of n points (coordinate in 2d plane) within a rectangular spac ...

  9. Careercup - Facebook面试题 - 5188884744896512

    2014-05-02 07:18 题目链接 原题: boolean isBST(const Node* node) { // return true iff the tree with root 'n ...

随机推荐

  1. 【Mongodb】---关联表查询population

    Population MongoDB是非关联数据库.但是有时候我们还是想引用其它的文档.这就是population的用武之地. Population是从其它文档替换文档中的特定路径.我们可以迁移一个单 ...

  2. YuXi-钰玺博客

    本博客将与YuXi-钰玺博客进行同步更新! YuXi-钰玺博客:www.studenty.cn

  3. 五.CSS盒子模型

    所谓盒模型,就是浏览器为每个HTML元素生成的矩形盒子.即HTML页面实际上就是由一系列盒子组成.这些盒子是按照可见版式在页面上排布的.并由三个属性进行控制:position属性,display属性, ...

  4. 浅谈sql 、linq、lambda 查询语句的区别

    浅谈sql .linq.lambda 查询语句的区别 LINQ的书写格式如下: from 临时变量 in 集合对象或数据库对象 where 条件表达式 [order by条件] select 临时变量 ...

  5. sql server实用工具sql prompt的安装与注册

    说起 sql prompt ,相信一直在sql server 领域摸爬滚打的技术人员们,并不陌生. 它是一款拥有SQL智能提示功能的SQL ServerVS插件.SQL Prompt能根据数据库的对象 ...

  6. 如何用C#代码查找某个路径下是否包含某个文件

      string path = "f:\\哈哈"; if (Directory.Exists(path)) { MessageBox.Show("存在文件夹") ...

  7. 对Json字符串进行格式化显示

    很多时候,我们拿Json字符串作为返回结果,但是当数据量多的时候,一堆的Json字符串看起来很不直观,这时候我们可以使用以下办法将Json字符串格式化一下再输出 var JsonUti = { //定 ...

  8. 网站添加到IIS和附件进程调试(新手使用篇)

    一.网站添加到IIS 做网站开发,很有必要把项目添加到IIS中,这对浏览和后期的调试很有帮助.怎么把网站添加到IIS上? 1). 打开IIS,然后操作步骤如下图: 选择Default Web Site ...

  9. UI1_UINavigationController

    // // FourthViewController.h // UI1_UINavigationController // // Created by zhangxueming on 15/7/6. ...

  10. android ListView的介绍和优化

    xml设计 <?xml version="1.0"?> -<RelativeLayout tools:context=".MainActivity&qu ...