1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-11 02:58:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/拆炸弹 [defuse-the-bomb].html
2022-03-29 12:43:11 +08:00

51 lines
2.0 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>你有一个炸弹需要拆除,时间紧迫!你的情报员会给你一个长度为 <code>n</code> 的 <strong>循环</strong> 数组 <code>code</code> 以及一个密钥 <code>k</code> 。</p>
<p>为了获得正确的密码,你需要替换掉每一个数字。所有数字会 <strong>同时</strong> 被替换。</p>
<ul>
<li>如果 <code>k > 0</code> ,将第 <code>i</code> 个数字用 <strong>接下来</strong> <code>k</code> 个数字之和替换。</li>
<li>如果 <code>k < 0</code> ,将第 <code>i</code> 个数字用 <strong>之前</strong> <code>k</code> 个数字之和替换。</li>
<li>如果 <code>k == 0</code> ,将第 <code>i</code> 个数字用 <code>0</code> 替换。</li>
</ul>
<p>由于 <code>code</code> 是循环的, <code>code[n-1]</code> 下一个元素是 <code>code[0]</code> ,且 <code>code[0]</code> 前一个元素是 <code>code[n-1]</code> 。</p>
<p>给你 <strong>循环</strong> 数组 <code>code</code> 和整数密钥 <code>k</code> ,请你返回解密后的结果来拆除炸弹!</p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre>
<b>输入:</b>code = [5,7,1,4], k = 3
<b>输出:</b>[12,10,16,13]
<b>解释:</b>每个数字都被接下来 3 个数字之和替换。解密后的密码为 [7+1+4, 1+4+5, 4+5+7, 5+7+1]。注意到数组是循环连接的。
</pre>
<p><strong>示例 2</strong></p>
<pre>
<b>输入:</b>code = [1,2,3,4], k = 0
<b>输出:</b>[0,0,0,0]
<b>解释:</b>当 k 为 0 时,所有数字都被 0 替换。
</pre>
<p><strong>示例 3</strong></p>
<pre>
<b>输入:</b>code = [2,4,9,3], k = -2
<b>输出:</b>[12,5,6,13]
<b>解释:</b>解密后的密码为 [3+9, 2+3, 4+2, 9+4] 。注意到数组是循环连接的。如果 k 是负数,那么和为 <strong>之前</strong> 的数字。
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>n == code.length</code></li>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= code[i] <= 100</code></li>
<li><code>-(n - 1) <= k <= n - 1</code></li>
</ul>