IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 数据结构与算法 -> LeetCode.1861 Rotating the Box(旋转箱子) -> 正文阅读

[数据结构与算法]LeetCode.1861 Rotating the Box(旋转箱子)

1.题目

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.

2.样例

  • 样例1
Input: box = [["#",".","#"]]
Output: [["."],
         ["#"],
         ["#"]]
  • 样例2
Input: box = [["#",".","*","."],
              ["#","#","*","."]]
Output: [["#","."],
         ["#","#"],
         ["*","*"],
         [".","."]]
  • 样例3
Input: box = [["#","#","*",".","*","."],
              ["#","#","#","*",".","."],
              ["#","#","#",".","#","."]]
Output: [[".","#","#"],
         [".","#","#"],
         ["#","#","*"],
         ["#","*","."],
         ["#",".","*"],
         ["#",".","."]]

3.题解

  • 描述:题目的意思是对箱子(二维数组)进行顺时针旋转90度,其中单元格中的石头因为重力的原因下落,在碰到障碍时就停下了。否则就一直下落指导箱底。
  • 题解:
class Solution {
    public static char[][] rotateTheBox(char[][] box) {
        // 思路:从右往的遍历,统计碰到的empty的个数,直到遇到stone后重新清零,最后将整个数据旋转90即可
        if (box == null || box[0] == null || box[0].length == 0) {
            return new char[0][];
        }

        char empty = '.';
        char obstacle = '*';
        char stone = '#';

        int m = box.length;
        int n = box[0].length;
        for (int i = 0; i < m; i++) {
            int emptyCount = 0;
            for (int j = n - 1; j >= 0; j--) {
                if (box[i][j] == empty) {
                    emptyCount++;
                } else if (box[i][j] == stone) {
                    box[i][j] = empty;
                    box[i][j + emptyCount] = stone;
                } else if (box[i][j] == obstacle) {
                    emptyCount = 0;
                }
            }
        }

        // 旋转数组,从下往上每行竖直(从左往右竖直)
        char[][] res = new char[n][m];
        for (int i = m - 1; i >= 0; i--) {
            for (int j = 0; j < n; j++) {
                res[j][m - 1 - i] = box[i][j];
            }
        }
        return res;
    }
}
  数据结构与算法 最新文章
【力扣106】 从中序与后续遍历序列构造二叉
leetcode 322 零钱兑换
哈希的应用:海量数据处理
动态规划|最短Hamilton路径
华为机试_HJ41 称砝码【中等】【menset】【
【C与数据结构】——寒假提高每日练习Day1
基础算法——堆排序
2023王道数据结构线性表--单链表课后习题部
LeetCode 之 反转链表的一部分
【题解】lintcode必刷50题<有效的括号序列
上一篇文章      下一篇文章      查看所有文章
加:2021-08-02 11:02:29  更:2021-08-02 11:03:03 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/11 1:22:42-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码