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

原网站

版权声明
本文为[Programmed ape without hair loss 2]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206231051261353.html