Skip to content

Latest commit

 

History

History
178 lines (117 loc) · 5.26 KB

File metadata and controls

178 lines (117 loc) · 5.26 KB

中文文档

Description

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:

    <li>A stone <code>&#39;#&#39;</code></li>
    
    <li>A stationary obstacle <code>&#39;*&#39;</code></li>
    
    <li>Empty <code>&#39;.&#39;</code></li>
    

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.

 

Example 1:

Input: box = [["#",".","#"]]

Output: [["."],

         ["#"],

         ["#"]]

Example 2:

Input: box = [["#",".","*","."],

              ["#","#","*","."]]

Output: [["#","."],

         ["#","#"],

         ["*","*"],

         [".","."]]

Example 3:

Input: box = [["#","#","*",".","*","."],

              ["#","#","#","*",".","."],

              ["#","#","#",".","#","."]]

Output: [[".","#","#"],

         [".","#","#"],

         ["#","#","*"],

         ["#","*","."],

         ["#",".","*"],

         ["#",".","."]]

 

Constraints:

    <li><code>m == box.length</code></li>
    
    <li><code>n == box[i].length</code></li>
    
    <li><code>1 &lt;= m, n &lt;= 500</code></li>
    
    <li><code>box[i][j]</code> is either <code>&#39;#&#39;</code>, <code>&#39;*&#39;</code>, or <code>&#39;.&#39;</code>.</li>
    

Solutions

Python3

class Solution:
    def rotateTheBox(self, box: List[List[str]]) -> List[List[str]]:
        m, n = len(box), len(box[0])
        res = [[None] * m for _ in range(n)]
        for i in range(m):
            for j in range(n):
                res[j][m - i - 1] = box[i][j]
        for j in range(m):
            q = deque()
            for i in range(n - 1, -1, -1):
                if res[i][j] == '*':
                    q.clear()
                    continue
                if res[i][j] == '.':
                    q.append(i)
                else:
                    if not q:
                        continue
                    res[q.popleft()][j] = '#'
                    res[i][j] = '.'
                    q.append(i)
        return res

Java

class Solution {
    public char[][] rotateTheBox(char[][] box) {
        int m = box.length, n = box[0].length;
        char[][] res = new char[n][m];
        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < n; ++j) {
                res[j][m - i - 1] = box[i][j];
            }
        }
        for (int j = 0; j < m; ++j) {
            Deque<Integer> q = new ArrayDeque<>();
            for (int i = n - 1; i >= 0; --i) {
                if (res[i][j] == '*') {
                    q.clear();
                    continue;
                }
                if (res[i][j] == '.') {
                    q.offer(i);
                } else {
                    if (q.isEmpty()) {
                        continue;
                    }
                    res[q.poll()][j] = '#';
                    res[i][j] = '.';
                    q.offer(i);
                }
            }
        }
        return res;
    }
}

...