LeetCode——Spiral Matrix

保姆级教程 | MS构建三层石墨后转成LAMMPS的data文件 喜欢的话记得一键三连~ 阅读详情

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

  • Example 1:
    Input:
    [
    [ 1, 2, 3 ],
    [ 4, 5, 6 ],
    [ 7, 8, 9 ]
    ]
    Output: [1,2,3,6,9,8,7,4,5]
  • Example 2:
    Input:
    [
    [1, 2, 3, 4],
    [5, 6, 7, 8],
    [9,10,11,12]
    ]
    Output: [1,2,3,4,8,12,11,10,9,5,6,7]

解法

一个复杂又白痴的做法,按要求螺旋遍历数组一个一个把数据加进res

public List<Integer> spiralOrder(int[][] matrix) {
		List<Integer> res=new ArrayList<Integer>();
		if(matrix==null||matrix.length==0)
            return res;
		int n=matrix.length;
		int m=matrix[0].length;
		boolean[][] visited=new boolean[n][m];
		for(int i=0;i<n;i++)
		{	if(i>m-1||visited[i][i])
				break;
			right(matrix,visited,res,i,i,m-1-i);
			if(i>m-1||i>=n-1||visited[i+1][m-1-i])
				break;
			down(matrix,visited,res,m-1-i,i+1,n-1-i);
			if(i>m-2||i>n-1||visited[n-1-i][m-2-i]||i>m-2)
				break;
			left(matrix,visited,res,n-1-i,m-2-i,i);
			if(i>n-2||i>=m||visited[n-2-i][i])
				break;
			up(matrix,visited,res,i,n-2-i,1+i);
		}
		return res;
	}
	private void up(int[][] matrix, boolean[][] visited, List<Integer> res, int col, int start, int end) {
		// TODO Auto-generated method stub
		for(int i=start;i>=end;i--)
		{
			if(visited[i][col])
				break;
			visited[i][col]=true;
			res.add(matrix[i][col]);
		}
	}

	private void left(int[][] matrix, boolean[][] visited, List<Integer> res, int row, int start, int end) {
		// TODO Auto-generated method stub
		for(int i=start;i>=end;i--)
		{
			if(visited[row][i])
				break;
			visited[row][i]=true;
			res.add(matrix[row][i]);
		}
	}

	private void down(int[][] matrix, boolean[][] visited, List<Integer> res, int col, int start, int end) {
		// TODO Auto-generated method stub
		for(int i=start;i<=end;i++)
		{
			if(visited[i][col])
				break;
			visited[i][col]=true;
			res.add(matrix[i][col]);
		}
	}
    private void right(int[][] matrix, boolean[][] visited, List<Integer> res, int row,int start,int end) {
		// TODO Auto-generated method stub
		for(int i=start;i<=end;i++)
		{
			if(visited[row][i])
				break;
			visited[row][i]=true;
			res.add(matrix[row][i]);
		}
    }

Runtime: 1 ms, faster than 100.00% of Java online submissions for Spiral Matrix.
Memory Usage: 37 MB, less than 64.52% of Java online submissions for Spiral Matrix.
简洁一点的写法

public List<Integer> spiralOrder(int[][] matrix) {
    	List<Integer> res=new ArrayList<Integer>();
		if(matrix==null||matrix.length==0)
            return res;
		int n=matrix.length;
		int m=matrix[0].length;
		int up=0,down=n-1,left=0,right=m-1;
		while(true){
			for(int j=left;j<=right;j++)
				res.add(matrix[up][j]);
			if(++up>down)
				break;
			for(int j=up;j<=down;j++)
				res.add(matrix[j][right]);
			if(--right<left)
				break;
			for(int j=right;j>=left;j--)
				res.add(matrix[down][j]);
			if(--down<up)
				break;
			for(int j=down;j>=up;j--)
				res.add(matrix[j][left]);
			if(++left>right)
				break;
		}
		return res;
    }
STM32L452与FPGA通信翻车?手把手教你关闭I2C时钟延展避坑(附OVR错误解决方案) 本文深入解析STM32L452与FPGA在I2C通信中因时钟延展功能导致的常见问题,提供关闭I2C时钟延展的详细配置方法(NOSTRETCH模式),并针对可能出现的OVR错误给出Combined模式解决方案。通过实战案例和波形分析,帮助工程师快速排查和解决嵌入式系统中的I2C兼容性问题。 阅读详情

相关推荐

禅道18.3版本二开避坑指南:手把手教你扩展自定义报表功能

本文详细介绍了禅道18.3版本的二次开发实践,重点讲解如何扩展自定义报表功能。通过zentaoPHP框架的深度定制,从开发环境搭建到核心数据模型扩展,再到可视化配置和性能优化,手把手教你构建多维报表分析系统,满足企业级项目管理需求。

day7的博客 377

【LeetCode】解题54:Spiral Matrix

LeetCode解题 54:Spiral Matrix Problem 54: Spiral Matrix [Medium]解题思路Solution (Java)修改过程 Problem 54: Spiral Matrix [Medium] Given a matrix of m x n elements (m rows, n columns), return all elements of th...

Fayedily的博客 259

STM32F429移植LVGL9.3.0:从源码配置到双缓冲优化实战

本文详细介绍了如何在STM32F429平台上移植LVGL 9.3.0图形库,涵盖从源码准备、工程配置、显示驱动适配到双缓冲优化等关键步骤。重点解析了利用STM32F429的硬件特性实现高效内存管理和流畅显示性能的实战技巧,为嵌入式GUI开发提供完整解决方案。

q6r7s8t9的博客 1165

LeetCode力扣之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 r...

LeeWei 219

机器学习kaggle案例:风控评分卡模型(Give_Me_Some_Credit)

kaggle链接:https://www.kaggle.com/c/GiveMeSomeCredit ipynb文件:https://github.com/824024445/KaggleCases/blob/master/%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0kaggle%E6%A1%88%E4%BE%8B%EF%BC%9A%E9%A3%8E%E6%8E%A7%...

weixin_42662126的博客 1万+

告别环境报错:用Pycharm一站式搞定Superset 3.0.0二次开发环境(Windows/Mac通用)

本文详细介绍了如何使用Pycharm一站式搭建Apache Superset 3.0.0的二次开发环境,涵盖Windows和Mac平台。通过虚拟环境配置、依赖管理、数据库初始化及前后端协同开发等步骤,帮助开发者高效解决环境报错问题,提升开发效率。特别适合需要进行Superset二次开发的工程师参考。

weixin_42549496的博客 320

Leetcode 59. Spiral Matrix II

题目 解法 这道题目其实跟这道https://blog.csdn.net/qq_37821701/article/details/112093118一模一样。参考这道题目里面的第二种解法 class Solution: def generateMatrix(self, n: int) -> List[List[int]]: cnt = 1 dirs = [[0,1],[1,0],[0,-1],[-1,0]] row = 0 co

qq_37821701的博客 473

LPDDR5 物理层一致性测试(连载・下):电平、过冲下冲与 Rx 接收眼图 Mask 掩模完整指标解读

本文系统梳理LPDDR5物理层一致性测试全流程,涵盖差分/单端电平、边沿斜率、交叉点电压、过冲下冲及CS/CA/DQ接收眼图掩模(Rx Mask)测试,详解各参数定义与工程实操要点。强调配置需与DRAM MR寄存器同步,突出差分≠合格、面积积分不可忽视、掩模失败优先排查参考电压与信号完整性等高频坑点,为硬件调试与SI工程师提供权威参考。

一只豌豆象的博客 85

LeetCode力扣之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 ]...

LeeWei 179

GEE APP:基于GEE的红树林监测平台

本文将介绍如何利用Google Earth Engine平台,结合Landsat系列卫星影像和SRTM高程数据,实现红树林分布的动态监测与变化分析。

此星光明博客 163

Android 13 Settings蓝牙列表卡顿问题排查及优化过程

此时我们的卡顿优化基本完成了,我们应该要考虑内存是否会泄漏,如果大家不知道哪些需要回收可以用leakcanary和android profiler进行内存检测,看代码有没有内存泄漏。

龚礼鹏的博客 2557

Leetcode之 Spiral Matrix 问题

问题描述: Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. 示例: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] You should return [1,2,3,6,9,8,7,4,

漫漫学习路 360

34、Text2KGBench与GeoQuestions1089:知识图谱生成与地理空间问答的新进展

本文介绍了Text2KGBench和GeoQuestions1089在知识图谱生成与地理空间问答领域的最新进展。Text2KGBench提供了一个开放且可复现的基准,用于从文本生成知识图谱,并结合多个LLM模型和评估工具推动研究。GeoQuestions1089是目前最大的地理空间问答数据集,与GeoQA2系统结合,推动了地理空间问答系统的发展。文章还探讨了技术挑战和未来研究方向,包括多模态知识图谱生成和深度学习优化的地理空间问答系统。

css33的专栏 120

LeetCode 54. Spiral Matrix

LeetCode 54. Spiral Matrix 循环输出矩阵元素,中等难度题目,查数要认真。 Solution1:我的答案 class Solution { public: vector&amp;lt;int&amp;gt; spiralOrder(vector&amp;lt;vector&amp;lt;int&amp;gt;&amp;gt;&amp;amp; matrix) { if (!matrix.size()

Allenlzcoder的博客 512

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

LeetCode //C - 885. Spiral Matrix III

Article Summary The task requires starting at the grid's starting point (rStart, cStart), traversing all grid locations in a clockwise spiral (including moving beyond the grid's bounds), ultimately returning the coordinates of all visited grid locations. T

Made in Code 1196

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

LeetCode解题报告—— Group Anagrams & Pow(x, n) & Spiral Matrix

1.Group Anagrams Given an array of strings, group anagrams together. For example, given:["eat", "tea", "tan", "ate", "nat", "bat"], [ ["ate", "eat","tea"], ["nat","tan"], ["bat"] ]...

dbvz4901的博客 162

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

LeetCode 59. Spiral Matrix II

LeetCode 59. Spiral Matrix II Solution1:我的答案 思路和第54题差不多,就是有点慢。。。。 class Solution { public: vector&lt;vector&lt;int&gt;&gt; generateMatrix(int n) { if (n == 1) return {{1}}; vect...

Allenlzcoder的博客 916

【LeetCode】#54螺旋矩阵(Spiral Matrix)

【LeetCode】#54螺旋矩阵(Spiral Matrix) 题目描述 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素。 示例 示例 1: 输入: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] 输出: [1,2,3,6,9,8,7,4,5] 示例 2: 输入: [ [1, 2, 3, 4], [5, ...

weixin_43858604的博客 109

leetcode59 - Spiral Matrix II - medium

54. Spiral MatrixGiven a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.Example 1:Input:[ [1, 2, 3, 4], [5, 6, 7, 8], [9,10,11,12]]Output: [1,2,3,4,...

weixin_30711917的博客 132

NYUer | LeetCode59 Spiral Matrix II

NYUer | LeetCode59 Spiral Matrix II

Moses_SU的博客 192

leetcode学习笔记59 Spiral Matrix II

leetcode学习笔记59问题思考方法1方法2 问题 Spiral Matrix II Given a positive integer n, generate an n x n matrix filled with elements from 1 to n2 in spiral order. Example 1: Input: n = 3 Output: [[1,2,3],[8,9,4],[7,6,5]] Example 2: Input: n = 1 Output: [[1]] Constr

kuma的博客 168

LeetCode Algorithm 0059 - Spiral Matrix II (Medium)

0059 - Spiral Matrix II (Medium),Related Topics: Array

DarkRabbit的专栏 388

LeetCode Spiral Matrix

将一个矩阵中的内容螺旋输出。

DRFish 1240

LeetCode[54] Spiral Matrix

LeetCode[54] 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 matri...

weixin_34413357的博客 87

python写算法题:leetcode: 59. Spiral Matrix II

class Solution(object): def val(self, n, x, y): px=n-1-abs(2*x-n+1) py=n-1-abs(2*y-n+1) v=min(px,py) if v>=n-1: return n*n v/=2 pas...

260

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

寒泉 5万+
上一篇: Python教程精简版
下一篇: LeetCode——Jump Game
yjfanling
博客等级 码龄12年 41粉丝 91原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值