2014-05-02 00:30

题目链接

原题:

  1. Given two arrays of sorted integers, merge them keeping in mind that there might be common elements in the arrays and that common elements must only appear once in the merged array.

题目:归并两个已排序的数组,重复元素只能在归并的结果里出现一次。

解法:题意很清晰,只是没有说两个数组里本身是否有重复元素,依题意写代码就可以了。

代码:

  1. // http://www.careercup.com/question?id=4713484755402752
  2. #include <vector>
  3. using namespace std;
  4.  
  5. class Solution {
  6. public:
  7. void mergeSortedArray(vector<int> &a, vector<int> &b, vector<int> &c) {
  8. int na, nb;
  9.  
  10. na = (int)a.size();
  11. nb = (int)b.size();
  12. if (na == ) {
  13. c = b;
  14. return;
  15. } else if (nb == ) {
  16. c = a;
  17. return;
  18. }
  19.  
  20. int i, j;
  21. i = j = ;
  22. while (i < na && j < nb) {
  23. if (a[i] < b[j])
  24. c.push_back(a[i++]);
  25. } else if (a[i] > b[j]) {
  26. c.push_back(b[j++]);
  27. } else {
  28. c.push_back((a[i++], b[j++]));
  29. }
  30. }
  31. while (i < na) {
  32. c.push_back(a[i++]);
  33. }
  34. while (j < nb) {
  35. c.push_back(b[j++]);
  36. }
  37. }
  38. };

Careercup - Facebook面试题 - 4713484755402752的更多相关文章

  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面试题 - 5435439490007040

    2014-05-02 07:37 题目链接 原题: // merge sorted arrays 'a' and 'b', each with 'length' elements, // in-pla ...

随机推荐

  1. CSS3写常用的形状

    正方形: 1 .square{ width: 100px;height: 100px; background: #E57779;}   长方形: .rectangle{ width: 200px;he ...

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

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

  3. 【CSS3】---first-of-type选择器+nth-of-type(n)选择器

    first-of-type选择器 “:first-of-type”选择器类似于“:first-child”选择器,不同之处就是指定了元素的类型,其主要用来定位一个父元素下的某个类型的第一个子元素. 示 ...

  4. 破解软件系列-PE文件深入浅出之Section Table节表

    我们已经学了许多关于 DOS header 和 PE header 的知识.接下来就该轮到 section table(节表)了.节表其实就是紧挨着 PE header 的一结构数组.该数组成员的数目 ...

  5. 二十一、Android上常见度量单位【xdpi、hdpi、mdpi、ldpi】解读

    术语和概念 屏幕尺寸 屏幕的物理尺寸,以屏幕的对角线长度作为依据(比如 2.8寸, 3.5寸). 简而言之, Android把所有的屏幕尺寸简化为三大类:大,正常,和小. 程序可以针对这三种尺寸的屏幕 ...

  6. asp.net 组织结构图控件

    记得之前做项目的时候客户需要看一个组织结构图,从而了解一下公司的概况,本来自己之前没有做过这方面的控件,只好找度娘,出于对项目的完美,网上很多控件画面感比较渣,后来只能在这些个中挑个比较好的来做,先看 ...

  7. GPRS组网的几种方案【来自网络】

    GPRS组网的几种方案:1) 方案一:中心采用ADSL等INTELNET公网连接,采用公网固定IP或者公网动态IP+DNS解析服务.此种方案向先INTERNET运营商申请ADSL等宽带业务.     ...

  8. (转)如何构建高性能,稳定SOA应用之-负载均衡-Decoupled Invocation(一)

    当我们在为一个软件设计架构的时候,我们不仅仅要确保所做出来的架构要满足系统的业务需求,更加要确保做出来的架构要满足可维护性,安全,稳定性的非业务行的需求. 另外一个非常重要的非功能性需求就是性能.性能 ...

  9. Grunt 认识

    基本工作流: JS合并.JS压缩.CSS压缩.CSS Sprite.图片优化.测试.静态资源缓存(版本更新)... 基于工作流产生的工具: JSHint(jshint.com).CSSLint(css ...

  10. 9款超绚丽的HTML5/CSS3应用和动画特效

    1.CSS3飘带状3D菜单 菜单带小图标 这次我们要来分享一款很特别的CSS3菜单,菜单的外观是飘带状的,并且每一个菜单项有一个精美的小图标,鼠标滑过菜单项时,菜单项就会向上凸起,像是飘带飘动一样,形 ...