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

边栏推荐
- Use the benchmarksql tool to perform a data prompt on kingbases. The jdbc driver cannot be found
- MC Layer Target
- Kubernetes源码分析(一)
- P35-P41 fourth_ context
- 金仓数据库KingbaseES 插件kdb_date_function
- What functions need to be set after the mall system is built
- 金仓数据库KingbaseES 插件kdb_exists_expand
- Classes in TS
- 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
- C language series - Section 3 - functions
猜你喜欢

After job hopping at the end of the year, I interviewed more than 30 companies in two weeks and finally landed

Feature_selection

Database management tool, querious direct download

2022 t elevator repair simulation examination question bank and t elevator repair simulation examination question bank

解决bp中文乱码

一名外包仔的2022年中总结

How to choose cross-border e-commerce multi merchant system

BMZCTF simple_ pop

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

A outsourcing boy's mid-2022 summary
随机推荐
220214c language learning diary
[set theory] set concept and relationship (set family | set family examples | multiple sets)
The latest activation free version of Omni toolbox
Database management tool, querious direct download
Know that Chuangyu cloud monitoring - scanv Max update: Ecology OA unauthorized server request forgery and other two vulnerabilities can be detected
Kubernetes source code analysis (I)
Redis persistence principle
Prefix and (continuously updated)
Design and implementation of JSP logistics center storage information management system
Square root of X
[set theory] binary relation (example of binary relation on a | binary relation on a)
Basic use of continuous integration server Jenkins
[fxcg] inflation differences will still lead to the differentiation of monetary policies in various countries
2022 P cylinder filling test content and P cylinder filling simulation test questions
2022 tea master (intermediate) examination questions and tea master (intermediate) examination skills
[set theory] set operation (Union | intersection | disjoint | relative complement | symmetric difference | absolute complement | generalized union | generalized intersection | set operation priority)
Crazy scientist
X-ray normal based contour rendering
跨境电商多商户系统怎么选
Bugku CTF daily question baby_ flag. txt