当前位置:网站首页>【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,喜欢的可以点个收藏和关注,我们下次再见!
边栏推荐
- 【acwing】528. cheese
- CSDN正文自动生成目录
- Mxnet imports various libcudarts * so、 libcuda*. So not found
- windows下Redis-cluster集群搭建
- [Chongqing Guangdong education] National Open University 2047t commercial bank operation and management reference test in autumn 2018
- Introduce Hamming distance and calculation examples
- XSS injection
- 直播预告 | 容器服务 ACK 弹性预测最佳实践
- [groovy] closure (closure call is associated with call method | call () method is defined in interface | call () method is defined in class | code example)
- 2022 thinking of Mathematical Modeling B problem of American college students / analysis of 2022 American competition B problem
猜你喜欢

Setting up redis cluster cluster under Windows

Solutions and answers for the 2021 Shenzhen cup

Group counting notes (1) - check code, original complement multiplication and division calculation, floating point calculation

Flutter tips: various fancy nesting of listview and pageview

直播预告 | 容器服务 ACK 弹性预测最佳实践

Detailed introduction of OSPF header message

2022-2028 global and Chinese FPGA prototype system Market Research Report

AutoCAD - feature matching
![Rip notes [rip message security authentication, increase of rip interface measurement]](/img/89/f70af97676496d7b9aa867be89f11d.jpg)
Rip notes [rip message security authentication, increase of rip interface measurement]

Looking at Chinese science and technology from the Winter Olympics: what is the mystery of the high-speed camera that the whole people thank?
随机推荐
Managed service network: application architecture evolution in the cloud native Era
The remainder operation is a hash function
Neural network and deep learning Chapter 1: introduction reading questions
Setting up redis cluster cluster under Windows
【acwing】528. cheese
Key review route of probability theory and mathematical statistics examination
The principle of attention mechanism and its application in seq2seq (bahadanau attention)
[ideas] 2021 may day mathematical modeling competition / May Day mathematical modeling ideas + references + codes
Raki's notes on reading paper: code and named entity recognition in stackoverflow
2022 American College Students' mathematical modeling ABCDEF problem thinking /2022 American match ABCDEF problem analysis
English topic assignment (26)
[groovy] closure (closure parameter binding | curry function | rcurry function | ncurry function | code example)
Detailed introduction of OSPF header message
How can CIOs use business analysis to build business value?
#775 Div.1 C. Tyler and Strings 组合数学
Rip notes [rip three timers, the role of horizontal segmentation, rip automatic summary, and the role of network]
JVM 原理和流程简介
#775 Div.1 B. Integral Array 数学
Web开发人员应该养成的10个编程习惯
Wenet: E2E speech recognition tool for industrial implementation