当前位置:网站首页>【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,喜欢的可以点个收藏和关注,我们下次再见!
边栏推荐
- Web开发人员应该养成的10个编程习惯
- Is $20billion a little less? Cisco is interested in Splunk?
- AutoCAD -- dimension break
- Observable time series data downsampling practice in Prometheus
- 2021 huashubei mathematical modeling idea + reference + paper
- Qt蓝牙:搜索蓝牙设备的类——QBluetoothDeviceDiscoveryAgent
- 官宣!第三届云原生编程挑战赛正式启动!
- Debug insights
- Introduce Hamming distance and calculation examples
- 【acwing】836. Merge sets
猜你喜欢
Special information | finance, accounting, audit - 22.1.23
Introduction to JVM principle and process
2021 higher education social cup mathematical modeling national tournament ABCD questions - problem solving ideas - Mathematical Modeling
Solution of circular dependency
Construction d'un Cluster redis sous Windows
Wan broadband access technology V EPON Technology
[groovy] closure (closure call is associated with call method | call () method is defined in interface | call () method is defined in class | code example)
自动语音识别(ASR)研究综述
AutoCAD - isometric annotation
2022 American College Students' mathematical modeling ABCDEF problem thinking /2022 American match ABCDEF problem analysis
随机推荐
MD5绕过
2021 electrician cup (the 12th "China Society of electrical engineering Cup" National Undergraduate electrician mathematical modeling) detailed ideas + codes + references
Reading and visualization of DICOM, MHD and raw files in medical imaging
[groovy] closure (closure as function parameter | code example)
首席信息官如何利用业务分析构建业务价值?
Data security -- 14 -- Analysis of privacy protection governance
Raki's notes on reading paper: soft gazetteers for low resource named entity recognition
Invalid bound statement (not found) in idea -- problem solving
Raki's notes on reading paper: code and named entity recognition in stackoverflow
Chapter 6 text processing tools for shell programming (awk)
Neural networks and deep learning Chapter 5: convolutional neural networks reading questions
2022-2028 global and Chinese FPGA prototype system Market Research Report
How should programmers learn mathematics
Advanced length of redis -- deletion strategy, master-slave replication, sentinel mode
Special information | real estate and office buildings - 22.1.9
Debug insights
PHP读取ini文件并修改内容写入
Flutter 小技巧之 ListView 和 PageView 的各种花式嵌套
Neural networks and deep learning Chapter 3: linear model reading questions
775 Div.1 B. integral array mathematics