当前位置:网站首页>【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,喜欢的可以点个收藏和关注,我们下次再见!
边栏推荐
- 首席信息官如何利用业务分析构建业务价值?
- Observable time series data downsampling practice in Prometheus
- 程序员应该怎么学数学
- Practice | mobile end practice
- 775 Div.1 B. integral array mathematics
- CUDA Programming atomic operation atomicadd reports error err:msb3721, return code 1
- CSDN正文自动生成目录
- Basic analysis of IIC SPI protocol
- 775 Div.1 C. Tyler and strings combinatorial mathematics
- Leetcode 222 number of nodes of complete binary tree
猜你喜欢
[groovy] closure closure (customize closure parameters | customize a single closure parameter | customize multiple closure parameters | specify the default value of closure parameters)
On-off and on-off of quality system construction
Detailed introduction of OSPF header message
windows下Redis-cluster集群搭建
CSDN body auto generate directory
解密函数计算异步任务能力之「任务的状态及生命周期管理」
2022 thinking of mathematical modeling C problem of American college students / analysis of 2022 American competition C problem
XSS injection
Rip notes [rip message security authentication, increase of rip interface measurement]
Hypothesis testing -- learning notes of Chapter 8 of probability theory and mathematical statistics
随机推荐
windows下Redis-cluster集群搭建
English topic assignment (26)
Neural networks and deep learning Chapter 6: Circular neural networks reading questions
【acwing】528. cheese
Interface joint commissioning test script optimization V5.0 (end)
Introduce Hamming distance and calculation examples
Live broadcast preview | container service ack elasticity prediction best practice
Advanced length of redis -- deletion strategy, master-slave replication, sentinel mode
Introduction to RT thread kernel (4) -- clock management
取余操作是一个哈希函数
Practice | mobile end practice
【acwing】240. food chain
Mxnet imports various libcudarts * so、 libcuda*. So not found
Power management bus (pmbus)
Flutter tips: various fancy nesting of listview and pageview
托管式服务网络:云原生时代的应用体系架构进化
A survey of automatic speech recognition (ASR) research
猿人学第一题
Chapter 6 text processing tools for shell programming (awk)
[AI bulletin 20220211] the hard core up owner has built a lidar and detailed AI accelerator