Careercup - Facebook面试题 - 4713484755402752
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的更多相关文章
- 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面试题 - 5435439490007040
2014-05-02 07:37 题目链接 原题: // merge sorted arrays 'a' and 'b', each with 'length' elements, // in-pla ...
随机推荐
- Part 36 to 39 Talking about Delegates in c#
Part 36 Delegates in c# Part 37 Delegates usage in c# class Progim { public static void Main() { Lis ...
- 【python】python文件和目录操作方法大全(含实例)
转自:http://www.jb51.net/article/48001.htm 一.python中对文件.文件夹操作时经常用到的os模块和shutil模块常用方法.1.得到当前工作目录,即当前Pyt ...
- 利用kvc对UITabBar上的UITabBarButton的尝试修改.md
一.前言 一次比较懒的想法,不想自定义UITabBar,也不想用第三方框架,于是想尝试修改苹果私有类来达到部分效果 效果如下 点击tabBar 上的按钮,图片有变大再变小的动画 tabBar 上某个按 ...
- netstat命令[转]
原文地址:http://www.cnblogs.com/peida/archive/2013/03/08/2949194.html netstat命令用于显示与IP.TCP.UDP和ICMP协议相关的 ...
- Geodatabase介绍
一.概述 (1)Geodatabase是什么? ArcGIS操作基于GIS文件格式和存储于地理数据库(Geodatabase)中的地理信息.Geodatabase是ArcGIS的本地数据结构,是用于编 ...
- CPU卡及NFC供应商
1.苏州市民卡供应商 北京握奇数据/watchdata 2.苏州市民卡手表 苏州连爱/linklove公司 3.NFC供应商 英飞凌/Infelion, NXP,复旦微电子, 4.什么是CPU卡? C ...
- c/c++面试总结(3)
7.vector和list的区别(这个也算是经常问的) vector和数组类似,拥有一段连续的内存空间,并且起始地址不变,这样对随机的读取很有效率(就是我们所有的[]运算符了),因为内存是连续的如果我 ...
- 探索VS中C++多态实现原理
引言 最近把<深度探索c++对象模型>读了几遍,收获甚大.明白了很多以前知其然却不知其所以然的姿势.比如构造函数与拷贝构造函数什么时候被编译器合成,虚函数.实例函数.类函数的区别等等.在此 ...
- CSS1-CSS3 <color>颜色知识知多少?
非本人原创,原文转载自http://www.zhangxinxu.com/wordpress/2015/07/know-css1-css3-color/ by zhangxinxu from http ...
- easyUI中treegrid组件构造树形表格(简单数据类型)+ssm后台
这几天做的项目要求用树形表格的形式展示一部分数据,于是就想到了使用easyUI的treegrid组件,但几经翻查各种资料,发现数据类型大多采取标准数据类型,即包含children元素的数据类型,小编查 ...