Skip to content

Commit 9f79946

Browse files
committed
feat: add solutions to lc problem: No.1861
No.1861.Rotating the Box
1 parent bd392b4 commit 9f79946

File tree

7 files changed

+246
-84
lines changed

7 files changed

+246
-84
lines changed

index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<meta name="description" content="LeetCode、剑指Offer、程序员面试金典题解">
1111
<meta name="viewport"
1212
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
13-
<link rel="stylesheet" href="https://cdn-doocs.oss-cn-shenzhen.aliyuncs.com/npm/docsify/lib/themes/vue.css">
13+
<link rel="stylesheet" href="https://cdn-doocs.oss-cn-shenzhen.aliyuncs.com/npm/docsify@4.13.0/lib/themes/vue.css">
1414
<link rel="stylesheet" href="https://cdn-doocs.oss-cn-shenzhen.aliyuncs.com/npm/docsify-darklight-theme@latest/dist/style.min.css">
1515
<link rel="stylesheet" href="https://cdn-doocs.oss-cn-shenzhen.aliyuncs.com/npm/katex@latest/dist/katex.min.css"/>
1616
<link rel="icon" type="image/png" sizes="32x32" href="images/favicon-32x32.png">
@@ -125,7 +125,7 @@
125125
]
126126
}
127127
</script>
128-
<script src="https://cdn-doocs.oss-cn-shenzhen.aliyuncs.com/npm/docsify/lib/docsify.min.js"></script>
128+
<script src="https://cdn-doocs.oss-cn-shenzhen.aliyuncs.com/npm/docsify@4.13.0/lib/docsify.min.js"></script>
129129
<script src="https://cdn-doocs.oss-cn-shenzhen.aliyuncs.com/npm/prismjs/components/prism-c.min.js"></script>
130130
<script src="https://cdn-doocs.oss-cn-shenzhen.aliyuncs.com/npm/prismjs/components/prism-go.min.js"></script>
131131
<script src="https://cdn-doocs.oss-cn-shenzhen.aliyuncs.com/npm/prismjs/components/prism-sql.min.js"></script>

solution/1800-1899/1861.Rotating the Box/README.md

Lines changed: 88 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,11 @@
7474

7575
<!-- 这里可写通用的实现逻辑 -->
7676

77-
先旋转,再挪动箱子。
77+
**方法一:队列模拟**
78+
79+
我们先将矩阵顺时针旋转 90 度,然后模拟每一列石头的下落过程。
80+
81+
时间复杂度 $O(m \times n)$,空间复杂度 $O(m \times n)$。其中 $m$ 和 $n$ 分别是矩阵的行数和列数。
7882

7983
<!-- tabs:start -->
8084

@@ -86,25 +90,22 @@
8690
class Solution:
8791
def rotateTheBox(self, box: List[List[str]]) -> List[List[str]]:
8892
m, n = len(box), len(box[0])
89-
res = [[None] * m for _ in range(n)]
93+
ans = [[None] * m for _ in range(n)]
9094
for i in range(m):
9195
for j in range(n):
92-
res[j][m - i - 1] = box[i][j]
96+
ans[j][m - i - 1] = box[i][j]
9397
for j in range(m):
9498
q = deque()
9599
for i in range(n - 1, -1, -1):
96-
if res[i][j] == '*':
100+
if ans[i][j] == '*':
97101
q.clear()
98-
continue
99-
if res[i][j] == '.':
102+
elif ans[i][j] == '.':
100103
q.append(i)
101-
else:
102-
if not q:
103-
continue
104-
res[q.popleft()][j] = '#'
105-
res[i][j] = '.'
104+
elif q:
105+
ans[q.popleft()][j] = '#'
106+
ans[i][j] = '.'
106107
q.append(i)
107-
return res
108+
return ans
108109
```
109110

110111
### **Java**
@@ -115,36 +116,98 @@ class Solution:
115116
class Solution {
116117
public char[][] rotateTheBox(char[][] box) {
117118
int m = box.length, n = box[0].length;
118-
char[][] res = new char[n][m];
119+
char[][] ans = new char[n][m];
119120
for (int i = 0; i < m; ++i) {
120121
for (int j = 0; j < n; ++j) {
121-
res[j][m - i - 1] = box[i][j];
122+
ans[j][m - i - 1] = box[i][j];
122123
}
123124
}
124125
for (int j = 0; j < m; ++j) {
125126
Deque<Integer> q = new ArrayDeque<>();
126127
for (int i = n - 1; i >= 0; --i) {
127-
if (res[i][j] == '*') {
128+
if (ans[i][j] == '*') {
128129
q.clear();
129-
continue;
130-
}
131-
if (res[i][j] == '.') {
130+
} else if (ans[i][j] == '.') {
132131
q.offer(i);
133-
} else {
134-
if (q.isEmpty()) {
135-
continue;
136-
}
137-
res[q.poll()][j] = '#';
138-
res[i][j] = '.';
132+
} else if (!q.isEmpty()) {
133+
ans[q.pollFirst()][j] = '#';
134+
ans[i][j] = '.';
139135
q.offer(i);
140136
}
141137
}
142138
}
143-
return res;
139+
return ans;
144140
}
145141
}
146142
```
147143

144+
### **C++**
145+
146+
```cpp
147+
class Solution {
148+
public:
149+
vector<vector<char>> rotateTheBox(vector<vector<char>>& box) {
150+
int m = box.size(), n = box[0].size();
151+
vector<vector<char>> ans(n, vector<char>(m));
152+
for (int i = 0; i < m; ++i) {
153+
for (int j = 0; j < n; ++j) {
154+
ans[j][m - i - 1] = box[i][j];
155+
}
156+
}
157+
for (int j = 0; j < m; ++j) {
158+
queue<int> q;
159+
for (int i = n - 1; ~i; --i) {
160+
if (ans[i][j] == '*') {
161+
queue<int> t;
162+
swap(t, q);
163+
} else if (ans[i][j] == '.') {
164+
q.push(i);
165+
} else if (!q.empty()) {
166+
ans[q.front()][j] = '#';
167+
q.pop();
168+
ans[i][j] = '.';
169+
q.push(i);
170+
}
171+
}
172+
}
173+
return ans;
174+
}
175+
};
176+
```
177+
178+
### **Go**
179+
180+
```go
181+
func rotateTheBox(box [][]byte) [][]byte {
182+
m, n := len(box), len(box[0])
183+
ans := make([][]byte, n)
184+
for i := range ans {
185+
ans[i] = make([]byte, m)
186+
}
187+
for i := 0; i < m; i++ {
188+
for j := 0; j < n; j++ {
189+
ans[j][m-i-1] = box[i][j]
190+
}
191+
}
192+
for j := 0; j < m; j++ {
193+
q := []int{}
194+
for i := n - 1; i >= 0; i-- {
195+
if ans[i][j] == '*' {
196+
q = []int{}
197+
} else if ans[i][j] == '.' {
198+
q = append(q, i)
199+
} else if len(q) > 0 {
200+
ans[q[0]][j] = '#'
201+
q = q[1:]
202+
ans[i][j] = '.'
203+
q = append(q, i)
204+
}
205+
}
206+
}
207+
return ans
208+
}
209+
```
210+
148211
### **...**
149212

150213
```

solution/1800-1899/1861.Rotating the Box/README_EN.md

Lines changed: 83 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,9 @@
77
<p>You are given an <code>m x n</code> matrix of characters <code>box</code> representing a side-view of a box. Each cell of the box is one of the following:</p>
88

99
<ul>
10-
1110
<li>A stone <code>&#39;#&#39;</code></li>
12-
1311
<li>A stationary obstacle <code>&#39;*&#39;</code></li>
14-
1512
<li>Empty <code>&#39;.&#39;</code></li>
16-
1713
</ul>
1814

1915
<p>The box is rotated <strong>90 degrees clockwise</strong>, 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 <strong>does not</strong> affect the obstacles&#39; positions, and the inertia from the box&#39;s rotation <strong>does not </strong>affect the stones&#39; horizontal positions.</p>
@@ -91,15 +87,10 @@
9187
<p><strong>Constraints:</strong></p>
9288

9389
<ul>
94-
9590
<li><code>m == box.length</code></li>
96-
9791
<li><code>n == box[i].length</code></li>
98-
9992
<li><code>1 &lt;= m, n &lt;= 500</code></li>
100-
10193
<li><code>box[i][j]</code> is either <code>&#39;#&#39;</code>, <code>&#39;*&#39;</code>, or <code>&#39;.&#39;</code>.</li>
102-
10394
</ul>
10495

10596
## Solutions
@@ -112,25 +103,22 @@
112103
class Solution:
113104
def rotateTheBox(self, box: List[List[str]]) -> List[List[str]]:
114105
m, n = len(box), len(box[0])
115-
res = [[None] * m for _ in range(n)]
106+
ans = [[None] * m for _ in range(n)]
116107
for i in range(m):
117108
for j in range(n):
118-
res[j][m - i - 1] = box[i][j]
109+
ans[j][m - i - 1] = box[i][j]
119110
for j in range(m):
120111
q = deque()
121112
for i in range(n - 1, -1, -1):
122-
if res[i][j] == '*':
113+
if ans[i][j] == '*':
123114
q.clear()
124-
continue
125-
if res[i][j] == '.':
115+
elif ans[i][j] == '.':
126116
q.append(i)
127-
else:
128-
if not q:
129-
continue
130-
res[q.popleft()][j] = '#'
131-
res[i][j] = '.'
117+
elif q:
118+
ans[q.popleft()][j] = '#'
119+
ans[i][j] = '.'
132120
q.append(i)
133-
return res
121+
return ans
134122
```
135123

136124
### **Java**
@@ -139,33 +127,95 @@ class Solution:
139127
class Solution {
140128
public char[][] rotateTheBox(char[][] box) {
141129
int m = box.length, n = box[0].length;
142-
char[][] res = new char[n][m];
130+
char[][] ans = new char[n][m];
143131
for (int i = 0; i < m; ++i) {
144132
for (int j = 0; j < n; ++j) {
145-
res[j][m - i - 1] = box[i][j];
133+
ans[j][m - i - 1] = box[i][j];
146134
}
147135
}
148136
for (int j = 0; j < m; ++j) {
149137
Deque<Integer> q = new ArrayDeque<>();
150138
for (int i = n - 1; i >= 0; --i) {
151-
if (res[i][j] == '*') {
139+
if (ans[i][j] == '*') {
152140
q.clear();
153-
continue;
154-
}
155-
if (res[i][j] == '.') {
141+
} else if (ans[i][j] == '.') {
156142
q.offer(i);
157-
} else {
158-
if (q.isEmpty()) {
159-
continue;
160-
}
161-
res[q.poll()][j] = '#';
162-
res[i][j] = '.';
143+
} else if (!q.isEmpty()) {
144+
ans[q.pollFirst()][j] = '#';
145+
ans[i][j] = '.';
163146
q.offer(i);
164147
}
165148
}
166149
}
167-
return res;
150+
return ans;
151+
}
152+
}
153+
```
154+
155+
### **C++**
156+
157+
```cpp
158+
class Solution {
159+
public:
160+
vector<vector<char>> rotateTheBox(vector<vector<char>>& box) {
161+
int m = box.size(), n = box[0].size();
162+
vector<vector<char>> ans(n, vector<char>(m));
163+
for (int i = 0; i < m; ++i) {
164+
for (int j = 0; j < n; ++j) {
165+
ans[j][m - i - 1] = box[i][j];
166+
}
167+
}
168+
for (int j = 0; j < m; ++j) {
169+
queue<int> q;
170+
for (int i = n - 1; ~i; --i) {
171+
if (ans[i][j] == '*') {
172+
queue<int> t;
173+
swap(t, q);
174+
} else if (ans[i][j] == '.') {
175+
q.push(i);
176+
} else if (!q.empty()) {
177+
ans[q.front()][j] = '#';
178+
q.pop();
179+
ans[i][j] = '.';
180+
q.push(i);
181+
}
182+
}
183+
}
184+
return ans;
168185
}
186+
};
187+
```
188+
189+
### **Go**
190+
191+
```go
192+
func rotateTheBox(box [][]byte) [][]byte {
193+
m, n := len(box), len(box[0])
194+
ans := make([][]byte, n)
195+
for i := range ans {
196+
ans[i] = make([]byte, m)
197+
}
198+
for i := 0; i < m; i++ {
199+
for j := 0; j < n; j++ {
200+
ans[j][m-i-1] = box[i][j]
201+
}
202+
}
203+
for j := 0; j < m; j++ {
204+
q := []int{}
205+
for i := n - 1; i >= 0; i-- {
206+
if ans[i][j] == '*' {
207+
q = []int{}
208+
} else if ans[i][j] == '.' {
209+
q = append(q, i)
210+
} else if len(q) > 0 {
211+
ans[q[0]][j] = '#'
212+
q = q[1:]
213+
ans[i][j] = '.'
214+
q = append(q, i)
215+
}
216+
}
217+
}
218+
return ans
169219
}
170220
```
171221

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public:
3+
vector<vector<char>> rotateTheBox(vector<vector<char>>& box) {
4+
int m = box.size(), n = box[0].size();
5+
vector<vector<char>> ans(n, vector<char>(m));
6+
for (int i = 0; i < m; ++i) {
7+
for (int j = 0; j < n; ++j) {
8+
ans[j][m - i - 1] = box[i][j];
9+
}
10+
}
11+
for (int j = 0; j < m; ++j) {
12+
queue<int> q;
13+
for (int i = n - 1; ~i; --i) {
14+
if (ans[i][j] == '*') {
15+
queue<int> t;
16+
swap(t, q);
17+
} else if (ans[i][j] == '.') {
18+
q.push(i);
19+
} else if (!q.empty()) {
20+
ans[q.front()][j] = '#';
21+
q.pop();
22+
ans[i][j] = '.';
23+
q.push(i);
24+
}
25+
}
26+
}
27+
return ans;
28+
}
29+
};

0 commit comments

Comments
 (0)