LeetCode | 54. Spiral Matrix

逆向攻防世界CTF系列40-ReverseMe-120 注意sub_401000main中只传了两个参数,但是跟进后发现四个(可能是隐式传递,放在寄存器里传递了),调试发现a4是输入长度,与最终的you_know_how_to_remove_junk_code比较的是v12(但是并没有被传入),开始怀疑是不是隐式传递了。_mm_xor_si128用于计算128位(16字节)的按位异或,然后通过v14控制循环结束的条件,可以看到v14增长的步长为16,而且通过上面得到的flag值解码得到的字符串为32个字节大小,正好是16的整数倍。 阅读详情

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].

思路:蛇形矩阵。设立四个方向的边界,然后模拟一遍即可。

// 3 ms
class Solution {
public:
/*蛇形矩阵
 [
   [ 1, 2, 3 ],
   [ 4, 5, 6 ],
   [ 7, 8, 9 ]
 ]

 => [1,2,3,6,9,8,7,4,5]
*/

    vector<int> spiralOrder(vector<vector<int> >& matrix)
    {
        vector<int> res;
        int x = 0, y = 0, tot = 0;  //tot用于计数,作为停止条件
        int m = matrix.size();
        if(m == 0)      //错误输入数据,特殊处理
            return res;
        int n = matrix[0].size();
        int up = 0, down = m-1, left = 0, right = n-1;

        if(n==0)        //错误输入数据,特殊处理
            return res;

        //开始遍历
        while(tot < m*n)
        {
            while(x==up && tot < m*n)     //最上面一行
            {
                res.push_back(matrix[x][y]);
                tot++;
                if(y == right)
                    x++;
                else
                    y++;
            }
            up += 1;

            while(y==right  && tot < m*n)
            {
                res.push_back(matrix[x][y]);
                tot++;
                if(x == down)
                    y--;
                else
                    x++;
            }
            right -= 1;

            while(x==down && tot < m*n)
            {
                res.push_back(matrix[x][y]);
                tot++;
                if(y == left)
                    x--;
                else
                    y--;
            }
            down -= 1;

            while(y==left && tot < m*n)
            {
                res.push_back(matrix[x][y]);
                tot++;
                if(x == up)
                    y++;
                else
                    x--;
            }
            left += 1;
        }

        //cout<<up<<down<<left<<right;
        return res;
    }
};

在solution中看到更简洁的代码(思路是一样的),据称 0ms,交上去呵呵了,也是3 ms

class Solution {
public:
    vector<int> spiralOrder(vector<vector<int>>& matrix) {
        if (matrix.empty()) return {};
        int m = matrix.size(), n = matrix[0].size();
        vector<int> spiral(m * n);
        int u = 0, d = m - 1, l = 0, r = n - 1, k = 0;
        while (true) {
            // up
            for (int col = l; col <= r; col++) spiral[k++] = matrix[u][col];
            if (++u > d) break;
            // right
            for (int row = u; row <= d; row++) spiral[k++] = matrix[row][r];
            if (--r < l) break;
            // down
            for (int col = r; col >= l; col--) spiral[k++] = matrix[d][col];
            if (--d < u) break;
            // left
            for (int row = d; row >= u; row--) spiral[k++] = matrix[row][l];
            if (++l > r) break;
        }
        return spiral;
    }
};
【MicroPython编程-ESP32篇:设备驱动】-BNO08X系列传感器驱动 BNO08X 系列(BNO085/BNO086)是一款系统级封装 (SiP),集成了三轴加速度计、三轴陀螺仪、磁力计以及运行 CEVA SH-2 固件的 32 位 ARM® Cortex™-M0+ 微控制器。SH-2 包含 MotionEngine™ 软件,该软件提供复杂的信号处理算法来处理传感器数据,并提供精确的实时 3D 姿态、航向、校准加速度和校准角速度,以及更高级的上下文输出。BNO08X 集成在一个 28 引脚 LGA 封装中,尺寸为 3.8mm x 5.2mm x 1.1mm。它兼容 Andro 阅读详情

相关推荐

地理空间智能体(GI Agent)

地理空间智能体(Geospatial Intelligence Agent, GI Agent)是AI Agent技术与地理信息系统(GIS)深度融合的产物。其核心是以大型语言模型(LLM)为驱动,通过自然语言理解、任务分解和工具调用能力,实现地理空间任务的自动化执行。​关键特性​自主性​:独立规划任务流程(如空间数据检索、分析、可视化);​交互性​:支持自然语言对话交互,替代传统图形界面操作;​扩展性​:灵活集成第三方工具和API,支持跨平台协作。

百态·数智思维 | 聚焦大数据、量化与未来AI 2035

【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的博客 258

Box2D-Lite源码阅读笔记(2)

0.初始化函数 static void InitDemo(int index) { world.Clear(); numBodies = 0; numJoints = 0; bomb = NULL; demoIndex = index; demos[index](bodies, joints); } 在这个初始化函数中,一般会默认执行index=0的函数,这个函数就是一个示例,因为示例比较简单,所以也便于研究,示例效果如下。 就是一个方块。 进入第一个示例函数。(忽略我加的注释)

ARTELE的博客 977

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的博客 510

从零搭建基于OpenSpec的现代化UI自动化测试框架实战指南

UI自动化测试是软件质量保障的关键环节,其核心在于通过脚本模拟用户操作,对Web应用界面进行功能验证。其原理在于利用浏览器驱动工具(如Selenium、Playwright)提供的API,实现对页面元素的定位、交互与断言。一个设计良好的自动化框架能显著提升测试脚本的可维护性、可读性和执行稳定性,从而在持续集成、回归测试等场景中发挥巨大价值。本文聚焦于如何构建一个现代化的UI测试框架,通过整合OpenSpec架构规范、Pytest测试引擎与Playwright浏览器自动化工具,形成清晰的页面对象模型(POM)

weixin_28836989的博客 286

EM算法求解混合伯努利模型

本文记录了EM算法求解混合伯努利分布的推导,并提供了matlab实验代码。

霁月难逢 彩云易散 6976

LeetCode54——Spiral Matrix

LeetCode54——Spiral Matrix 还是用图来表述: 从左上开始,按照箭头的顺序,进行4个for循环,完成一圈,一共用count = min(row,col)/2  圈。 注意最后的红色箭头,如果行数和列数中较小的数是奇数,就会出现上述情况。 这个时候咱们在最后补上就好 代码: class Solution { public: vector spiralOrd

上善若水 2152

基于方差分解分析气象/水文干旱驱动因素(含MATLAB全代码)

简介基于方差分解方法分析气象/水文干旱驱动因素

WW、forever的博客 803

【LeetCode】54. Spiral Matrix

问题描述https://leetcode.com/problems/spiral-matrix/#/description 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

zgljl2012的专栏 1091

第 9 篇:TMC2240 速度调节实战|STEP 信号生成 + 平滑调速算法(无抖动)

摘要:本文详细介绍了TMC2240电机调速方案,重点解决调速时电机抖动、丢步等问题。核心原理是通过STEP信号频率控制转速,结合平滑加减速算法避免电流冲击。文章从速度与STEP频率的数学关系入手,提供了定时器生成固定频率STEP信号的实现代码,并给出不同转速下的频率参数表。针对42步进电机+256微步细分场景,详细讲解了定时器初始化、中断处理及固定转速设置方法,帮助开发者实现稳定调速。全文包含可直接复用的代码片段,适合嵌入式开发者快速实现电机平滑调速功能。

BackCatK Chen的博客 1036

NVIDIA Isaac GR00T N1 人形机器人强化学习入门教程(二)详细步骤

LeRobot 格式 1.1 加载数据集 要加载数据集,我们需要定义以下 3 项内容: 在数据中显示图像帧 我们还可以对 LeRobotSingleDataset 类中的数据进行一系列转换。下面展示了如何对数据进行转换。 现在看看应用转换后的数据有何不同。 例如,对状态和操作进行规范化和串联,对视频图像进行裁剪、调整大小和颜色抖动。 二、GR00T 推断 本教程介绍如何使用 GR00

2506_90492529的博客 3014

[LeetCode 54] Spiral Matrix

题目链接:spiral-matrix import java.util.ArrayList; import java.util.List; /** * Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For

瓦尔登湖 1035

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

leetcode矩阵类题目专杀之54. Spiral Matrix

http://www.fengchang.cc/post/3    

xunileida的专栏 267

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

Leetcode 54. Spiral Matrix

题目描述:按照螺旋顺序打印矩阵。 题目链接:Leetcode 54. Spiral Matrix 思考思路:就是矩阵的一个搜索遍历,用到思想就是要dx\dy 来控制方向,正如 (0,0,-1,1)这样的方向选择,就是选择不同方向,要思考的就是什么时候转变方向。 代码如下 class Solution(object): def spiralOrder(self, matrix): ...

Za七杂⑧ 271

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

leetcode 54. Spiral Matrix 、59. Spiral Matrix II

54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0)、(1,1)这种开始的。 用endx、endy来表示每次转圈的x、y方向的终止位置,方便后面进行边界条件设置。 注意:后面start < endx && start &...

weixin_30374009的博客 120

LeetCode54.Spiral Matrix

LeetCode54.Spiral Matrix

weixin_38665509的博客 362

leetcode-54. Spiral Matrix

problem 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]...

学习笔记 319

Leetcode54. Spiral Matrix解题方案

Leetcode54. Spiral Matrix 题目描述 Input: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] Output: [1,2,3,6,9,8,7,4,5] 思路 有四个方向right, left, top, bottom. direction=“right”, 当到达最右边界时,代表本行输出结束,top++, direction=“bottom”,当到达最下边界时, right–, direction = “left”,当到达最左边界是

初七的博客 162

【C++】【LeetCode】54. Spiral Matrix && 59. Spiral Matrix II

题目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

zjajgyy的博客 361

Leetcode 54, Spiral Matrix

Leetcode 54, Spiral Matrix, 难度medium就是按照规定顺序输出一个矩形,用四个变量保存了边界,然后对每一条边进行循环读入,处理一下相应的边界问题,复杂度为O(n),遍历了一遍数组。 以下是代码:class Solution { public: vector<int> spiralOrder(vector<vector<int>>& matrix) {

chiyiyan1586的博客 174

leetcode54 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 ] ]

明天会更好 372

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 matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]

无名山丘,崛起成峰 1018

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的博客 86

Leetcode oj java 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 matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7,

Xcy的博客 339

LeetCode 54 Spiral Matrix

1. 2. class Solution { public ArrayList<Integer> list=new ArrayList<Integer>(); public List<Integer> spiralOrder(int[][] matrix) { if(matrix.length<=0){ ...

susuxuezhang的博客 176

Leetcode 54. Spiral Matrix and 59. Spiral Matrix II

一: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. 方法一:python切片 if not matrix: return [] ret = [] while matrix: ...

rainMan的博客 216
上一篇: LeetCode | 53. Maximum Subarray
下一篇: LeetCode | 55. Jump Game
PKU_CXK
博客等级 码龄10年 87粉丝 142原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值