剑指offer—构建乘积数组
来源:互联网 发布:复杂网络算法matlab 编辑:程序博客网 时间:2024/06/06 13:09
华电北风吹
天津大学认知计算与应用重点实验室
日期:2015/10/8
题目描述
给定一个数组A[0,1,…,n-1],请构建一个数组B[0,1,…,n-1],其中B中的元素B[i]=A[0]A[1]…A[i-1]*A[i+1]…*A[n-1]。不能使用除法。
解析:用两个数组分别保存i以前和以后所有元素的乘积。然后计算输出向量的每个元素。
class Solution {public: vector<int> multiply(const vector<int>& A) { int size = A.size(); vector<int> B(size); vector<int> pre(size, 1), post(size, 1); for (int i = 1; i<A.size(); i++) { pre[i] = pre[i - 1] * A[i - 1]; } for (int i = size - 2; i >= 0; i--) { post[i] = post[i + 1] * A[i + 1]; } for (int i = 0; i<A.size(); i++) { B[i] = pre[i] * post[i]; } return B; }};
0 0
- 《剑指offer》——构建乘积数组
- 《剑指offer》——构建乘积数组
- 剑指offer——构建乘积数组
- 剑指offer——构建乘积数组
- 剑指offer—构建乘积数组
- 剑指offer—构建乘积数组
- 剑指offer--构建乘积数组
- 《剑指offer》构建乘积数组
- 剑指offer-构建乘积数组
- 剑指offer:构建乘积数组
- 剑指offer:构建乘积数组
- 剑指offer:构建乘积数组
- [剑指offer]构建乘积数组
- 剑指offer|构建乘积数组
- 《剑指offer》-构建乘积数组
- 剑指offer 构建乘积数组
- 构建乘积数组--剑指Offer
- 【剑指offer】构建乘积数组
- 选择排序之直接选择排序
- ocp-220
- NSCopying和NSMutableCopying协议
- 在Unity中添加UnityAds广告
- 服务器端如何判断客户端是不是手机
- 剑指offer—构建乘积数组
- MyEclipse问题收集
- Unity3D中用VS2012打开C#代码的设置
- 【自考】数据结构导论
- 10月风怒(2)
- Android:Material Design(七) 兼容性
- java的浮点的精度、无穷大、无穷小、和非数
- Ubuntu装机
- 我用KDE的N个理由