Skip to content

csharp array

Posted on:October 24, 2023 at 04:06 AM

数组

public class Solution {
    public int[] Decrypt(int[] code, int k) {
int n = code.Length;
    int[] result = new int[n];

    for (int i = 0; i < n; i++) {
        if (k > 0) {
            for (int j = 1; j <= k; j++) {
                result[i] += code[(i + j) % n];
            }
        } else if (k < 0) {
            for (int j = 1; j <= -k; j++) {
                result[i] += code[(i - j + n) % n];
            }
        }
    }

    return result;
    }
}