leetcode update

This commit is contained in:
gameloader 2024-11-23 16:58:05 +08:00
parent 3013423832
commit 7687287272

View File

@ -18182,3 +18182,53 @@ public:
}; };
``` ```
## day260 2024-11-23
### 1861. Rotating the Box
You are given an m x n matrix of characters box representing a side-view of a box. Each cell of the box is one of the following:
A stone '#'
A stationary obstacle '*'
Empty '.'
The box is rotated 90 degrees clockwise, causing some of the stones to fall due to gravity. Each stone falls down until it lands on an obstacle, another stone, or the bottom of the box. Gravity does not affect the obstacles' positions, and the inertia from the box's rotation does not affect the stones' horizontal positions.
It is guaranteed that each stone in box rests on an obstacle, another stone, or the bottom of the box.
Return an n x m matrix representing the box after the rotation described above.
![1123pEbOZz2P5uHK](https://testingcf.jsdelivr.net/gh/game-loader/picbase@master/uPic/1123pEbOZz2P5uHK.png)
### 题解
本题图里的石头挺有意思的像哪里来的魔法石。题目直观的想象是将一个容器翻转90°翻转后石头会自由落体直到落到一块比较坚实的“地面”上。考虑原来的行和翻转后的列的关系原来的第一行翻转后会变为第一列如果共有m行n列那么原来位于(p,q)位置的会变为位于(q,m-p-1)。先构建一个n x m的空数组遍历原始的m x n数组当按行遍历时统计该行中遇到过的石头的个数直到遇到一个障碍物此时将该障碍物按照转换后的位置填入空数组中并根据该障碍物前面的石头个数在空数组中该障碍物的位置向上填充对应数量的石头。如此反复即得最终转换后的数组。
### 代码
```cpp
class Solution {
public:
vector<vector<char>> rotateTheBox(vector<vector<char>>& box) {
int m = box.size();
int n = box[0].size();
vector<vector<char>> rotate(n,(vector<char>(m,'.')));
int stones = 0;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if (box[i][j] == '#'){
stones++;
}else if(box[i][j] == '*'){
rotate[j][m-i-1] = '*';
while(stones > 0){
rotate[j-stones][m-i-1] = '#';
stones--;
}
}
}
while(stones > 0){
rotate[n-stones][m-i-1] = '#';
stones--;
}
}
return rotate;
}
};
```