当前位置:网站首页>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
总结
前后缀积
边栏推荐
- Modifying theme styles in typora
- Cv2.fillpoly coco annotator segment coordinate conversion to mask image
- 六月集训 第六日——滑动窗口
- Opencv_100问_第五章 (21-25)
- 使用 ms17-010 永恒之蓝漏洞对 win7 进行渗透及建立永久后门
- Multithreading mode (I) -- protective pause and join source code
- Android studio mobile development creates a new database and obtains picture and text data from the database to display on the listview list
- Leetcode sword finger offer (Second Edition) complete version of personal questions
- Excel VBA opens a file that begins with the specified character
- Install MySQL tutorial
猜你喜欢

Simulateur nightGod ADB View log

Multithreading (4) -- no lock (3) -- longadder source code

It only takes 10 minutes to understand the underlying principle of NiO

Video fire detection based on Gaussian mixture model and multi-color

What states do threads have?

SQL injection read / write file

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

Word vector training based on nnlm

Tips for using the potplayer video player

leetcode 278. First wrong version
随机推荐
Cause analysis of motion blur / smear caused by camera shooting moving objects
The difference between get and post and the code implementation of message board
LeetCode-1303. Team size
Touch screen setting for win7 system dual screen extended display
Chartextcnn (Ag dataset - news topic classification)
RNN implementation regression model
SQL language
N-degree Bessel curve
Tomato learning notes dvector and other basics
(14)Blender源码分析之闪屏窗口显示软件版本号
Redis application (I) -- distributed lock
Tips for using the potplayer video player
Zip and Items() difference
It only takes 10 minutes to understand the underlying principle of NiO
C2w model - language model
Book classification based on Naive Bayes
Using hidden Markov model to mark part of speech
Redis data type (VII) -- hyperloglog
Multithreading mode (I) -- protective pause and join source code
Codeforces Round #793 (Div. 2) A B C