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 ...
随机推荐
- PHP分页
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- web性能瓶颈
1.网络,网络不好,其他做得再好,性能也是问题. 其中包括自己的带宽和请求的数量,带宽是我们无法控制的.我们能做的是尽可能的减少不必要的请求. 2.服务器,一个产品,服务器是关键,所有的请求都要经过服 ...
- Linux 文件及文件夹权限
普及 linux 基础知识,Linux 文件及文件夹权限,不要动不动就 777 权限.网上教程过于复杂啰嗦,简单总结如下...一.查看 Linux 文件权限 ls -l (通用)ll (Ubuntu适 ...
- mysql数据库用户和权限管理记录
一.MySQL用户的基本说明: 1.1 用户的基本结构MySQL的用户:用户名@主机 ■用户名:16个字符以内■主机:可以是主机名.IP地址.网络地址等主机名:www.111cn.net,localh ...
- JAVA 模糊查询方法
当我们需要开发一个方法用来查询数据库的时候,往往会遇到这样一个问题:就是不知道用户到底会输入什么条件,那么怎么样处理sql语句才能让我们开发的方法不管接受到什么样的条件都可以正常工作呢?这时where ...
- 杭电ACM2084--数塔
http://acm.hdu.edu.cn/showproblem.php?pid=2084 这种DP是相对容易的,一个二维数组,遍历一次,计算结果,存在指定位置. 本题关键代码是: a[i-1][j ...
- The-ith-Element
Brief: the-ith-element,given a array A with n element , return the i-th element of A. A(n,i) this p ...
- NSS_05 数据访问选型
在数据访问层上很想用orm框架, 选用Nhibernate或ef, 可以直接操作类对象, 避免转换, 更加的面向对象,更重要的是开发起来就方便多了. 但是从网上了解到这些框架太高级了, 用得不好到时会 ...
- jQuery插件使用大全
1.jQuery datepicker日历插件使用说明 http://wenku.baidu.com/view/12804e1e59eef8c75fbfb3e3 2.jqueryFileUpload插 ...
- JS重写alert,保证弹窗错误的友好性
// ------------------------------------------------------------- // 重写alert,保证弹窗错误的友好性 var j_oldAler ...