2014-05-02 00:30

题目链接

原题:

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.

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

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

代码:

 // http://www.careercup.com/question?id=4713484755402752
#include <vector>
using namespace std; class Solution {
public:
void mergeSortedArray(vector<int> &a, vector<int> &b, vector<int> &c) {
int na, nb; na = (int)a.size();
nb = (int)b.size();
if (na == ) {
c = b;
return;
} else if (nb == ) {
c = a;
return;
} int i, j;
i = j = ;
while (i < na && j < nb) {
if (a[i] < b[j])
c.push_back(a[i++]);
} else if (a[i] > b[j]) {
c.push_back(b[j++]);
} else {
c.push_back((a[i++], b[j++]));
}
}
while (i < na) {
c.push_back(a[i++]);
}
while (j < nb) {
c.push_back(b[j++]);
}
}
};

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. 微软推出的免费新书《Introducing Microsoft SQL Server 2012》

    微软推出的免费新书<Introducing Microsoft SQL Server 2012>,该书详细介绍微软SQL 2012数据库服务最新功能以及功能应用和使用技巧. 该书适合SQL ...

  2. GridViewWIthSubActivityDemo

    GridViewWIthSubActivityDemo 拥有一个sub-activity的GridView示例: 显示一系列图片,点击其中之一的话,在新的sub-activity中放大显示. Imag ...

  3. Part 98 Anonymous methods in c#

    What is an anonymous method? Anonymous method is a method without a name. Introduced in C# 2.0,they ...

  4. 20141124-JS 变量,数据类型

    变量: 变量是JS中存储信息的容器,必须以字母开头. 格式: 定义变量只能用 var 定义,用 = 赋值. <script> var x=2; </script> 字符串型的值 ...

  5. UI2_UICollectionViewPicture

    // AppDelegate.m // UI2_UICollectionViewPicture // // Created by zhangxueming on 15/7/16. // Copyrig ...

  6. 济南学习 Day 3 T1 pm

    巧克力棒(chocolate)Time Limit:1000ms Memory Limit:64MB题目描述LYK 找到了一根巧克力棒,但是这根巧克力棒太长了,LYK 无法一口吞进去.具体地,这根巧克 ...

  7. C语言算法系列---1.队列和栈

    写在前面:在家玩了好久,实在是不知道干嘛了,突然想找些事做,现在是时候做些什么了.这些东西不见得多高深,也可能很简单,但很基础,也无法忽视.同时,也是自己学习走过的一条路. 这是开头,就写写C的队列和 ...

  8. AndroidStudio支持新的NDK的操作使用

    在2015的Google I / O大会,5月底,谷歌宣布了一项新的支持由Android NDK Studio 1.3,Jetbrains CLion集成功能,Android gradle插件.这种支 ...

  9. iOS ARC基本原理

    一.ARC基本简介 ARC:Automatic Reference Counting 自动引用 完全消除了手动管理内存的烦琐,编译器会自动在适当的地方插入适当的retain.release.autor ...

  10. 让backspace键默认为删除键

    在/root/.bashrc  中插入一条: stty erase ^H