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 ...
随机推荐
- 【Mongodb】---Scheme和Collections对应问题
Mongodb通过mongoose来与数据进行操作.而mongoose是通过model来创建数据库中对应的collection mongoose.model('User', UserSchema); ...
- CSS3 照片墙
HTML <body> <h2>照片墙制作</h2> <div class="container"> <img class=& ...
- CSS导航指示箭头
效果图 代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www. ...
- C#去掉周六周日的算法
/// <summary> /// 用来获取工作日(不含周六周日) /// </summary> /// <param name="dtSub"> ...
- JAVA JDBC 元数据分析小结
纯干货: 获取数据库名称: /** * 获取数据库的名称 */ public void getDataBaseName() throws Exception { Connection con = DS ...
- rac安装oem
[oracle@node1 ~]$ emca -config dbcontrol db -repos recreate -cluster STARTED EMCA at May 31, 2016 3: ...
- 【学习笔记】【C语言】数据
1. 什么是数据 生活中时时刻刻都在跟数据打交道,比如体重数据.血压数据.股价数据等.在我们使用计算机的过程中,会接触到各种各样的数据,有文档数据.图片数据.视频数据,还有聊QQ时产生的文字数据.用迅 ...
- CSS布局 ——从display,position, float属性谈起(转)
CSS布局 ——从display,position, float属性谈起 页面布局,或者是在页面上做些小效果的时候经常会用到 display,position和float 属性,如果对它们不是很了 ...
- MYSQL基础02(查询)
查询是很大的一块,所以这里我只会写mysql的特点,就我目前使用的情况,MYSQL对标准SQL是比较支持,如果是新手的话,建议去w3school 学习标准SQL. 1.DUAL DUAL是一个虚拟表, ...
- 求单链表倒数第m个结点
问题:求单链表倒数第m个结点,要求不准求链表的长度,也不许对链表进行逆转 解:设置两个指针p和q,p.q指向第一个结点.让p先移动到链表的第m个结点,然后p和q同时向后移动,直到p首先到达尾结点.此时 ...