【LeetCode-Java】54. Spiral Matrix+59. Spiral Matrix II

2023-11-09

54. Spiral Matrix:

1.原题

链接:https://leetcode.com/problems/spiral-matrix/

Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.

For example,
Given the following matrix:

[
 [ 1, 2, 3 ],
 [ 4, 5, 6 ],
 [ 7, 8, 9 ]
]

You should return [1,2,3,6,9,8,7,4,5].

2.题目大意

给出一个矩阵,从外圈到内圈顺时针依次打印。

3.解题思路

这道题没有涉及复杂的数据结构或高级算法,但包含多个循环,需判断多个边界条件。

思路是每一次打印矩阵中的一个圈,用四个变量来控制:行起点,行终点,列起点,列终点。需注意的是,第三步遍历列数时需判断行数是否满足,第四步遍历行数时需判断列数是否满足。

时间复杂度为O(mn),空间复杂度为O(mn)

4.代码实现

    public List<Integer> spiralOrder(int[][] matrix) {
  List<Integer> result=new ArrayList<Integer>();
        if(matrix.length<1)
        return result;
        int rowBegin=0,rowEnd=matrix.length-1;
        int colBegin=0,colEnd=matrix[0].length-1;
        while(rowBegin<=rowEnd && colBegin<=colEnd){
        for(int i=colBegin;i<=colEnd;i++)
        result.add(matrix[rowBegin][i]);
        rowBegin++;
        for(int i=rowBegin;i<=rowEnd;i++)
        result.add(matrix[i][colEnd]);
        colEnd--;
        if(rowBegin<=rowEnd){
        for(int i=colEnd;i>=colBegin;i--)
            result.add(matrix[rowEnd][i]);
            rowEnd--;
        }
        if(colBegin<=colEnd){
        for(int i=rowEnd;i>=rowBegin;i--)
            result.add(matrix[i][colBegin]);
            colBegin++;
        }
        }
        return result;
    }


59. Spiral Matrix II

1.原题

链接:https://leetcode.com/problems/spiral-matrix-ii/

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.

For example,
Given n = 3,

You should return the following matrix:
[
 [ 1, 2, 3 ],
 [ 8, 9, 4 ],
 [ 7, 6, 5 ]
]
2.题目大意

给出整数n,生成一个n*n的矩阵。

3.解题思路

从外圈到内圈顺时针依次打印1,2,3...n^2,每次生成一个外圈。与上面这题不同的是,这个是一个方形矩阵,有些特殊情况不用判断。

时间复杂度为O(n^2),空间复杂度为O(n^2)

4.代码实现

public int[][] generateMatrix(int n) {
    int[][] matrix=new int[n][n];
        int rowBegin=0,rowEnd=n-1;
        int colBegin=0,colEnd=n-1;
        int num=1;
        while(rowBegin<=rowEnd && colBegin<=colEnd){
        for(int i=colBegin;i<=colEnd;i++){
        matrix[rowBegin][i]=num;
        num++;
        }
        rowBegin++;
        for(int i=rowBegin;i<=rowEnd;i++){
        matrix[i][colEnd]=num;
        num++;
        }
        colEnd--;
        for(int i=colEnd;i>=colBegin;i--){
        matrix[rowEnd][i]=num;
        num++;
        }
        rowEnd--;
        for(int i=rowEnd;i>=rowBegin;i--){
        matrix[i][colBegin]=num;
        num++;
        }
        colBegin++;
        }
    return matrix;
    }

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

【LeetCode-Java】54. Spiral Matrix+59. Spiral Matrix II 的相关文章

  • JPA:运行时如何指定类对应的表名?

    注意 我对 Java 非常熟悉 但对 Hibernate 或 JPA 还不太熟悉 还没有 我想编写一个通过 JPA 与 DB2 400 数据库通信的应用程序 现在我可以获取表中的所有条目并将它们列出到 System out 使用 MyEcl
  • Java 中的 sscanf 等效项[重复]

    这个问题在这里已经有答案了 可能的重复 用于使用已知模式解析字符串中的值的 sscanf 的 Java 等效项是什么 https stackoverflow com questions 8430022 what is the java eq
  • 用于将字符串与预定义字符混合/混淆的简单算法

    我有一个字符串如下 它的长度是10 它代表基数 36 因此包含数字和大写字母 字符串的来源是数据库生成的序列 即从 1 及以上 正在转换为基数 36 我的问题是转换为base 36转换的结果也是连续 顺序的 例如 ID 1402 gt 00
  • 访问 java jigsaw 模块中的资源文件[重复]

    这个问题在这里已经有答案了 我正在尝试从项目中的类访问 Eclipse 项目中的文件 我需要将该项目声明为 jigsaw 模块才能从其他项目访问它 但是通过这样做 我无法再访问项目中的 example png 等文件 这是我的项目结构 pr
  • 使用 Morphia 配置 Spring Boot?

    我不想利用 Spring DATA MongoDB 支持 我想利用名为 Morphia 的 MongoDB ORM https github com mongodb morphia https github com mongodb morp
  • Android 中的 ImageView 拖动限制

    我在布局中有一个 ImageView 并在 ImageView 上设置 OnTouchListener 来拖动 ImageView 它工作得很好 我的问题是如何防止将 ImageView 移动到布局范围之外 这是我的代码 活动类别 publ
  • Android - Firebase - 保存新数据而不覆盖旧数据

    创建此问题是因为我之前的问题包含 2 个问题 而不是将其缩小到 1 Aim 用户将能够存储新数据而不会覆盖之前提交的数据 描述 目前 当用户输入新报告时 事件报告节点中的用户事件报告数据将被覆盖 用户发送的旧事件报告中的数据应与新数据一起保
  • 在 ElasticSearch API 应用程序中找不到 NodeBuilder

    我正在尝试实现 Elasticsearch API 我的系统接受 nodeBuilder 时出现错误 这是代码 import org elasticsearch action index IndexResponse import org e
  • Spring Boot 是否支持服务器名称指示(SNI)?

    Spring Boot 是否支持服务器名称指示 SNI 具体来说 运行嵌入式 Tomcat 服务器并打包为可执行 jar 文件的 Spring Boot 2 2 2 RELEASE 应用程序是否可以根据传入请求的主机名支持多个 SSL 证书
  • 无法在 Spring boot 中使用 findOne() 方法

    我的项目是关于用户管理器网络的 我是 Spring 和 Java 的新手 这是我的代码 在 UserController 中 RequestMapping value users name method RequestMethod GET
  • 非法监控状态异常

    如何将轮询线程传递给另一个线程进行处理 程序执行在控制器类中 该类具有 main 方法和线程池 主类控制器 public static void main String args throws InterruptedException Ru
  • 关于 mongodb java 驱动程序的困惑[重复]

    这个问题在这里已经有答案了 我是 MongoDB 的初学者 我正在使用 JAVA 驱动程序来使用它 我有以下代码 MongoClient client new MongoClient DB d client getDB world DBCo
  • 将 JAR 文件打包为 WAR 文件

    我有一系列依赖的Java项目 我想将它们打包成一个 JAR 文件 以便在我的 WAR 文件中使用 这些项目依赖于大量的外部库和项目 如log4j apache commons等 我选择 Eclipse 中的所有项目并导出为 JAR 文件 然
  • 在 init 之外在 java 中创建对象

    因此 对于我正在创建的游戏 我有一些扩展 GameDriver 的类 到目前为止 在所有其他类上我都能够扩展 GameDriver 然后在 GameDriver 中我可以执行以下操作 ArrayList
  • 如何将 JAVAX-WS 端点绑定更改为 SOAP 1.2?

    我正在使用发布测试 WS 实现Endpoint publish 用于在 Visual Studio 中使用 根据文档 http metro java net nonav 1 2 docs endpoint html默认的 SOAP 绑定是1
  • 使用服务器 java api 从 jasperserver 存储库检索资源

    我正在尝试使用其 java API 从 Jasperserver 存储库检索资源 根据jasper 报表服务器终极指南 https community jaspersoft com documentation jasperreports s
  • Java 统一编码

    A Java char is 2 bytes http java sun com docs books tutorial java nutsandbolts datatypes html 最大大小为 65 536 但有95 221 http
  • 如何提高QNX6下Eclipse IDE的性能

    我们在 VMWare 环境中通过 QNX6 运行 Eclipse 速度非常慢 Eclipse 是这样启动的 usr qnx630 host qnx6 x86 usr qde eclipse eclipse data root workspa
  • 如何使用 itext 在 pdf 页脚上添加页码,它应该照顾其宽度?

    我的代码示例如下 Override public void onEndPage PdfWriter writer Document document addFooter writer private void addFooter PdfWr
  • 在 Eclipse Testrunner 中使用名称的 ParameterizedTest

    当您使用 Eclipse TestRunner 运行 JUnit 4 ParameterizedTest 时 图形表示相当愚蠢 对于每个测试 您都有一个名为 0 1 ETC 是否可以进行测试 0 1 等显式名称 实施一个toString测试

随机推荐