当前位置: 首页 > article >正文

【LeetCode面试150】——54螺旋矩阵

博客昵称:沈小农学编程

作者简介:一名在读硕士,定期更新相关算法面试题,欢迎关注小弟!

PS:哈喽!各位CSDN的uu们,我是你的小弟沈小农,希望我的文章能帮助到你。欢迎大家在评论区唠嗑指正,觉得好的话别忘了一键三连哦!😘

题目难度:中等

默认优化目标:最小化时间复杂度。

Python默认为Python3。

目录

1 题目描述

2 题目解析

3 算法原理及代码实现

3.1 按层模拟

参考文献


1 题目描述

给你一个 mn 列的矩阵 matrix ,请按照 顺时针螺旋顺序 ,返回矩阵中的所有元素。

示例 1:

img

输入:matrix = [[1,2,3],[4,5,6],[7,8,9]]
输出:[1,2,3,6,9,8,7,4,5]

示例 2:

img

输入:matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
输出:[1,2,3,4,8,12,11,10,9,5,6,7]

提示:

  • m == matrix.length

  • n == matrix[i].length

  • 1 <= m, n <= 10

  • -100 <= matrix[i][j] <= 100

2 题目解析

输入是一个m行n列的矩阵matrix,输出是一维数组ans。约束条件是顺时针螺旋输出matrix中的元素。

大致流程如下所示:

 

3 算法原理及代码实现

3.1 按层模拟

我们可以按层(行,列)将矩阵分解,先输出外层的元素,再输出次外层的数据,一次类推。

我们初始化4个指针:top指向matrix第一行;left指向matrix第一列;bottom指向matrix最后一行;right指向matrix最后一列。然后按照以下步骤将matrix中元素压入ans。

① 从左到右遍历上侧元素,从(top,left)到(top,right)

② 从上到下遍历右侧元素,从(top+1,right)到(bottom,right)

当left<right和top<bottom

③ 从右往左输出下侧元素,从(bottom,right-1)到(bottom,left+1)

④ 从下到上输出左侧元素,从(bottom,left)到(top+1,left)

之后left++;top++;right--;bottom--;

算法流程图如下:

 

时间复杂度为O(mn)。不算ans的空间,则空间复杂度为O(1)。

C++代码实现

class Solution {
public:
    vector<int> spiralOrder(vector<vector<int>>& matrix) {
        int m=matrix.size(),n=matrix[0].size();
​
        if(!(m*n)) return {};
        vector<int> ans;
        int top=0,left=0,bottom=m-1,right=n-1;
        while(top<=bottom && left<=right){
            for(int column=left;column<=right;column++){
                ans.push_back(matrix[top][column]);
            }
            for(int row=top+1;row<=bottom;row++){
                ans.push_back(matrix[row][right]);
            }
            if(top<bottom && left<right){
                for(int column=right-1;column>left;column--){
                    ans.push_back(matrix[bottom][column]);
                }
                for(int row=bottom;row>top;row--){
                    ans.push_back(matrix[row][left]);
                }
            }
            left++;right--;top++;bottom--;
        } 
        return ans;
​
    }
};

Python代码实现

class Solution:
    def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
        m,n=len(matrix),len(matrix[0])
        if m*n==0:
            return list()
        ans=list()
        left,right,top,bottom=0,n-1,0,m-1
        while left<=right and top<=bottom:
            for column in range(left,right+1,1):
                ans.append(matrix[top][column])
            for row in range(top+1,bottom+1,1):
                ans.append(matrix[row][right])
            if left<right and top<bottom:
                for column in range(right-1,left,-1):
                    ans.append(matrix[bottom][column])
                for row in range(bottom,top,-1):
                    ans.append(matrix[row][left])
            left+=1
            right-=1
            top+=1
            bottom-=1
        return ans

参考文献

力扣面试经典150题

力扣官方题解


http://www.kler.cn/news/283289.html

相关文章:

  • React Hooks 的高级用法
  • LuaJit分析(八)LuaJit预编译库函数加载过程
  • 【秋招笔试】8.21华为秋招-三语言题解
  • 算法训练营|图论第4天 110.字符串接龙 105.有向图的完全可达性 106.岛屿的周长
  • 网络原理 TCP与UDP协议
  • 本地构建spotbugs,替换gradle的默认仓库地址。
  • chapter08-面向对象编程——(Object类详解)——day09
  • 【C++ Primer Plus习题】7.5
  • Docker方式部署K8s集群
  • 灵神算法题单——不定长滑动窗口(求最长最大)
  • C#入门(13)if语句
  • HTML简单了解和基础知识记录
  • 《机器学习》 基于GANs构建数字图像生成器 探索深度学习世界
  • 群晖(Docker Compose)配置 frp 服务
  • 移情别恋c++ ദ്ദി˶ー̀֊ー́ ) ——8.stackqueuepriority_queue(模拟实现)
  • zset使用lua实现取最高分数中的随机成员
  • 使用notepad++将shell脚本转为UNIX格式方法(主要差别在换行符)
  • MySQL中的锁详解
  • AndroidStudio无线连接Android手机进行调试
  • 利润暴涨507%的携程,做对了什么?
  • C++/Qt 多媒体(续三)
  • 酒店管理系统小程序(包含源码C++实现)
  • 生成和应用patch
  • Redis入门篇 - CentOS 7下载、安装Redis实操演示
  • 每天学习一个基础算法之顺序查找
  • Python观察者模式:构建松耦合的通信机制
  • 深入理解归并排序
  • C++,如何写单元测试用例?
  • PHP语言有哪些优势和特点?
  • C语言通用函数 - 判断ip是否合法