Careercup - Facebook面试题 - 5179916190482432
2014-05-01 00:45
原题:
input [,,,]
output [,,,] Multiply all fields except it's own position. Restrictions:
. no use of division
. complexity in O(n)
题目:给定一个整数数组,将个元素变为其他元素的乘积,例如[2, 3, 1, 4]变为[12, 8, 24, 6]。限制不准用除法,而且时间复杂度为线性级别。
解法:用一个额外的数组能够完成O(n)时间的算法。由于每个元素在变化之后,应该等于左边和右边的累计乘积,所以两边的累计乘积必须同时能够知道。一边可以用O(1)空间扫描得到,另一边只能用O(n)空间进行记录。时间空间复杂度均为O(n),请看代码。
代码:
// http://www.careercup.com/question?id=5179916190482432
#include <cstdio>
#include <vector>
using namespace std; void multiplyArray(vector<int> &v)
{
vector<int> vp;
int p;
int i;
int n = (int)v.size(); vp.resize(n);
p = ;
for (i = ; i <= n - ; ++i) {
vp[i] = p;
p *= v[i];
} p = ;
for (i = n - ; i >= ; --i) {
vp[i] = p * vp[i];
p *= v[i];
} for (i = ; i < n; ++i) {
v[i] = vp[i];
} vp.clear();
} int main()
{
int i, n;
vector<int> v; while (scanf("%d", &n) == && n >= ) {
v.resize(n);
for (i = ; i < n; ++i) {
scanf("%d", &v[i]);
}
multiplyArray(v);
for (i = ; i < n; ++i) {
printf((i ? " %d" : "%d"), v[i]);
}
putchar('\n');
} return ;
}
Careercup - Facebook面试题 - 5179916190482432的更多相关文章
- 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 ...
随机推荐
- 【CSS3】---练习制作导航菜单
练习题 根据所学知识,使用CSS3实现下图的导航菜单效果 任务 1.制作导航圆角 提示:使用border-radius实现圆角 2.制作导航立体风格 提示:使用box-shadow实现立体风格 3.制 ...
- 【转载】TalkingData首席金融行业专家鲍忠铁:18亿数据解读移动互联网
http://www.36dsj.com/archives/33417 鲍忠铁:大家下午好! 今天我会讲三个议题,一是用18亿数据解读现在移动互联网的生态圈.二是看看数据有什么样的应用.三是大数据的隐 ...
- 快速调试的VS设置
这是2013年“惹”的“祸”. 自己一直使用着VS2012,以前的调试是相当方便的,或许是之前的同事设置好的VS,我一直不会去注意我停掉调试(停掉调试的意思是:将状态1正在调试的状态,变更为状态2待启 ...
- java中的@Override是否需要
java中的重载注解 @Override 是否需要?今天被人问到这个问题,回答的不太好,下来看了一下源码 /** * Annotation type used to mark methods that ...
- 清除windows的EFS加密
所使用软件为aefsdr_setup_en,搜索名为advanced.efs.data.recovery 1. 创建需要加密的文件 2. 进行加密 ...
- 20141017--异常语句try-catch
//try-catch 尝试(try)-抓获(catch) try//尝试,保护起来,使程序出错也能执行 { //确定不会出错时不要用try,当不确定时使用try-catch可以捕获错误, int i ...
- 九款酷炫基于jquery实现的应用及源码
1.HTML5 Loading动画加载 五彩的圆环Loading 今天我们要分享一款基于HTML5的Loading加载动画特效,这款HTML5加载动画是一个五彩的圆环,圆环不停地转动从而体现加载正在进 ...
- call与apply函数
call与apply函数 1.为什么需要call与apply函数 Javascript中,每一个函数内部都有一个特殊的关键词this,其随着所处环境的不同其指向也是不同的. 函数的内部其this也是指 ...
- DropDownList另一种写法
2013-09-29 17:04:47 1.性别: <asp:DropDownList ID="DrpSex" runat ="server" Widt ...
- mplayer-for-windows change color scheme in win 7
Q: When I play movie on Windows7, always comes this message: The color scheme has been changed The f ...