diff --git a/LeetCode/1652.cpp b/LeetCode/1652.cpp new file mode 100644 index 0000000..a3cdcbd --- /dev/null +++ b/LeetCode/1652.cpp @@ -0,0 +1,37 @@ + +class Solution { +public: + vector decrypt(vector& code, int k) { + if (k == 0) { + return vector(code.size(), 0); + } + + int n = code.size(); + + vector result; + + if (k > 0) { + for (int i = 0; i < n; i++) { + int sum = 0; + for (int j = 0; j < k; j++) { + int index = (i + 1 + j) % n; + sum += code[index]; + } + result.push_back(sum); + } + } + else { + for (int i = 0; i < n; i++) { + int sum = 0; + for (int j = 0; j < -k; j++) { + int index = (i - 1 - j); + if (index < 0) index += n; + sum += code[index]; + } + result.push_back(sum); + } + } + + return result; + } +};