当前位置:网站首页>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
总结
前后缀积
边栏推荐
- RMB classification II
- QT--实现TCP通信
- Multithreading (V) -- Concurrent tools (II) -- j.u.c concurrent contracting (I) -- AQS and reentrantlock principles
- 六月集训 第二天——字符串
- SQL 注入-盲注
- Tomato learning notes dvector and other basics
- LeetCode-1490. Clone n-ary tree
- MLP sensor
- Multithreading (2) -- pipeline (4) -- Park and unpark
- Explanation of sensor flicker/banding phenomenon
猜你喜欢

(14)Blender源码分析之闪屏窗口显示软件版本号

Deep and detailed analysis of PHP one sentence Trojan horse

leetcode 35. Search insert location

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

MLP sensor

Information content security experiment of Harbin Institute of Technology

Codeforces Round #793 (Div. 2) A B C

Jetson TX2 machine brushing jetpack4.2 (self test successful version)

LeetCode-1490. Clone n-ary tree

C2w model - language model
随机推荐
Introduction to the method of diligently searching for the alliance procedure
Android studio mobile development creates a new database and obtains picture and text data from the database to display on the listview list
May training (day 28) - Dynamic Planning
Video based fire smoke detection using robust AdaBoost
Touch screen setting for win7 system dual screen extended display
SQL injection based on error reporting
Codeforces Round #793 (Div. 2) A B C
MNIST handwritten data recognition by CNN
SQL注入——联合查询union
The difference between get and post and the code implementation of message board
使用 ms17-010 永恒之蓝漏洞对 win7 进行渗透及建立永久后门
Simulateur nightGod ADB View log
Leetcode January 12 daily question 334 Increasing ternary subsequence
Unreal Engine learning notes
上传文件(post表单提交form-data)
The seventh day of June training - hash table
数据库全量SQL分析与审计系统性能优化之旅
Piecewise Bezier curve
Video summary with long short term memory
LeetCode-1350. Invalid students