Skip to content

Commit 36955f3

Browse files
authored
Create 26 September Rotate Deque By K (#897)
2 parents 8ae5c73 + caef408 commit 36955f3

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

26 September Rotate Deque By K

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
void rotateDeque(deque<int>& dq, int type, int k) {
4+
int n = dq.size();
5+
if (n == 0) return;
6+
7+
k %= n; // no need to rotate more than n times
8+
if (k == 0) return;
9+
10+
if (type == 1) {
11+
// Right rotation: move last k elements to front
12+
rotate(dq.begin(), dq.end() - k, dq.end());
13+
}
14+
else if (type == 2) {
15+
// Left rotation: move first k elements to back
16+
rotate(dq.begin(), dq.begin() + k, dq.end());
17+
}
18+
}
19+
};
20+

0 commit comments

Comments
 (0)