当前位置:网站首页>Daily question 7-1652 Defuse the bomb
Daily question 7-1652 Defuse the bomb
2022-06-23 11:15:00 【Programmed ape without hair loss 2】
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
Ideas :
Using prefixes and arrays , Don't count every time , And the results are still in code To increase the complexity of space .
java Code :
class Solution {
public int[] decrypt(int[] code, int k) {
int n = code.length;
// A special case k == 0
if(k == 0) return new int[n];
// Prefixes and arrays , Easy to calculate k The number and
int[] sum = new int[n];
sum[0] = code[0];
for(int i = 1; i < n; ++i) {
sum[i] = sum[i-1] + code[i];
}
// In order not to judge more than once in the loop k Plus or minus , There are two cases to write directly
if(k > 0) {
for(int i = 0; i < n; ++i) {
int x = i + k;
if(x < n) {
code[i] = sum[x] - sum[i];
} else {
code[i] = sum[n-1] - sum[i] + sum[x-n];
}
}
} else {
for(int i = 0; i < n; ++i) {
int x = i + k;
if(x > 0) {
code[i] = sum[i-1] - sum[x-1];
} else {
// i = 0 when , No, sum[i-1], Special judgment required
code[i] = i == 0 ? 0 : sum[i-1];
code[i] += sum[n-1] - sum[n-1+x];
}
}
}
return code;
}
}
边栏推荐
- Noi OJ 1.2 06: round floating point numbers to zero
- 运行时应用自我保护(RASP):应用安全的自我修养
- Deveco device tool helps openharmony device development
- Win10 微软输入法(微软拼音) 不显示 选字栏(无法选字) 解决方法
- Tensorrt notes (4) Modèle de segmentation du raisonnement
- 最简单DIY基于51单片机、PCA9685、IIC、云台的舵机集群控制程序
- Tensorrt notes (IV) reasoning segmentation model
- Noi OJ 1.3 05: floating point numeric C language for calculating fractions
- php 正则表达式
- 最简单DIY串口蓝牙硬件实现方案
猜你喜欢

Flutter series: wrap in flutter

Vone news | wanglian technology empowers the public to enjoy the self-organization management of the chain network, creating an enterprise level alliance Dao

Maui uses Masa blazor component library

Win10 Microsoft input method (Microsoft Pinyin) does not display the word selection column (unable to select words) solution

New technology aesthetics and original biological networking operating system reshape the whole house intelligence

L'outil de périphérique deveco aide au développement de périphériques openharmony

新派科技美学、原生物联网操作系统重塑全屋智能

ESP32-CAM高性价比温湿度监控系统配网与上网方案设计与实现

Install the typescript environment and enable vscode to automatically monitor the compiled TS file as a JS file

Step by step introduction to sqlsugar based development framework (9) -- Realizing field permission control with WinForm control
随机推荐
程序中创建一个子进程,然后父子进程各自独自运行,父进程在标准输入设备上读入小写字母,写入管道。子进程从管道读取字符并转化为大写字母。读到x结束
实现常用C语言字符串处理函数
技术创造价值,手把手教你薅羊毛篇
list的介绍及使用
flutter系列之:flutter中的Wrap
经济小常识
TTY驱动框架
L'outil de périphérique deveco aide au développement de périphériques openharmony
UART的奇偶校验
Rancher 2.6 全新 Monitoring 快速入门
What does NFTs, Web3 and metauniverse mean for digital marketing?
PHP regular expression
Noi OJ 1.4 03: odd even judging C language
SPI与IIC异同
最简单DIY基于蓝牙、51单片机和舵机的钢铁爱国者机关枪控制器
【MATLAB 图形用户界面控件使用】
Design and implementation of esp32-cam wireless monitoring intelligent gateway
Why does the pointer not change the corresponding value as a formal parameter
Esp32-cam, esp8266, WiFi, Bluetooth, MCU, hotspot create embedded DNS server
[use of Matlab graphical user interface controls]