当前位置:网站首页>【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 !
边栏推荐
- Private collection project practice sharing [Yugong series] February 2022 U3D full stack class 006 unity toolbar
- [groovy] closure (Introduction to closure class closure | this, owner, delegate member assignment and source code analysis)
- Group counting notes (1) - check code, original complement multiplication and division calculation, floating point calculation
- Matplotlib draws three-dimensional scatter and surface graphs
- Flutter 小技巧之 ListView 和 PageView 的各种花式嵌套
- 775 Div.1 B. integral array mathematics
- 中国聚氨酯硬泡市场调研与投资预测报告(2022版)
- AutoCAD - Center zoom
- [AI bulletin 20220211] the hard core up owner has built a lidar and detailed AI accelerator
- AutoCAD - isometric annotation
猜你喜欢

【acwing】528. cheese

Create a pyGame window with a blue background

Redis 排查大 key 的4种方法,优化必备

Use assimp library to read MTL file data

Understand encodefloatrgba and decodefloatrgba
![[groovy] closure (closure parameter list rule | default parameter list | do not receive parameters | receive custom parameters)](/img/36/c4206a95c007e41df628d99e06ba18.jpg)
[groovy] closure (closure parameter list rule | default parameter list | do not receive parameters | receive custom parameters)

2022 thinking of Mathematical Modeling B problem of American college students / analysis of 2022 American competition B problem

Emlog blog theme template source code simple good-looking responsive

Key review route of probability theory and mathematical statistics examination

windows下Redis-cluster集群搭建
随机推荐
Solutions and answers for the 2021 Shenzhen cup
質量體系建設之路的分分合合
How should programmers learn mathematics
Variable category (automatic, static, register, external)
[groovy] closure (Introduction to closure class closure | this, owner, delegate member assignment and source code analysis)
中国溶聚丁苯橡胶(SSBR)行业研究与预测报告(2022版)
Inline built-in function
3 minutes learn to create Google account and email detailed tutorial!
Rip notes [rip three timers, the role of horizontal segmentation, rip automatic summary, and the role of network]
2021 electrician Cup - high speed rail traction power supply system operation data analysis and equivalent modeling ideas + code
【acwing】836. Merge sets
Solution of circular dependency
#775 Div.1 C. Tyler and Strings 组合数学
Emlog blog theme template source code simple good-looking responsive
2021 Higher Education Club Cup mathematical modeling national tournament ABCD problem - problem solving ideas
Neural network and deep learning Chapter 1: introduction reading questions
Introduce Hamming distance and calculation examples
Pointer function (basic)
Error statuslogger log4j2 could not find a logging implementation
Function overloading