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 ...
随机推荐
- 简直喝血!H.265要被专利费活活玩死
转自 http://news.mydrivers.com/1/440/440145.htm H.264是如今最流行的视频编码格式之一,不但技术先进,而且专利费很低,企业每年只需支付650万美元,而个人 ...
- UI2_ScrollView&UIPageControl
// // ViewController.h // UI2_ScrollView&UIPageControl // // Created by zhangxueming on 15/7/10. ...
- UI4_UIToolBar
// // AppDelegate.m // UI4_UIToolBar // // Created by zhangxueming on 15/7/6. // Copyright (c) 2015年 ...
- BeanDefinition的Resource定位——2
1.FileSystemXmlApplicationContext的实现 public class FileSystemXmlApplicationContext extends AbstractXm ...
- 几道hihocoder不会做的题
1.https://hihocoder.com/problemset/problem/1433?sid=970287 boarding passes,不会做,看的别人的代码,现在还不是很理解. 2. ...
- Use XSLT in wix
Following content is directly reprinted from https://installpac.wordpress.com/2012/05/07/conflict-ma ...
- 修改eclipse中tomcat的发布路径
当我们在eclipse部署好tomcat的时候,默认这个项目是部署在eclipse\workspace\.metadata\.plugins\org.eclipse.wst.server.core\t ...
- CentOS6.5升级手动安装gcc4.8.2
一.简易安装 操作环境 CentOS6.5 64bit,原版本4.4.7,不能支持C++11的特性~,希望升级到4.8.2 不能通过yum的方法升级,需要自己手动下载安装包并编译 1.1 获取安装包并 ...
- C# 条码标签打印程序,RDLC报表动态显示多条码标签的方法
初学c#,因最近公司客户要求原出货标签需实现条码化,练手的机会来了,遂动手做这个程序,开始都是一些增删改查操作一直很顺利,但到RDLC报表将条码显示到报表上犯难了,因为初学未接触过报表,上网查资料均一 ...
- JAVA四种线程池实例
1.new Thread的弊端 执行一个异步任务你还只是如下new Thread吗? Java 1 2 3 4 5 6 7 new Thread(new Runnable() { ...