ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

LeetCode 1861. Rotating the Box

2022-07-19 13:01:22  阅读:147  来源: 互联网

标签:Box Rotating stone cur int res box 1861 length


原题链接在这里:https://leetcode.com/problems/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.

Example 1:

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

Example 2:

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

Example 3:

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

Constraints:

  • m == box.length
  • n == box[i].length
  • 1 <= m, n <= 500
  • box[i][j] is either '#''*', or '.'.

题解:

Rotate 90 degree clockwise, res[i][j] = box[m - j - 1][i].

Then for each column, iterate from the bottom.

When hitting obstacle, cur position is decreased by 1. cur is the current position we could place stone.

If we encounter a stone, mark it as empty, then mark current position as stone.

Time Complexity: O(m * n). m = box.length. n = box[0].length.

Space: O(1). regardless res.

AC Java:

 1 class Solution {
 2     public char[][] rotateTheBox(char[][] box) {
 3         if(box == null || box.length == 0 || box[0].length == 0){
 4             return box;
 5         }
 6         
 7         int m = box.length;
 8         int n = box[0].length;
 9         char [][] res = new char[n][m];
10         for(int i = 0; i < n; i++){
11             for(int j = 0; j < m; j++){
12                 res[i][j] = box[m - j - 1][i];
13             }
14         }
15         
16         for(int j = 0; j < m; j++){
17             int cur = n - 1;
18             for(int i = n - 1; i >= 0; i--){
19                 if(res[i][j] == '*'){
20                     cur = i - 1;
21                 }else if(res[i][j] == '#'){
22                     res[i][j] = '.';
23                     res[cur--][j] = '#';
24                 }
25             }
26         }
27         
28         return res;
29     }
30 }

类似Rotate Image.

标签:Box,Rotating,stone,cur,int,res,box,1861,length
来源: https://www.cnblogs.com/Dylan-Java-NYC/p/16493690.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有