当前位置:网站首页>The simple problem of leetcode: dismantling bombs
The simple problem of leetcode: dismantling bombs
2022-07-03 04:31:00 【·Starry Sea】
subject
You have a bomb to dismantle , Pressed for time ! Your agent will give you a length of n Of loop Array code And a key k .
To get the right password , You need to replace every number . All the numbers will meanwhile Be replaced .
If k > 0 , Will be the first i For numbers Next k The sum of the numbers replaces .
If k < 0 , Will be the first i For numbers Before k The sum of the numbers replaces .
If k == 0 , Will be the first i For numbers 0 Replace .
because code It's cyclical , code[n-1] The next element is code[0] , And code[0] The first element is code[n-1] .
Here you are. loop Array code And integer keys k , Please return the decrypted results to dismantle the bomb !
Example 1:
Input :code = [5,7,1,4], k = 3
Output :[12,10,16,13]
explain : Every number is followed by 3 The sum of the numbers replaces . The decrypted password is [7+1+4, 1+4+5, 4+5+7, 5+7+1]. Notice that arrays are concatenated circularly .
Example 2:
Input :code = [1,2,3,4], k = 0
Output :[0,0,0,0]
explain : When k by 0 when , All the numbers are 0 Replace .
Example 3:
Input :code = [2,4,9,3], k = -2
Output :[12,5,6,13]
explain : The decrypted password is [3+9, 2+3, 4+2, 9+4] . Notice that arrays are concatenated circularly . If k It's a negative number , So, and for Before The number of .
Tips :
n == code.length
1 <= n <= 100
1 <= code[i] <= 100
-(n - 1) <= k <= n - 1
source : Power button (LeetCode)
Their thinking
This question mainly focuses on the access of circular arrays , The calculation of cyclic numbers is a relatively basic content in number theory , This kind of circular access often occurs in the circular queue . In this question, we only need to answer code Each element in can be modified one by one . notes : The modified element does not participate in the new operation , So we need to open up a new space to store the answers .
class Solution:
def decrypt(self, code: List[int], k: int) -> List[int]:
n=len(code)
CODE=[0]*n
if k==0:
return CODE
elif k>0:
for i in range(n):
s=0
for j in range(k):
s+=code[(i+j+1)%n]
CODE[i]=s
else:
for i in range(n):
s=0
for j in range(-k):
s+=code[(i+n-j-1)%n]
CODE[i]=s
return CODE

边栏推荐
- 商城系统搭建完成后需要设置哪些功能
- Basic use of continuous integration server Jenkins
- Ffmpeg mix
- sd卡数据损坏怎么回事,sd卡数据损坏怎么恢复
- Drf--- quick start 01
- 2.14 summary
- Xrandr modify resolution and refresh rate
- [文献阅读] Sparsity in Deep Learning: Pruning and growth for efficient inference and training in NN
- [pat (basic level) practice] - [simple simulation] 1063 calculate the spectral radius
- [set theory] inclusion exclusion principle (including examples of exclusion principle)
猜你喜欢

2022 tea master (intermediate) examination questions and tea master (intermediate) examination skills

Two drawing interfaces - 1 Matlab style interface
![[Thesis Writing] how to write the overall design of JSP tourism network](/img/02/841e8870c2ef871c182b9bb8252a83.jpg)
[Thesis Writing] how to write the overall design of JSP tourism network

When using the benchmarksql tool to test the concurrency of kingbasees, there are sub threads that are not closed in time after the main process is killed successfully

Know that Chuangyu cloud monitoring - scanv Max update: Ecology OA unauthorized server request forgery and other two vulnerabilities can be detected

FuncS sh file not found when using the benchmarksql tool to test kingbases
![[NLP]—sparse neural network最新工作简述](/img/65/35ae0137f4030bdb2b0ab9acd85e16.png)
[NLP]—sparse neural network最新工作简述

使用BENCHMARKSQL工具对kingbasees并发测试时kill掉主进程成功后存在子线程未及时关闭

Some information about the developer environment in Chengdu

stm32逆向入门
随机推荐
When using the benchmarksql tool to preheat data for kingbasees, execute: select sys_ Prewarm ('ndx_oorder_2 ') error
Two points -leetcode-540 A single element in an ordered array
Priv app permission exception
Matplotlib -- save graph
Daily question - ugly number
SSM based campus part-time platform for College Students
2022 t elevator repair simulation examination question bank and t elevator repair simulation examination question bank
Integration of Android high-frequency interview questions (including reference answers)
Smart contract security audit company selection analysis and audit report resources download - domestic article
Xrandr modify resolution and refresh rate
跨境电商多商户系统怎么选
[set theory] binary relationship (binary relationship notation | binary relationship from a to B | number of binary relationships | example of binary relationship)
Some information about the developer environment in Chengdu
[set theory] set concept and relationship (set family | set family examples | multiple sets)
Dismantle a 100000 yuan BYD "Yuan". Come and see what components are in it.
How to use kotlin to improve productivity: kotlin tips
Classes in TS
Kubernetes源码分析(一)
Introduction of pointer variables in function parameters
Competitive product analysis and writing