美文网首页Lintcode
Lintcode50 Product of Array Excl

Lintcode50 Product of Array Excl

作者: 代码码着玩 | 来源:发表于2017-04-19 07:26 被阅读21次

【题目描述】

Given an integers array A.Define B[i] = A[0] * ... * A[i-1] * A[i+1] * ... * A[n-1], calculate B WITHOUT divide operation.

给定一个整数数组A。定义B[i] = A[0] * ... * A[i-1] * A[i+1] * ... * A[n-1], 计算B的时候请不要使用除法。

【题目链接】

http://www.lintcode.com/en/problem/product-of-array-exclude-itself/

【题目解析】

有点动态规划的思想。

首先从后往前遍历数组,用f[i]保存i到end所有元素的乘积。

然后从前往后遍历数组,对于当前位i,其值的计算公式为start~i-1的元素的乘积 * f[i]。start~i-1的乘积在i-1位置时已经可以得到。

【参考答案】

http://www.jiuzhang.com/solutions/product-of-array-exclude-itself/

相关文章

网友评论

    本文标题:Lintcode50 Product of Array Excl

    本文链接:https://www.haomeiwen.com/subject/zjrznttx.html