mirror of
https://github.com/Kiritow/OJ-Problems-Source.git
synced 2024-03-22 13:11:29 +08:00
Create 1652.cpp
This commit is contained in:
parent
c88ede9274
commit
1be36799dd
37
LeetCode/1652.cpp
Normal file
37
LeetCode/1652.cpp
Normal file
|
@ -0,0 +1,37 @@
|
|||
|
||||
class Solution {
|
||||
public:
|
||||
vector<int> decrypt(vector<int>& code, int k) {
|
||||
if (k == 0) {
|
||||
return vector<int>(code.size(), 0);
|
||||
}
|
||||
|
||||
int n = code.size();
|
||||
|
||||
vector<int> 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;
|
||||
}
|
||||
};
|
Loading…
Reference in New Issue
Block a user