当前位置:网站首页>【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 !
边栏推荐
- Special information | finance, accounting, audit - 22.1.23
- 2021 huashubei mathematical modeling idea + reference + paper
- The remainder operation is a hash function
- History of web page requests
- Key review route of probability theory and mathematical statistics examination
- 775 Div.1 C. Tyler and strings combinatorial mathematics
- 質量體系建設之路的分分合合
- A survey of automatic speech recognition (ASR) research
- An article takes you to thoroughly understand descriptors
- Stage experience
猜你喜欢
Thematic information | carbon, carbon neutrality, low carbon, carbon emissions - 22.1.9
Group counting notes (1) - check code, original complement multiplication and division calculation, floating point calculation
windows下Redis-cluster集群搭建
Special information | real estate and office buildings - 22.1.9
Understand encodefloatrgba and decodefloatrgba
Solution of circular dependency
[groovy] closure (closure as function parameter | code example)
AutoCAD - command repetition, undo and redo
10 programming habits that web developers should develop
54. 螺旋矩阵 & 59. 螺旋矩阵 II ●●
随机推荐
Minor spanning tree
AutoCAD -- dimension break
Neural networks and deep learning Chapter 5: convolutional neural networks reading questions
質量體系建設之路的分分合合
中国AS树脂市场调研与投资预测报告(2022版)
[AI bulletin 20220211] the hard core up owner has built a lidar and detailed AI accelerator
Introduction to JVM principle and process
Manually implement heap sorting -838 Heap sort
Decryption function calculates "task state and lifecycle management" of asynchronous task capability
Neural network and deep learning Chapter 1: introduction reading questions
Neural networks and deep learning Chapter 2: machine learning overview reading questions
AutoCAD - isometric annotation
The 22nd Spring Festival Gala, an immersive stage for the yuan universe to shine into reality
[groovy] closure (Introduction to closure class closure | this, owner, delegate member assignment and source code analysis)
Neural networks and deep learning Chapter 3: linear model reading questions
Fluent objects and lists
2022-2028 global and Chinese equipment as a Service Market Research Report
AutoCAD - scaling
Autocad-- dynamic zoom
Private collection project practice sharing [Yugong series] February 2022 U3D full stack class 006 unity toolbar