LeetCode Hot 100:矩阵

力扣hot100第21题:搜索二维矩阵IIpython实现 编写一个高效的算法来搜索m x n矩阵matrix中的一个目标值target。truefalse。 阅读详情

LeetCode Hot 100:矩阵

73. 矩阵置零

思路 1:辅助数组

class Solution {
public:
    void setZeroes(vector<vector<int>>& matrix) {
        int m = matrix.size(), n = m ? matrix[0].size() : 0;
        vector<int> row(m, false), col(n, false);
        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++)
                if (matrix[i][j] == 0) {
                    row[i] = true;
                    col[j] = true;
                }

        for (int i = 0; i < m; i++)
            if (row[i] == true)
                for (int j = 0; j < n; j++)
                    matrix[i][j] = 0;
        for (int j = 0; j < n; j++)
            if (col[j] == true)
                for (int i = 0; i < m; i++)
                    matrix[i][j] = 0;
    }
};

思路 2:常量额外空间

class Solution {
public:
    void setZeroes(vector<vector<int>>& matrix) {
        int m = matrix.size(), n = m ? matrix[0].size() : 0;
        int flag_col0 = false, flag_row0 = false;
        for (int i = 0; i < m; i++)
            if (matrix[i][0] == 0)
                flag_col0 = true;

        for (int j = 0; j < n; j++)
            if (matrix[0][j] == 0)
                flag_row0 = true;

        for (int i = 1; i < m; i++)
            for (int j = 1; j < n; j++)
                if (matrix[i][j] == 0) {
                    matrix[i][0] = 0;
                    matrix[0][j] = 0;
                }

        for (int i = 1; i < m; i++)
            for (int j = 1; j < n; j++)
                if (matrix[i][0] == 0 || matrix[0][j] == 0)
                    matrix[i][j] = 0;

        if (flag_col0)
            for (int i = 0; i < m; i++)
                matrix[i][0] = 0;

        if (flag_row0)
            for (int j = 0; j < n; j++)
                matrix[0][j] = 0;
    }
};

54. 螺旋矩阵

思路 1:模拟

class Solution {
private:
    // 向右、向下、向左、向上
    vector<vector<int>> directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};

public:
    vector<int> spiralOrder(vector<vector<int>>& matrix) {
        // 特判
        if (matrix.empty())
            return {};

        int m = matrix.size(), n = matrix[0].size();
        vector<int> ans(m * n, 0);
        vector<vector<bool>> visited(m, vector<bool>(n, false));
        int r = 0, c = 0;
        int dirIdx = 0;
        for (int i = 0; i < m * n; i++) {
            ans[i] = matrix[r][c];
            visited[r][c] = true;
            int nextRow = r + directions[dirIdx][0],
                nextColumn = c + directions[dirIdx][1];
            // 遇到边界,转向
            if (nextRow < 0 || nextRow >= m || nextColumn < 0 ||
                nextColumn >= n || visited[nextRow][nextColumn])
                dirIdx = (dirIdx + 1) % 4;
            r += directions[dirIdx][0];
            c += directions[dirIdx][1];
        }

        return ans;
    }
};

思路 2:模拟

class Solution {
public:
    vector<int> spiralOrder(vector<vector<int>>& matrix) {
        // 特判
        if (matrix.empty())
            return {};

        int m = matrix.size(), n = m ? matrix[0].size() : 0;
        vector<int> ans(m * n, 0);
        int index = 0;
        // 边界标记
        int left = 0, right = n - 1;
        int top = 0, bottom = m - 1;
        
        while (index < m * n) {
            // 向右
            for (int i = left; i <= right && index < m * n; i++)
                ans[index++] = matrix[top][i];
            top += 1;
            // 向下
            for (int i = top; i <= bottom && index < m * n; i++)
                ans[index++] = matrix[i][right];
            right -= 1;
            // 向左
            for (int i = right; i >= left && index < m * n; i--)
                ans[index++] = matrix[bottom][i];
            bottom -= 1;
            // 向上
            for (int i = bottom; i >= top && index < m * n; i--)
                ans[index++] = matrix[i][left];
            left++;
        }

        return ans;
    }
};

48. 旋转图像

思路 1:原地模拟

class Solution {
public:
    void rotate(vector<vector<int>>& matrix) {
        int n = matrix.size();
        // 特判
        if (n == 1)
            return;

        for (int i = 0; i < n / 2; i++) {
            for (int j = 0; j < (n + 1) / 2; j++) {
                int temp = matrix[i][j];
                matrix[i][j] = matrix[n - j - 1][i];
                matrix[n - j - 1][i] = matrix[n - i - 1][n - j - 1];
                matrix[n - i - 1][n - j - 1] = matrix[j][n - i - 1];
                matrix[j][n - i - 1] = temp;
            }
        }
    }
};

240. 搜索二维矩阵 II

思路 1:从左下角开始

class Solution {
public:
    bool searchMatrix(vector<vector<int>>& matrix, int target) {
        if (matrix.empty())
            return false;

        int m = matrix.size(), n = m ? matrix[0].size() : 0;
        int i = m - 1, j = 0;

        while (i >= 0 && j < n) {
            if (matrix[i][j] == target)
                return true;
            else if (matrix[i][j] > target)
                i--;
            else
                j++;
        }
        
        return false;
    }
};

思路 2:从右下角开始

class Solution {
public:
    bool searchMatrix(vector<vector<int>>& matrix, int target) {
        if (matrix.empty())
            return false;

        int m = matrix.size(), n = m ? matrix[0].size() : 0;
        int i = 0, j = n - 1;

        while (i < m && j >= 0) {
            if (matrix[i][j] == target)
                return true;
            else if (matrix[i][j] > target)
                j--;
            else
                i++;
        }

        return false;
    }
};
力扣hot100第19题:螺旋矩阵python 给你一个m行n列的矩阵matrix,请按照,返回矩阵中的所有元素。 阅读详情

相关推荐

力扣hot100第18题:矩阵置零python

给定一个m x n的矩阵,如果一个元素为,则将其所在行和列的所有元素都设为。请使用算法。

weixin_74009895的博客 67

力扣hot100:矩阵:54.螺旋矩阵

1

2401_83606094的博客 582

java 对接工行聚富通H5聚合支付接口

java 对接工行聚富通H5聚合支付接口

lgl的博客 875

前端力扣刷题 | 6:hot100之 矩阵

给定一个 m x n 的矩阵,如果一个元素为 0 ,则将其所在行和列的所有元素都设为 0。请使用 原地 算法。编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target。给你一个 m 行 n 列的矩阵 matrix ,请按照 顺时针螺旋顺序 ,返回矩阵中的所有元素。给定一个 n × n 的二维矩阵 matrix 表示一个图像。请你将图像顺时针旋转 90 度。旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要 使用另一个矩阵来旋转图像。

2502_90366796的博客 2079

基于PSIM仿真的单相全桥逆变电路

根据单极性SPWM调制电路、双极性SPWM调制电路和逆变器拓扑可以得到完整的逆变器仿真电路模型。

weixin_59329579的博客 1万+

低速信号设计之 LPC 篇

摘要:LPC总线作为服务器低速信号设计的关键技术,在BIOS芯片连接、Super I/O芯片控制和嵌入式管理等方面发挥着重要作用。其采用33MHz时钟频率和4位并行传输机制,理论速率达16.5MB/s。设计时需重点关注布线长度(建议<10cm)、信号完整性(45°走线、合理层间隔离)和过孔优化等要点。典型应用案例表明,通过规范设计可确保在工业控制等复杂环境中稳定工作。LPC总线凭借低成本、低引脚数和良好兼容性,仍是服务器低速外设连接的重要解决方案。

qq_16237389的博客 1174

力扣hot100:搜索二维矩阵

二分查找返回的是目标值最先出现的位置或者是在有序数组中的插入位置,如果是在有序数组中的插入位置则可能为在数组最后一个位置加一个数,这是如果进行matrix[i][weizi]==target的判断的话会导致数组越界,必须先处理越界问题,最终判断条件应为weizi<matrix[i].length&&matrix[i][weizi]==target。本题的本质是一个查找算法,为了提高性能可以使用二分查找,这个二维矩阵可以看出许多个数组,只需要对每个数组都进行一次二分查找就可以实现查找整个二维矩阵。

一本大学生java学习技术分享与交流 409

PADS VX2.4差分对设置避坑指南:常见错误与解决方案

本文深度解析了在PADS VX2.4中进行高速PCB设计时,差分对设置的核心要点与常见陷阱。针对Layout规则设置、Router布线实战中的阻抗控制、线长匹配等关键环节,提供了具体的避坑指南和解决方案,帮助工程师确保信号完整性,提升设计质量。

weixin_29216049的博客 368

leetcode hot100 矩阵:螺旋矩阵

/ 边界判断:如果当前行索引已超出上边界(上边界随圈数收缩:0 + circle),则退出整个循环。// 调整位置:到达上边界后,行索引下移一位(避免越界),列索引右移一位(准备下一圈的右遍历)// 调整位置:到达右边界后,列索引左移一位(避免越界),行索引下移一位(准备向下遍历)// 调整位置:到达下边界后,行索引上移一位(避免越界),列索引左移一位(准备向左遍历)// 调整位置:到达左边界后,列索引右移一位(避免越界),行索引上移一位(准备向上遍历)// 从上到下遍历,行索引递增,直到触及下边界。

2401_82379797的博客 545

嵌入式系统中的轻量级压缩实践——miniLZO在STM32上的性能优化

本文深入探讨了miniLZO轻量级压缩算法在STM32嵌入式系统中的性能优化实践。通过对比无损压缩算法的性能特点,详细解析了miniLZO在内存占用、压缩速度方面的优势,并提供了STM32移植、参数调优及创新应用案例,帮助开发者在资源受限环境中实现高效数据压缩。

weixin_30776273的博客 89

基于物联网设计的蜂箱智能监测系统设计

为解决传统养蜂业依赖人工、管理效率低下、干扰蜂群等问题,本项目设计并实现了一套基于物联网(IoT)的蜂箱智能监测系统。

696

LeetCode HOT100刷题笔记(三):子串,矩阵

ps:笔记和代码按本人理解整理,重思路

学习记录 1267

Claude Code 100个真实案例 - 用AI设计Arduino PCB电路板(硬件小白也能画板)

本案例展示如何使用 Claude Code 配合 KiCad 的 Python 脚本接口,从零设计一块 Arduino 传感器扩展板 PCB。从原理图符号创建、网表生成到 PCB 布局布线,全程自动化完成,最终输出可直接发给工厂打样的 Gerber 文件。

yueyeguzhuo的专栏 461

力扣HOT100之矩阵:54. 螺旋矩阵

他的思路比较朴素,就是按照。的顺序遍历矩阵,然后将遍历到的元素添加到数组中,但是这个思路中是遍历一条边后就立刻更新对应的边界,例如,刚刚把上边界的所有元素添加到数组后,就立刻将上边界+1,然后遍历右边的边时,右边是直接从更新后的上边界为起点开始遍历的(这就意味着在同一圈中,上边界添加的元素一定比下边界添加的多,右边界添加的元素一定比左边界添加的多),另外,只要当前的下标值。),如果出现了这样的情况,就说明已经打印完了所有元素,无需再继续遍历,直接退出当前的循环。在退出循环之后,直接返回数组即可。

weixin_52151595的博客 567

【软件系统架构】系列五:TCP/IP 协议栈

TCP/IP协议栈是现代网络通信的核心框架,采用分层架构设计,包含应用层、传输层、网络层和网络接口层四层结构。应用层提供HTTP、FTP等用户服务接口;传输层通过TCP/UDP实现端到端通信;网络层负责IP寻址和路由选择;网络接口层处理物理数据传输。数据在传输过程中经历层层封装,从应用层报文最终转换为物理层的比特流。该协议栈通过三次握手建立连接、数据传输、四次挥手断开连接的流程,确保可靠通信。作为互联网的基础,TCP/IP协议栈具有跨平台、标准化和可扩展的特点,广泛应用于各类网络设备和嵌入式系统中。

moton2017的博客 2185

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

力扣HOT100之二分查找:74. 搜索二维矩阵

将每一行的第一个元素当作一个数组中的元素,然后对这个数组进行二分查找,如果直接找到了。才进行进一步的搜索,当遍历完整层都没有找到时,返回。所以我们需要判断一下退出二分查找后的。所在的位置,此时我们应当去上一层(,此时我们应当去上一层(,此时我们无法去上一层(,否则退出循环的时候。为61,则退出循环时。

weixin_52151595的博客 876

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

力扣HOT100之矩阵:73. 矩阵置零

置为0,经过一次遍历后,除了第0行和第0列的情况没有统计出来,其余的行和列都已经遍历结束并且打上标签,由于打标签会向第0行和第0列添加0,打完标签后无法判断其本身原本就包含0,因此在遍历矩阵打标签之前,我们需要遍历矩阵的第0行和第0列,如果他们本身就有0,我们就分别用两个变量记录下来,至此,我们先分别遍历了矩阵的第0行和第0列,再遍历了矩阵的其余行和列,这就遍历了一次矩阵,我们再遍历一次矩阵,从下标为1的行和列开始,对于matrix[i][j],如果。我们可以将第0行和第0列作为记录的标志位,例如。

weixin_52151595的博客 376

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

力扣hot100:矩阵:48.旋转图像

4

2401_83606094的博客 313

力扣hot100:搜索二维矩阵与在排序数组中查找元素的第一个和最后一个位置(74,34)

二分查找算法在有序数据中的高效应用 本文展示了两种使用二分查找算法解决有序数据结构问题的经典方法。第一种是二维矩阵搜索,通过两次二分(先定位行后定位列)将时间复杂度优化到O(logm + logn);第二种是查找目标值的范围,通过两次变种二分(分别找左右边界)高效确定首次和末次出现位置。两种方法都充分利用了数据的有序特性,通过调整搜索边界的方向,在O(logn)时间内完成搜索任务。文中详细分析了算法思路、边界处理技巧和实现细节,并提供了完整的代码示例,是掌握二分查找变种应用的优秀范例。

ze15829163918的博客 1264

leetcode-hot100-矩阵:73矩阵置零-54螺旋矩阵-48旋转图像-240搜索二维矩阵

矩阵:73矩阵置零-54螺旋矩阵-48旋转图像-240搜索二维矩阵

XSYaaaaan的博客 267

力扣hot100:739. 每日温度/54. 螺旋矩阵

如果我们记住四个顶点,则我们就有足够信息可以使得我们无差错的转一圈。并且转完这一圈,四个顶点更新为内层的四个顶点是非常简单的。,我们可以直接按螺旋顺序遍历螺旋矩阵,然后在原矩阵中直接标记被遍历的位置。矩阵,从外到内分层,则有每次都是转一圈。如果需要原地进行,空间复杂度且需要是。经典单调栈问题,求下一个更大的数。,则需要进行层序遍历。

m0_63997099的博客 1763

leetcode hot100 矩阵:旋转图像

/ j只到i为止,因为当j >= i时,[i][j]和[j][i]的交换会重复之前的操作。// 内层循环:遍历当前行中"行索引大于列索引"的元素(避免重复交换)// 交换[i][j]和[j][i]的元素,实现行与列的转换。// 第二步:反转每一行的元素(将每行从左到右的顺序反转)// 定义左右指针,l从行首开始,r从行尾开始。// 左指针右移,右指针左移,缩小范围。// 交换当前行中l和r位置的元素。// 当左指针在右指针左边时,持续交换。// 外层循环:遍历每一行,i表示当前行索引。

2401_82379797的博客 448

力扣HOT100之矩阵:48. 旋转图像

这道题本来想用剥洋葱的办法的,一直写不对,放弃了。直接去看题解,用剥洋葱其实也可以做,就是要从外层处理到内层,每一个边界上的元素为matrix[0].size() - 1个,这样一来,四条边界上的元素个数都相同,然后直接狂用swap函数交换就完事了,但是我看评论区还有更加简洁的办法,就是通过找规律可以看出来,,我想了一下,剥洋葱还是有点太麻烦了,然后就直接用这个简单的思路,代码也很好写。

weixin_52151595的博客 389

LeetCode Hot 100系列:螺旋矩阵(Q54)

54.螺旋矩阵。

m0_73965192的博客 1332

力扣HOT100之矩阵:240. 搜索二维矩阵 II

的题解,感觉两位大佬的思路都很相近,只不过一个从矩阵的右上角开始比较,一个从矩阵的左下角开始比较,这里我就主要讲一下Krahets的思路,还挺通俗易懂的。这道题直接暴力AC的,虽然也能过,但是耗时太长了,AC之后看了下灵神和Krahets。我们直接找到矩阵最左下角的元素,假设为。列可以直接舍弃,因为第。列可以直接舍弃,因为第。时终止搜寻,直接返回。

weixin_52151595的博客 344

LeetCode hot100:054 螺旋矩阵:螺旋矩阵遍历全攻略

摘要:本文介绍了两种解决螺旋矩阵遍历问题的方法。边界收缩法通过定义上下左右四个边界,按照顺时针方向依次遍历并收缩边界,时间复杂度O(mn),空间复杂度O(1)。方向模拟法则使用方向数组和访问标记来模拟螺旋过程,时间复杂度O(mn),但需要O(mn)额外空间存储访问状态。其中边界收缩法更为推荐,因其逻辑清晰且空间效率高。文章详细说明了算法的核心思路、实现步骤和边界处理技巧,适用于各种矩阵规模的螺旋遍历需求。

m0_62396717的博客 325
上一篇: LeetCode Hot 100:双指针
下一篇: LeetCode Hot 100:普通数组
UestcXiye
博客等级 码龄8年 9046粉丝 1306原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

UestcXiye

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值
⌂ 换网址