当前位置:网站首页>【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 !
边栏推荐
- SQL set operation
- Introduce Hamming distance and calculation examples
- [crampon programming] lintcode decoding Encyclopedia - 872 termination process
- Solutions and answers for the 2021 Shenzhen cup
- Leetcode 222 number of nodes of complete binary tree
- Matplotlib draws three-dimensional scatter and surface graphs
- Hypothesis testing -- learning notes of Chapter 8 of probability theory and mathematical statistics
- Introduction to JVM principle and process
- What are the building energy-saving software
- MySQL audit log Archive
猜你喜欢
Setting up redis cluster cluster under Windows
Matplotlib draws three-dimensional scatter and surface graphs
Private collection project practice sharing [Yugong series] February 2022 U3D full stack class 006 unity toolbar
What are the building energy-saving software
【acwing】836. Merge sets
AutoCAD - command repetition, undo and redo
[goweb development] Introduction to authentication modes based on cookies, sessions and JWT tokens
3 minutes learn to create Google account and email detailed tutorial!
AutoCAD - stretching
On-off and on-off of quality system construction
随机推荐
[groovy] closure (closure parameter list rule | default parameter list | do not receive parameters | receive custom parameters)
2022 U.S. college students' mathematical modeling e problem ideas / 2022 U.S. game e problem analysis
[groovy] closure (closure call | closure default parameter it | code example)
jmeter -- 分布式压测
Advanced length of redis -- deletion strategy, master-slave replication, sentinel mode
Neural networks and deep learning Chapter 4: feedforward neural networks reading questions
【acwing】528. cheese
Function overloading
#775 Div.1 B. Integral Array 数学
AutoCAD - Center zoom
中国聚氨酯硬泡市场调研与投资预测报告(2022版)
Solutions and answers for the 2021 Shenzhen cup
Introduce Hamming distance and calculation examples
Hypothesis testing -- learning notes of Chapter 8 of probability theory and mathematical statistics
次小生成树
Decryption function calculates "task state and lifecycle management" of asynchronous task capability
AutoCAD - Zoom previous
The remainder operation is a hash function
2021 electrician cup idea + code - photovoltaic building integration plate index development trend analysis and prediction: prediction planning issues
[groovy] closure (closure parameter binding | curry function | rcurry function | ncurry function | code example)