当前位置:网站首页>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

边栏推荐
- 金仓数据库KingbaseES 插件kdb_database_link
- Wine travel Jianghu War: Ctrip is strong, meituan is strong, and Tiktok is fighting
- 2022 t elevator repair simulation examination question bank and t elevator repair simulation examination question bank
- 使用BENCHMARKSQL工具对KingbaseES预热数据时执行:select sys_prewarm(‘NDX_OORDER_2 ‘)报错
- 金仓KFS数据双向同步场景部署
- 金仓数据库KingbaseES 插件kdb_exists_expand
- FISCO bcos zero knowledge proof Fiat Shamir instance source code
- 220214c language learning diary
- Daily question - ugly number
- Php+mysql registration landing page development complete code
猜你喜欢
![[文献阅读] Sparsity in Deep Learning: Pruning and growth for efficient inference and training in NN](/img/7e/50fa6f65b5a4f0bb60909f57daff56.png)
[文献阅读] Sparsity in Deep Learning: Pruning and growth for efficient inference and training in NN

Php+mysql registration landing page development complete code

SSM based campus part-time platform for College Students

P35-P41 fourth_ context

Joint set search: merge intervals and ask whether two numbers are in the same set

Bugku CTF daily question baby_ flag. txt

Two points -leetcode-540 A single element in an ordered array

Joint search set: the number of points in connected blocks (the number of points in a set)

4 years of experience to interview test development, 10 minutes to end, ask too

X-ray normal based contour rendering
随机推荐
Php+mysql registration landing page development complete code
Kingbasees plug-in KDB of Jincang database_ database_ link
Ffmpeg mix
Smart contract security audit company selection analysis and audit report resources download - domestic article
After reviewing MySQL for a month, I was stunned when the interviewer of Alibaba asked me
跨境电商多商户系统怎么选
Prefix and (continuously updated)
RSRS指标择时及大小盘轮动
[set theory] Cartesian product (concept of Cartesian product | examples of Cartesian product | properties of Cartesian product | non commutativity | non associativity | distribution law | ordered pair
X-ray normal based contour rendering
使用BENCHMARKSQL工具对KingbaseES预热数据时执行:select sys_prewarm(‘NDX_OORDER_2 ‘)报错
How to retrieve the password for opening word files
Matplotlib -- save graph
Dive into deep learning - 2.1 data operation & Exercise
Dismantle a 100000 yuan BYD "Yuan". Come and see what components are in it.
JVM原理简介
2022 tea master (intermediate) examination questions and tea master (intermediate) examination skills
Why should programmers learn microservice architecture if they want to enter a large factory?
Solve BP Chinese garbled code
4 years of experience to interview test development, 10 minutes to end, ask too