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的更多相关文章

  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. jQuery.Hotkeys - lets you watch for keyboard events anywhere in your code supporting almost any key combination

    About jQuery Hotkeys is a plug-in that lets you easily add and remove handlers for keyboard events a ...

  2. SQL SERVER 中的行列转换小结

    1. 介绍说明 前段时间组内的小伙伴在升级维护项目中,经常涉及一些复杂的数据转换问题,让我去看下有些地方怎么处理,我发现好多都是涉及到行列转换的问题,处理起来经常会比较麻烦,借此也总结一下,方便以后的 ...

  3. Flex前台和后台WCF服务之间数据的接收与传输

    1.首先在flex程序中通过添加webservice,方式是主菜单Data->Connect to WebService,然后输入wsdl文档的地址.如果输入地址后始终添加不进了,或者报错,一般 ...

  4. 十、Notepad++正则表达式使用

    推荐个正则表达式在线测试的工具http://ccmpp.com/Regex/ Notepad++正则表达式使用 2011-01-06 10:01:35| 分类: 文档 | 标签:正则表达式 替换 no ...

  5. 浅谈sqlserver数据库优化(一)----开光篇

    今天暂时无事,风和日丽,万里无云.游山的.玩水的.遛麻雀的都闲的不亦乐乎,也忙的不亦乐乎.在这美好的季节,依旧躲在被窝或是电脑旁绞尽脑汁敲键盘的人们,也别有一番滋味.废话少说,言归正传. 赶上了一个最 ...

  6. MVC小例子

    [约定胜于配置] 1. 右键Mode数据层添加新建项,用linq连接数据库 (不要在控制层上直接操控linq,要在数据层新建一个类,来对数据库进行操作) 2. 右键Mode数据层添加类,来完成对数据库 ...

  7. UNIX 信号基本概念

    1. 信号的基本概念 为了理解信号,先从我们最熟悉的场景说起: 用户输入命令,在Shell下启动一个前台进程. 用户按下Ctrl-C,这个键盘输入产生一个硬件中断. 如果CPU当前正在执行这个进程的代 ...

  8. linux 环境变量的设置【转】

    现在使用linux的朋友越来越多了,在linux下做开发首先就是需要配置环境变量,下面以java环境变量为例介绍三种配置环境变量的方法. 1.修改/etc/profile文件 如果你的计算机仅仅作为开 ...

  9. 了解GDAL的图像处理/Python

    GDAL是一个操作各种栅格地理数据格式的库.包括读取.写入.转换.处理各种栅格数据格式(有些特定的格式对一些操作如写入等不支持).它使用了一个单一的抽象数据模型就支持了大多数的栅格数据(GIS对栅格, ...

  10. OC 内存管理机制总结

    OC 内存管理机制总结 一:OC内存管理机制目前分为两块,其一自动内存管理机制,其二手动内存管理机制: 1.首先我们从自动内存管理机制讲起: 1)什么是自动内存管理机制,自动内存管理机制就是程序中所创 ...