当前位置:网站首页>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
边栏推荐
- [untitled] 2022 safety production supervisor examination question bank and simulated safety production supervisor examination questions
- Bugku CTF daily question baby_ flag. txt
- How to process the current cell with a custom formula in conditional format- How to address the current cell in conditional format custom formula?
- Two points -leetcode-540 A single element in an ordered array
- RSRS指标择时及大小盘轮动
- Reptile exercise 02
- Xrandr modifier la résolution et le taux de rafraîchissement
- Some information about the developer environment in Chengdu
- 2022 electrician (Advanced) examination papers and electrician (Advanced) examination skills
- Joint set search: merge intervals and ask whether two numbers are in the same set
猜你喜欢
Two drawing interfaces - 1 Matlab style interface
Asp access teaching management system design finished product
The programmer went to bed at 12 o'clock in the middle of the night, and the leader angrily scolded: go to bed so early, you are very good at keeping fit
After reviewing MySQL for a month, I was stunned when the interviewer of Alibaba asked me
I've been in software testing for 8 years and worked as a test leader for 3 years. I can also be a programmer if I'm not a professional
2022 chemical automation control instrument examination summary and chemical automation control instrument certificate examination
Why should programmers learn microservice architecture if they want to enter a large factory?
JVM原理简介
Solve BP Chinese garbled code
Integration of Android high-frequency interview questions (including reference answers)
随机推荐
After job hopping at the end of the year, I interviewed more than 30 companies in two weeks and finally landed
使用BENCHMARKSQL工具对KingbaseES预热数据时执行:select sys_prewarm(‘NDX_OORDER_2 ‘)报错
[Thesis Writing] how to write the overall design of JSP tourism network
Matplotlib -- save graph
Which Bluetooth headset is good about 400? Four Bluetooth headsets with strong noise reduction are recommended
Pyqt control part (II)
有道云笔记
FFMpeg example
Joint set search: merge intervals and ask whether two numbers are in the same set
2022 P cylinder filling test content and P cylinder filling simulation test questions
拆一辆十万元的比亚迪“元”,快来看看里面的有哪些元器件。
2022 beautician (intermediate) new version test questions and beautician (intermediate) certificate examination
使用BENCHMARKSQL工具对KingbaseES执行测试时报错funcs sh file not found
Arthas watch grabs a field / attribute of the input parameter
商城系统搭建完成后需要设置哪些功能
What functions need to be set after the mall system is built
JVM原理简介
Kingbasees plug-in KDB of Jincang database_ database_ link
Competitive product analysis and writing
[pat (basic level) practice] - [simple simulation] 1063 calculate the spectral radius