当前位置:网站首页>【Leetcode】1352. 最后 K 个数的乘积
【Leetcode】1352. 最后 K 个数的乘积
2022-07-05 04:46:00 【wangzirui32】
博文作者 wangzirui32
喜欢的可以 点赞 收藏 关注哦~~
本文首发于CSDN,未经许可禁止转载
1. 题目描述
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/product-of-the-last-k-numbers/
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
2. 解题思路
这道题有点难度,我们不能记录每次add
添加的数字,这样每次getProduct
都要计算乘积,耗时较大(我用此方法编写代码显示超时),于是,我们需要换一种思路:我们可不可以每次add
时顺带把乘积计算完毕存入列表呢?可以,假设我们输入了如下长度为4的列表:
索引 | 0 | 1 | 2 | 3 |
---|---|---|---|---|
数值 | a a a | b b b | c c c | d d d |
然后,我们把它转化为一个新列表:
索引 | 0 | 1 | 2 | 3 |
---|---|---|---|---|
数值 | a a a | a b ab ab | a b c abc abc | a b c d abcd abcd |
可以看到,数值依次成为了ab
, abc
等的乘积,设我们需要计算后K个数的乘积(假设 k = 2 k=2 k=2),等价于求 c d cd cd的乘积,就是 a b c d / a b = c d abcd / ab = cd abcd/ab=cd,也就是索引倒数第一除以倒数第三,引入 K K K即可表达为:
r e s u l t = n u m s [ − 1 ] / n u m s [ − k − 1 ] result = nums[-1] / nums[-k-1] result=nums[−1]/nums[−k−1]
3. 代码实现
Code:
class ProductOfNumbers:
def __init__(self):
self.product_list = [1] # 初始值为1
def add(self, num):
if num == 0: # 如果为0 在此之后的乘积均为0 所以重置列表
self.product_list = [1]
else:
# 与列表最后一项相乘 即可出现 a ab abc 一类的列表
self.product_list.append(num * self.product_list[-1])
def getProduct(self, k):
if k >= len(self.product_list):
# k大于长度返回0
return 0
else:
# result = nums[-1] / nums[-k-1]
return self.product_list[-1] / self.product_list[-k-1]
4. 最终结果
好了,今天的课程就到这里,我是wangzirui32,喜欢的可以点个收藏和关注,我们下次再见!
边栏推荐
- Leetcode hot topic Hot 100 day 33: "subset"
- Interface joint commissioning test script optimization V5.0 (end)
- Understand encodefloatrgba and decodefloatrgba
- [groovy] closure (closure as function parameter | code example)
- AutoCAD - isometric annotation
- Looking at Chinese science and technology from the Winter Olympics: what is the mystery of the high-speed camera that the whole people thank?
- You Li takes you to talk about C language 7 (define constants and macros)
- Basic analysis of IIC SPI protocol
- Error statuslogger log4j2 could not find a logging implementation
- An article takes you to thoroughly understand descriptors
猜你喜欢
[Business Research Report] top ten trends of science and technology and it in 2022 - with download link
Reading and visualization of DICOM, MHD and raw files in medical imaging
Flutter tips: various fancy nesting of listview and pageview
MySQL in-depth learning - index creation and deletion, index design principles, index failure scenarios, query optimization, index push down ICP
Uncover the seven quirky brain circuits necessary for technology leaders
Live broadcast preview | container service ack elasticity prediction best practice
【acwing】528. cheese
Key review route of probability theory and mathematical statistics examination
[groovy] closure (Introduction to closure class closure | closure parametertypes and maximumnumberofparameters member usage)
Cookie learning diary 1
随机推荐
Looking at Chinese science and technology from the Winter Olympics: what is the mystery of the high-speed camera that the whole people thank?
[PCL self study: feature9] global aligned spatial distribution (GASD) descriptor (continuously updated)
官宣!第三届云原生编程挑战赛正式启动!
2021 electrician cup (the 12th "China Society of electrical engineering Cup" National Undergraduate electrician mathematical modeling) detailed ideas + codes + references
Séparation et combinaison de la construction du système qualité
Key review route of probability theory and mathematical statistics examination
2022-2028 global and Chinese virtual data storage Market Research Report
Manually implement heap sorting -838 Heap sort
[groovy] closure (closure parameter list rule | default parameter list | do not receive parameters | receive custom parameters)
2021 huashubei mathematical modeling idea + reference + paper
Mxnet imports various libcudarts * so、 libcuda*. So not found
English topic assignment (26)
Private collection project practice sharing [Yugong series] February 2022 U3D full stack class 006 unity toolbar
Flink集群配置
自动语音识别(ASR)研究综述
MySQL in-depth learning - index creation and deletion, index design principles, index failure scenarios, query optimization, index push down ICP
The principle of attention mechanism and its application in seq2seq (bahadanau attention)
Data security -- 14 -- Analysis of privacy protection governance
Web开发人员应该养成的10个编程习惯
How to carry out "small step reconstruction"?