-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrotatingtheBox.cpp
More file actions
36 lines (35 loc) · 1.06 KB
/
rotatingtheBox.cpp
File metadata and controls
36 lines (35 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Source: https://leetcode.com/problems/rotating-the-box/
// Author: Miao Zhang
// Date: 2021-06-15
class Solution {
public:
vector<vector<char>> rotateTheBox(vector<vector<char>>& box) {
int m = box.size();
int n = box[0].size();
for (int i = 0; i < m; i++) {
deque<int> q;
for (int j = n - 1; j >= 0; j--) {
if (box[i][j] == '*') {
q.clear();
} else if (box[i][j] == '#') {
if (!q.empty()) {
int pos = q.front();
q.pop_front();
box[i][pos] = '#';
box[i][j] = '.';
q.push_back(j);
}
} else {
q.push_back(j);
}
}
}
vector<vector<char>> res(n, vector<char>(m));
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
res[j][m - 1 - i] = box[i][j];
}
}
return res;
}
};