当前位置:网站首页>【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,喜欢的可以点个收藏和关注,我们下次再见!
边栏推荐
- Mode in BST (binary tree & Notes on question brushing)
- Mxnet imports various libcudarts * so、 libcuda*. So not found
- xss注入
- Key review route of probability theory and mathematical statistics examination
- 数论函数及其求和 待更新
- Rk3399 platform development series explanation (network debugging) 7.29 summary of network performance tools
- CSDN body auto generate directory
- Neural networks and deep learning Chapter 4: feedforward neural networks reading questions
- Invalid bound statement (not found) in idea -- problem solving
- Introduction to JVM principle and process
猜你喜欢

Power management bus (pmbus)

XSS injection

Raki's notes on reading paper: soft gazetteers for low resource named entity recognition
![[groovy] closure (Introduction to closure class closure | this, owner, delegate member assignment and source code analysis)](/img/aa/3c8b7b27e322417777d1315b9a5a8f.jpg)
[groovy] closure (Introduction to closure class closure | this, owner, delegate member assignment and source code analysis)

线上故障突突突?如何紧急诊断、排查与恢复

Looking at Chinese science and technology from the Winter Olympics: what is the mystery of the high-speed camera that the whole people thank?

QT Bluetooth: a class for searching Bluetooth devices -- qbluetooth devicediscoveryagent

49 pictures and 26 questions explain in detail what is WiFi?

JVM 原理和流程简介

直播預告 | 容器服務 ACK 彈性預測最佳實踐
随机推荐
Live broadcast preview | container service ack elasticity prediction best practice
An article takes you to thoroughly understand descriptors
AutoCAD - continuous annotation
JMeter -- distributed pressure measurement
2022 thinking of mathematical modeling D problem of American college students / analysis of 2022 American competition D problem
Debug insights
Reading and visualization of DICOM, MHD and raw files in medical imaging
History of web page requests
Function overloading
揭秘技术 Leader 必备的七大清奇脑回路
Introduction to RT thread kernel (4) -- clock management
Inline built-in function
Private collection project practice sharing [Yugong series] February 2022 U3D full stack class 006 unity toolbar
【acwing】837. Number of connected block points
775 Div.1 C. Tyler and strings combinatorial mathematics
Leetcode 222 number of nodes of complete binary tree
[groovy] closure (closure call is associated with call method | call () method is defined in interface | call () method is defined in class | code example)
2021 electrician cup idea + code - photovoltaic building integration plate index development trend analysis and prediction: prediction planning issues
49 pictures and 26 questions explain in detail what is WiFi?
CSDN body auto generate directory