-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1861-Rotating-the-Box.js
40 lines (40 loc) · 1.16 KB
/
1861-Rotating-the-Box.js
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
37
38
39
40
/**
* @param {character[][]} boxGrid
* @return {character[][]}
*/
var rotateTheBox = function(boxGrid) {
let count = 0;
for (let row = 0; row < boxGrid.length; row++) {
count = 0;
for (let col = 0; col < boxGrid[row].length; col++) {
if (boxGrid[row][col] === '#') {
boxGrid[row][col] = '.';
count += 1;
}
if (boxGrid[row][col] === '*' && count > 0) {
let tempcol = col - 1;
while (count > 0) {
boxGrid[row][tempcol--] = '#';
count--;
}
count = 0;
}
if (col === boxGrid[row].length - 1 && count > 0) {
let tempcol = col;
while (count > 0) {
boxGrid[row][tempcol--] = '#';
count--;
}
count = 0;
}
}
}
const res = [];
for (let col = 0; col < boxGrid[0].length; col++) {
res[col] = [];
for (let row = boxGrid.length - 1; row >= 0; row--) {
res[col].push(boxGrid[row][col]);
}
}
return res;
};