当前位置:网站首页>leetcode:剑指 Offer 66. 构建乘积数组【前后缀积的应用】
leetcode:剑指 Offer 66. 构建乘积数组【前后缀积的应用】
2022-06-12 06:34:00 【白速龙王的回眸】

分析
因为不能用除号
所以要考虑存一下前后缀积
然后对头尾特判,其他正常计算即可
注意None和【】
ac code
class Solution:
def constructArr(self, a: List[int]) -> List[int]:
if not a:
return []
n = len(a)
b = a[::-1]
preMul, postMul = [0] * n, [0] * n
preMul[0] = a[0]
for i in range(1, n):
preMul[i] = preMul[i - 1] * a[i]
postMul[0] = b[0]
for i in range(1, n):
postMul[i] = postMul[i - 1] * b[i]
ans = []
for i in range(n):
if i == 0:
ans.append(postMul[n - 2])
elif i == n - 1:
ans.append(preMul[n - 2])
else:
ans.append(preMul[i - 1] * postMul[n - i - 2])
return ans
总结
前后缀积
边栏推荐
- Bert use
- Multithreading (IV) -- no lock (IV) -- unsafe
- The eighth day of June training - prefix and
- Leetcode personal question solution (Sword finger offer3-5) 3 Duplicate number in array, 4 Find in 2D array, 5 Replace spaces
- Redis distributed lock
- The seventh day of June training - hash table
- Multithreading (4) -- no lock (2) -- Atomic related atomic classes
- leetcode 35. Search insert location
- Redis application (I) -- distributed lock
- Single channel picture reading
猜你喜欢

Redis problem (I) -- cache penetration, breakdown, avalanche

Computer composition and design work06 - based on MIPS

Modifying theme styles in typora

PHP 读写 COOKIE

Process when solving vagrant up_ builder. rb:43:in `join‘: incompatible character encodings: GBK and UTF-8

The vs 2019 community version Microsoft account cannot be logged in and activated offline

SQL injection read / write file

Logistic regression model

Automatic modeling of Interchange

SQL injection based on error reporting
随机推荐
PHP 开发环境搭建及数据库增删改查
(14)Blender源码分析之闪屏窗口显示软件版本号
Redis data structure (VIII) -- Geo
LeetCode-419. Battleship on deck
六月集训 第六日——滑动窗口
RNN model
SQL注入——联合查询union
SQL language
Explanation of sensor flicker/banding phenomenon
LeetCode-1490. Clone n-ary tree
Logistic regression model
LeetCode-219. Duplicate Element II present
The eighth day of June training - prefix and
The principle of SQL injection is to build sqli labs, and SQL injection is simple and practical
GET 和 POST 的区别及留言板代码实现
Node. Detailed installation tutorial of CPM and cnpm (including error resolution)
N-degree Bessel curve
Leetcode January 10 daily question 306 Additive number
LeetCode-1587. Bank account summary II
Single channel picture reading