当前位置:网站首页>【Leetcode】1352. Product of the last K numbers
【Leetcode】1352. Product of the last K numbers
2022-07-05 04:50:00 【wangzirui32】
Bowen author wangzirui32
Like can give the thumbs-up Collection Pay attention to ~~
This article was first published in CSDN, Reprint is prohibited without permission
1. Title Description

source : Power button (LeetCode)
link :https://leetcode.cn/problems/product-of-the-last-k-numbers/
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
2. Their thinking
This problem is a little difficult , We can't record every time add Added number , So every time getProduct We have to calculate the product , Time consuming ( I use this method to write code to display timeout ), therefore , We need to change our thinking : Can we each time add By the way, calculate the product and store it in the list ? Sure , Suppose we enter the following length 4 A list of :
| Indexes | 0 | 1 | 2 | 3 |
|---|---|---|---|---|
| The number | a a a | b b b | c c c | d d d |
then , We turn it into a new list :
| Indexes | 0 | 1 | 2 | 3 |
|---|---|---|---|---|
| The number | a a a | a b ab ab | a b c abc abc | a b c d abcd abcd |
You can see , The values in turn become ab, abc Equal product , Suppose we need to calculate K The product of numbers ( hypothesis k = 2 k=2 k=2), It's equivalent to asking for c d cd cd The product of the , Namely a b c d / a b = c d abcd / ab = cd abcd/ab=cd, That is, the index is divided by the penultimate third , introduce K K K It can be expressed as :
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 implementation
Code:
class ProductOfNumbers:
def __init__(self):
self.product_list = [1] # The initial value is 1
def add(self, num):
if num == 0: # If 0 The products after that are 0 So reset the list
self.product_list = [1]
else:
# Multiply by the last item in the list Can appear a ab abc A list of classes
self.product_list.append(num * self.product_list[-1])
def getProduct(self, k):
if k >= len(self.product_list):
# k Greater than length returns 0
return 0
else:
# result = nums[-1] / nums[-k-1]
return self.product_list[-1] / self.product_list[-k-1]
4. final result

Okay , That's all for today's lesson , I am a wangzirui32, You can collect and pay attention to what you like , See you next time !
边栏推荐
- AutoCAD - full screen display
- "Measuring curve length" of CAD dream drawing
- MySQL audit log Archive
- Decryption function calculates "task state and lifecycle management" of asynchronous task capability
- 2022 thinking of mathematical modeling D problem of American college students / analysis of 2022 American competition D problem
- 数论函数及其求和 待更新
- Raki's notes on reading paper: soft gazetteers for low resource named entity recognition
- 中国针状焦行业发展研究与投资价值报告(2022版)
- The remainder operation is a hash function
- 2021 Higher Education Club Cup mathematical modeling national tournament ABCD problem - problem solving ideas
猜你喜欢
![[groovy] closure closure (customize closure parameters | customize a single closure parameter | customize multiple closure parameters | specify the default value of closure parameters)](/img/92/937122b059b6f3a91ae0e0858685e7.jpg)
[groovy] closure closure (customize closure parameters | customize a single closure parameter | customize multiple closure parameters | specify the default value of closure parameters)

Wan broadband access technology V EPON Technology

windows下Redis-cluster集群搭建

MySQL in-depth learning - index creation and deletion, index design principles, index failure scenarios, query optimization, index push down ICP

XSS injection

2021 huashubei mathematical modeling idea + reference + paper

Discussion on the dimension of confrontation subspace

10 programming habits that web developers should develop

Use assimp library to read MTL file data

Séparation et combinaison de la construction du système qualité
随机推荐
Download the details and sequence of the original data access from the ENA database in EBI
AutoCAD - feature matching
[Chongqing Guangdong education] National Open University 2047t commercial bank operation and management reference test in autumn 2018
[ideas] 2021 may day mathematical modeling competition / May Day mathematical modeling ideas + references + codes
QT Bluetooth: a class for searching Bluetooth devices -- qbluetooth devicediscoveryagent
669. 修剪二叉搜索树 ●●
A survey of automatic speech recognition (ASR) research
2021 electrician cup idea + code - photovoltaic building integration plate index development trend analysis and prediction: prediction planning issues
10 programming habits that web developers should develop
Use assimp library to read MTL file data
[crampon programming] lintcode decoding Encyclopedia - 872 termination process
On-off and on-off of quality system construction
AutoCAD - stretching
中国艾草行业研究与投资前景预测报告(2022版)
Solutions and answers for the 2021 Shenzhen cup
AutoCAD - scaling
Variable category (automatic, static, register, external)
Pointer function (basic)
Flutter tips: various fancy nesting of listview and pageview
Special information | finance, accounting, audit - 22.1.23