leetcode 54. Spiral Matrix | 54. 螺旋矩阵(Java)
题目
https://leetcode.com/problems/spiral-matrix/
题解
类似于状态机的思路,设定一个 padding,走一圈之后,padding+1 。用 长宽 - padding
确定边界,一旦到达边界,则改变方向。用 count 记录已经走过的格子数量,当走过格子数量=所有格子之后,算法结束。
class Solution {public List<Integer> spiralOrder(int[][] matrix) {List<Integer> list = new ArrayList<>();int count = 0;int y = matrix.length;int x = matrix[0].length;int i = 0;int j = 0;int padding = 0;int direct = 0;// 0=right 1=down 2=left 3=upwhile (count < y * x) {list.add(matrix[i][j]);count++;switch (direct) {case 0 -> {if (j < x - padding - 1) {j++;} else {direct = 1;i++;}}case 1 -> {if (i < y - padding - 1) {i++;} else {direct = 2;j--;}}case 2 -> {if (j > padding) {j--;} else {direct = 3;i--;}}case 3 -> {if (i > padding + 1) {i--;} else {direct = 0;padding++;j++;}}}}return list;}
}
leetcode 54. Spiral Matrix | 54. 螺旋矩阵(Java)相关推荐
- LeetCode 59 Spiral Matrix II(螺旋矩阵II)(Array)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/5214 ...
- LeetCode 54. Spiral Matrix
54. Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the m ...
- LeetCode - 54. Spiral Matrix
54. Spiral Matrix Problem's Link ------------------------------------------------------------------- ...
- 【LeetCode-面试算法经典-Java实现】【054-Spiral Matrix(螺旋矩阵)】
[054-Spiral Matrix(螺旋矩阵)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a matrix of m x n elements (m ...
- LeetCode 54. Spiral Matrix--Python解法--螺旋排序
题目地址:Spiral Matrix - LeetCode Given a matrix of m x n elements (m rows, n columns), return all eleme ...
- 《leetcode》spiral-matrix-ii(构造螺旋矩阵)
题目描述 Given an integer n, generate a square matrix filled with elements from 1 to n 2 in spiral order ...
- java编写螺旋矩阵讲解_Java如何实现螺旋矩阵 Java实现螺旋矩阵代码实例
本篇文章小编给大家分享一下Java实现螺旋矩阵代码实例,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看. 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时 ...
- LeetCode 59. Spiral Matrix II
59. Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 ...
- LeetCode(59):螺旋矩阵 II
Medium! 题目描述: 给定一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵. 示例: 输入: 3 输出: [[ 1, 2, 3 ],[ 8, 9, 4 ...
最新文章
- 一文读懂人工智能产业最新发展趋势
- Appium入门实例(Java)
- MVP架构设计 进阶三
- java rgb hsl_RGB、HSB、HSL 互相转换算法
- Android 中input event的分析
- 多选题spss相关分析_SPSS进行典型相关分析结果总结
- python读取文件夹下所有图片_python 读取单文件夹中的图片文件信息保存到csv文件中...
- php 公众号验证回调方法_如何进行公众号文章收集 两种收集方法详解
- Moodle: Remove customise this page button from profile?
- oracle安装后怎么用plsql连接,oracle11g安装和使用PLSQL连接
- lombok构造方法_最佳实践Lombok
- 适用于物联网数据共享的区块链节点存储优化方案
- css compressor java_使用YUI Compressor压缩CSS/JS
- ps,ae,ui,ai,pr,cad,3DMAX,c4d,cdr,摄影后期
- MDIO总线介绍 |CSDN创作打卡
- Google大牛分享的面试秘籍
- 基于RV1126 Video分析-----链接 isp 与mipi csi 的media模块
- 保洁阿姨看完都会了!熬夜肝完这份Framework笔记,吐血整理
- html框架集frame是啥意思,HTML框架frame与iframe详解
- android apk 分析,Android APK分析工具
热门文章
- HDU - 2389 Rain on your Parade(Hopcroft-Krap算法求二分图最大匹配)
- CodeForces - 620E New Year Tree(线段树+dfs序+状态压缩)
- POJ - 3660 Cow Contest(最短路变形+闭包传递)
- Minetorch教程
- 数据分析与挖掘理论-概述
- python基础语法-异常处理
- java爬虫怎么确定url连接_Java爬虫之抓取一个网站上的全部链接
- 1_4 BuilderMode 建造者模式
- LXC C API 使用
- Python 的AES加密与解密