Class Solution

java.lang.Object
g1601_1700.s1652_defuse_the_bomb.Solution

public class Solution extends Object
1652 - Defuse the Bomb.<p>Easy</p> <p>You have a bomb to defuse, and your time is running out! Your informer will provide you with a <strong>circular</strong> array <code>code</code> of length of <code>n</code> and a key <code>k</code>.</p> <p>To decrypt the code, you must replace every number. All the numbers are replaced <strong>simultaneously</strong>.</p> <ul> <li>If <code>k > 0</code>, replace the <code>i<sup>th</sup></code> number with the sum of the <strong>next</strong> <code>k</code> numbers.</li> <li>If <code>k < 0</code>, replace the <code>i<sup>th</sup></code> number with the sum of the <strong>previous</strong> <code>k</code> numbers.</li> <li>If <code>k == 0</code>, replace the <code>i<sup>th</sup></code> number with <code>0</code>.</li> </ul> <p>As <code>code</code> is circular, the next element of <code>code[n-1]</code> is <code>code[0]</code>, and the previous element of <code>code[0]</code> is <code>code[n-1]</code>.</p> <p>Given the <strong>circular</strong> array <code>code</code> and an integer key <code>k</code>, return <em>the decrypted code to defuse the bomb</em>!</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> code = [5,7,1,4], k = 3</p> <p><strong>Output:</strong> [12,10,16,13]</p> <p><strong>Explanation:</strong> Each number is replaced by the sum of the next 3 numbers. The decrypted code is [7+1+4, 1+4+5, 4+5+7, 5+7+1]. Notice that the numbers wrap around.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> code = [1,2,3,4], k = 0</p> <p><strong>Output:</strong> [0,0,0,0]</p> <p><strong>Explanation:</strong> When k is zero, the numbers are replaced by 0.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> code = [2,4,9,3], k = -2</p> <p><strong>Output:</strong> [12,5,6,13]</p> <p><strong>Explanation:</strong> The decrypted code is [3+9, 2+3, 4+2, 9+4]. Notice that the numbers wrap around again. If k is negative, the sum is of the <strong>previous</strong> numbers.</p> <p><strong>Constraints:</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>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • decrypt

      public int[] decrypt(int[] code, int k)